继承类中的构造函数c ++

时间:2015-12-08 00:27:26

标签: c++ inheritance

我有一个父类:有两个继承类的员工:每小时和工资 在父类中,我重载了<<所以它将输出员工的所有变量值。我需要创建3个新员工:2个每小时和1个薪水,但我的构造函数似乎没有正常工作。程序将编译,但是当我调用Hourly构造函数时程序停止工作(堆栈溢出?)。这是一些代码:

class employee
{
    friend ostream& operator<<(ostream& out, const employee& emp);

    public:

    employee ();
    employee(string id, string fname, string lname, string bDate, string hDate, double pay);
    ~employee();
    void setEmpId(string id);
    string getEmpID();
    void setFirstName(string name);
    string getFirstName();
    void setLastName(string name);
    string getLastName();
    void setBirthDate(string birthDate);
    string getBirthDate();
    void setHireDate(string hireDate);
    string getHireDate();
    void setPayRate(double rate);
    double getPayRate();

    protected:

    string employee_id;
    string first_name;
    string last_name;
    string birth_date;
    string hire_date;
    double pay_rate;
};

这是我的父类,这是我继承的两个类:

class Hourly : public employee
{
    public: 

    Hourly(string fname, string lname, string bdate, string hdate, double rate, string id)
    {
        int random = rand() % 1000;
        this->employee_id=id;
        this->first_name=fname;
        this->last_name=lname;
        this->birth_date=bdate;
        this->hire_date=hdate;
        this->pay_rate=rate;
    }
};

Salary类与现在基本相同。这是我尝试创建我的每小时员工的地方:

employee empOne = Hourly("Brian", "Finn", "1/12/1995", "1/12/2015", 7.25, "1215");
cout << empOne;

我知道它永远不会超过构造函数,因为我试图测试并且程序永远不会让它成功。

3 个答案:

答案 0 :(得分:1)

你不能用值语义做多态。它必须是引用或指针,因此对象在对employee对象的赋值中被切片,最终得到employee而不是Hourly。您可以通过在堆上创建对象来停止此操作。您还应该将基类析构函数定义为虚拟,否则在通过基类指针删除时将调用错误的析构函数。

最后,您应该在派生类构造函数中调用基类构造函数。

通过所有这些更改,它对我来说很好。

#include <iostream>
#include <string>
#include <cstdlib>

using std::cout;
using std::string;
using std::ostream;

class employee
{
    friend ostream& operator<<(ostream& out, const employee& emp);

    public:
    employee ();
    employee(string const& id, string const& fname, string const& lname, string const& bDate, string const& hDate, double pay)
        : employee_id(id), first_name(fname), last_name(lname), birth_date(bDate), hire_date(hDate), pay_rate(pay)
    {}
    virtual ~employee(){};    

    protected:
    string employee_id;
    string first_name;
    string last_name;
    string birth_date;
    string hire_date;
    double pay_rate;
};

ostream& operator<<(ostream& out, const employee& emp)
{
    out << emp.employee_id;
    return out;
}

class Hourly : public employee
{

public: 
Hourly(string const& fname, string const& lname, string const& bdate, string const& hdate, double rate, string const& id)
    : employee(id, fname, lname, bdate, hdate, rate)
{
    int random = rand() % 1000;
}
};

void printEmployee(employee& e)
{
    cout << e << '\n';
}

int main()
{
    // using reference semantics
    Hourly empOne = Hourly("Brian", "Finn", "1/12/1995", "1/12/2015", 7.25, "1215");
    printEmployee(empOne);

    // using pointer semantics
    employee* empTwo = new Hourly("Dave", "Smith", "1/12/1995", "1/12/2015", 7.25, "1216");
    cout << *empTwo << '\n';
    delete empTwo; // would be better to use a `unique_ptr` and you wont need a delete.
}

答案 1 :(得分:1)

您的问题是您将对象分配给基类对象,然后slice它。

 employee empOne = Hourly("Brian", "Finn", "1/12/1995", "1/12/2015",
 7.25, "1215");

您的构造函数尝试使用wrong aliasing进行赋值并导致崩溃(请注意构造函数中指针的使用)。您可以通过在构造函数的初始化列表中进行初始化来克服第一部分。但是,切片仍然会发生。

您应该将其分配给其声明的类,或者通过基类指针保存它。

e.g。 1

Hourly empOne = Hourly("Brian", "Finn", "1/12/1995", "1/12/2015", 7.25, "1215");

e.g。 2

employee * empOne = new Hourly("Brian", "Finn", "1/12/1995", "1/12/2015", 7.25, "1215");

要使后者工作,您需要定义一个虚拟基类析构函数。

virtual ~employee() = default; 

答案 2 :(得分:0)

在下面的赋值语句中,每小时类型为sliced

 employee empOne = Hourly("Brian", "Finn", "1/12/1995", "1/12/2015", 7.25, "1215"); 

我怀疑以下内容可行

 Hourly hourlyEmp("Brian", "Finn", "1/12/1995", "1/12/2015", 7.25, "1215");
 cout << hourlyEmp;
相关问题