【问题标题】:Pass constructor values from subclass to superclass c++ [duplicate]将构造函数值从子类传递到超类c++ [重复]
【发布时间】:2018-05-08 15:11:40
【问题描述】:

我正在编写一个程序,其中超类“Entry”模拟一个库输入,其中有几个子类。其中之一是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;


    };


};

【问题讨论】:

    标签: c++ inheritance constructor


    【解决方案1】:

    你可以这样做:

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

    【讨论】:

      猜你喜欢
      • 2015-04-26
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 2019-03-18
      • 2011-01-20
      • 1970-01-01
      相关资源
      最近更新 更多