2017-11-25 22 views
0

Account是基類,並且檢查和保存是派生類我試圖設置速率(這是cheking和saving類中的方法)。我怎樣才能調用這樣的方法。 Attched是代碼的骨架。歡迎任何建議,因爲我的不實。使用數組作爲對象從派生類中調用方法

class Account { 
public: 
    void setAccNum(long acct) { 
    } 

    void setBalance(double bal) { 

    } 

    string toString(){ 


    } 


    double virtual computeIntr(int years) { 

    } 

}; 

class Checking : public Account { 

public: 

    Checking() { 

    } 
    Checking(long acct, double bal) : Account(acct, bal) { 

    } 
    void setMinIntrBalance(int minb) { 


    } 

     void setRate(int r) { 

    } 


    double computeIntr(int years) { 

    } 




    string toString() { 


    } 

}; 

class Saving : public Account { 
public: 
    Saving() { 

    } 
    Saving(long acct, double bal, double rate) : Account(acct, bal) { 



    } 

    void setRate(double rate) { 

    } 

    double computeIntr(int years) { 

    } 

    string toString() { 
}; 





int main() { 

    Account **accountPtrs; 
    accountPtrs = new Account*[100]; 

    for (int i = 0; i < 100; i++) { 
     accountPtrs[i] = nullptr; 
    } 
    accountPtrs[0] = new Checking(100, 1000 + 1000); 
    accountPtrs[1] = new Checking(101, 1000 + 1010); 
    accountPtrs[2] = new Checking(102, 1000 + 1020); 
    accountPtrs[3] = new Checking(103, 1000 + 1030); 
    accountPtrs[4] = new Checking(104, 1000 + 1040); 

    accountPtrs[5] = new Saving(200, 1000 + 2000, 3); 
    accountPtrs[6] = new Saving(201, 1000 + 2010, 3); 
    accountPtrs[7] = new Saving(202, 1000 + 2020, 3); 
    accountPtrs[8] = new Saving(203, 1000 + 2030, 3); 
    accountPtrs[9] = new Saving(204, 1000 + 2040, 3); 


    bool exit = true; 
    while (exit == true) { 

     cout << "5) Set Interest" << endl; 

     cout << " Please enter your choice: "; 
     int ch; 
     cin >> ch; 
     cout << endl; 

     switch (ch) 
     { 

     case 5: 
     { 
      bool a = true; 
      while (a == true) { 
       int h, b; 
       char t; 
       cout << "YOU WANT TO SET THE INTEREST OF AN ACCOUUNT." << endl; 
       cout << "Saving or checking: "; 
       cin >> t; 

       if (t == 'c' || t == 'C') { 
        cout << "Enter account number: "; 
        cin >> h; 
        h = h - 100; 
        cout << "ENTER THE INTEREST RATE:"; 
        cin >> b; 

        accountPtrs[h]->setRate(); /////// THIS IS WHERE IT DOES NOT WORK , HOW CAN I CALL SUCH METHOD AND SET THE RATE 

        a = false; 
       } 

       else if (t == 's' || t == 'S') { 
        cout << "Enter account number: "; 
        cin >> h; 
        h = h - 100; 
        cout << "ENTER THE INTEREST RATE:"; 
        cin >> b; 

        accountPtrs[h]-> setRate(b); 
        a = false; 

       } 

       else { 

        cout << "INVALID INPUT" << endl; 

        a = true; 
       } 

      } 

      break; 
     } 
    } 

    return 0; 


} 
+0

爲什麼'Checking'從'Account'獲得?你真的想要表達像'Checking' **是一個**'Account'這樣的語義嗎? – user0042

回答

0

你的陣列成員是指向Account -objects,但Accout不提供方法setRate;只有子類才行。 您需要在Account級別提供setRate,並將其實現爲空或聲明爲抽象。然後,accountPtrs[h]->setRate()將會是有效的,並調用相應的子類的setRate -implementations:

class Account { 
public: 
    ... 
    virtual void setRate(int) = 0; 
    ... 
}; 
+0

還有一個問題:如果我想要一種方法讓我添加一個帳戶(檢查或保存),我知道我可以使用新的關鍵詞,但是如何確保新帳戶(檢查或保存)是存儲在帳戶[100]數組中? – LondonSquare