2015-12-08 182 views
2

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

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; 
    } 
}; 

薪水阶级本质上是同样的事情的现在。这里就是我试图制作我的小时工:

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

我知道,它从来没有得到过去的构造,因为我试图清点测试和程序从不那么远。

+0

这是不正确的实际接受抄袭答案。 – g24l

回答

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

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

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

你的构造函数试图在碰撞与wrong aliasing和结果分配(注意在构造函数指针使用本)。您可以通过在构造函数的初始化程序列表中执行初始化来克服第一部分。但是,切片仍然会发生。

您应该将其分配给其声明的类或由基类指针保存。

例如1

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

例如, 2

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

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

virtual ~employee() = default; 
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;