2016-08-28 5 views
1

私は、基底クラス(メディア)を拡張する複数の子クラスを持っています。 子クラスのコンストラクタを呼び出すと、プライベートメンバーmBookLateFeeの値を使用して計算を行うことができません。メンバーの内容ではなく、デフォルトの0.0を使用しているようです。 しかし、そこに実際の値1.25を置くと、それは動作します。プライベートメンバーを使用する子クラスコンストラクタ

オブジェクトを作成すると、子クラスのメンバーから初期化できませんか?

class Media { 

private : 
    string mCallNumber; 

protected: 
    string mStatus; 
    string mTitle; 
    string mType; 
    int mDaysOverDue = 0; 
    double mDailyLateFee = 0.0; 
    double mTotalLateFees = 0.0; 

public: 
    Media(string status, string title, int days=0, string callNum="", string type="", double fee=0.0) : mStatus(status), 
      mTitle(title), mDaysOverDue(days), mCallNumber(callNum), mType(type), mDailyLateFee(fee) {}; 
    ~Media(){}; 
    void setCallNo(string newCallNum); 
    string getCallNo(); 
    void setHowLate(int numDays); 
    int getDaysOverDue(); 
    virtual void print(); 
    virtual double getFees() = 0; 

}; 




class Book : public Media { 
private: 
    double mBookLateFee = 1.25; 

protected: 
    string mAuthor; 
    int mNumPages; 

public: 
    Book(string status, string title, int days, string callNum, string author, int pages=0) : 
       Media(status, title, days, callNum, "Book", mBookLateFee), mAuthor(author), mNumPages(pages){ 
        mTotalLateFees = mDaysOverDue * mDailyLateFee; 
       }; 
    double getFees() { return mTotalLateFees;} 
    void print(); 

}; 

答えて

2

スーパークラスである親クラスは、子クラスの前に構築されます。

子クラスが親クラスのコンストラクタであるスーパークラスを呼び出すとき、子クラスはまだ構築されていません。 mBooklateFeeはまだ構築されていません。

言い換えれば、それはまだ存在しません。したがって、それを使用して親クラスのコンストラクタを呼び出すことはできません。

+0

私はそれがわかった、確認していただきありがとうございます! – NZSteve

関連する問題