将构造函数值从子类传递到超类c ++

时间:2018-05-08 15:11:41

标签: c++ inheritance constructor

我正在编写一个程序,其中包含超类"条目"模拟一个库输入,其中有几个子类。其中一个是musicAlbum。

entry有一个带有两个字符串参数的构造函数,用于获取借用项目的名称和年份。然后子类musicAlbum有一个构造函数来设置几个字符串参数(艺术家和记录标签)以及在超类中重新获得的参数。但是我得到一个错误,指出超类中没有默认构造函数,所以我相信我做错了很明显,但我无法看到。

任何帮助?

这是超类:

Class entry {

protected:
    int borrowed;
    string name, borrowedBy, year;

public:

entry(string cborrowedBy, string cyear) {
    borrowed = 1; //Borrowed changes to 1 to indicate that is currenty borrowed
    year = cyear;
    borrowedBy = cborrowedBy;
};    

virtual void entryBorrowed(string fname) {
    name = fname;
};
void entryReturned() {
    borrowed = 0;
}; // Calling this functions changes int borrowed into 0 to indicate that has been returned
virtual void printDetails() {};

};

这是子类:

    class musicAlbum : public entry {

protected: 

    string artist, recordLabel;

public:
    musicAlbum(string cmborrowedBy, string cmyear, string cartist, string crecordLabel){
         entry(cmborrowedBy, cmyear);
         artist = cartist;
         recordLabel = crecordLabel;

    }
    void entryBorrowed(string fname) {

        name = fname;



    }
    void printDetails() {

        cout << "This entry borrowed by: " << borrowedBy << " in " << year
            << endl << endl << "Name: " << name << endl << "Artist: " << artist << endl << "Record Label: " << recordLabel << endl;


    };


};

1 个答案:

答案 0 :(得分:1)

你可以这样做:

musicAlbum(string cmborrowedBy, string cmyear, string cartist, string crecordLabel):entry(cmborrowedBy, cmyear)
{
   //...