2015-12-08 185 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;