2016-08-05 17 views
0

最近我有一個面向對象的測試,問題並不那麼艱難,但他們給我的信息讓我問自己如何使用它。下面是代碼和有,我創建測試和看到一些代碼(我給的,我創建的代碼註釋)如何在類中的屬性/變量中使用組合類作爲矢量類型

#include <iostream> 
#include <vector> 
using namespace std; 

class Account{ 
protected: 
    int accno; 
    double balance; 
public: 
    // I add the initialization part to make the program works 
    // (In my test they don't provide because there is a question about that) 
    Account(int accno, double balance) : accno(accno), balance(balance){}; 
    double getBalance(); 
    int getAccno(); 
    void deposit(double amount); 
}; 

class Saving : public Account{ 
public: 
    // Same reason as above for initialization 
    Saving (int accno, double balance) : Account(accno,balance){}; 
    bool withdraw (double amount); 
    void compute_interest(); 
    // I add this method/function to see how Saving object behave 
    void print(){ 
     cout << "ID: " << accno << "  balance: " << balance << endl; 
    } 
}; 

class Bank{ 
    int ID; 
    //Saving accounts2;// If I comment this line I can compile otherwise I can't even create a Bank object 
    vector<Saving>accounts;// This is the line that I don't know how to make use of it 
public: 
    // The 2 methods/functions below & the attribute/variable "ID" I create to confirm my expectation 
    void addID(int newID){ 
     ID = newID; 
    } 
    void printID(){ 
     cout << "ID: " << ID << endl; 
    } 

    void addAccount(Saving account); 
    void print(); 
}; 

int main() 
{ 
    Saving s1(1001,500.0); 
    s1.print(); 
    Bank b1; 
    b1.addID(1111); 
    b1.printID(); 

    return 0; 
} 

所有主要功能裏面的代碼創建看到輸出的樣子。

我認爲下面的代碼是我們如何能夠利用的所有類 (但我仍然不知道如何使用,雖然向量)

Saving s1(5000,600.00); 
Saving s2(5001,111.11); 
Bank b1; 
b1.addAccount(s1); 
b1.addAccount(s2); 

所以我真正想知道的部分是:

  1. 如何利用這些代碼,這樣類似 上面的代碼的東西都可以使用(或更好的只是使用反推功能 創建新實例,因爲它的載體)

////

vector<Saving> accounts; 
void addAccount(Saving account); 
  • 爲什麼如果創建的我不能編譯 「保存accounts2;」
  • 回答

    0

    爲了給出一些如何使用矢量的指導,請看看:http://www.cplusplus.com/reference/vector/vector/介紹這個類。只是一個簡短的提示:在最常見的情況下,你通常添加到一個向量與push_back。這將在向量的末尾添加一個元素。然後你迭代它(Iteration over std::vector: unsigned vs signed index variable),並打印出所需的信息。

    關於:Saving accounts2;使您的程序無法編譯是由於您沒有默認構造函數。請參閱http://en.cppreference.com/w/cpp/language/default_constructor關於如何創建一個。

    +0

    我知道有關的push_back功能,但它只是沒有想到要使用@Vicente怎麼寫的,因爲我剛開始編程,所以我只知道如何使用普通類型添加元素而不使用類。 所以我測試一些代碼,但我似乎無法弄清楚如何打印帳戶 下面是我測試過 '無效addAccount(儲蓄賬戶)' '{ 賬戶。的push_back(帳戶); }' '空隙打印()' '{ COUT << 「ACCNO:」 <<帳戶[0] .accno << 「平衡:」 <<帳戶[0] << .balance ENDL; }' – hafhaf100

    0

    實現在Bankvoid addAccount(Saving account)功能其實帳戶添加到帳戶的載體:

    void addAccount(Saving account) { 
        accounts.push_back(account); 
    } 
    

    實現在Accountdouble getBalance()int getAccno()函數分別返回賬號和平衡:

    double getBalance() { return balance; } 
    int getAccno() { return accno; } 
    

    落實Bank類的print()功能打印賬戶麻木呃,並存儲在accounts矢量每個帳戶餘額:

    void print() { 
        for (vector<Saving>::iterator it = accounts.begin(); it != accounts.end(); it++) { 
         cout << "accno: " << it->getAccno() << " balance: " << it->getBalance() << endl; 
        } 
    } 
    
    +0

    所以這就是你如何在類中添加一個元素與矢量。因爲我剛開始學習班,我通常只需要創建新的對象爲類如: '保存S1(1000,100.00);' '保存S2(1001,200.00);' 所以,我寫我的代碼只是重載的構造函數,訪問器和mutator。 所以我覺得我應該這樣寫: 'void Saving :: addAccount(Saving account)''{{{0} {0} }' – hafhaf100

    +0

    在'bank'類中,你有一個'addAccount'方法,它目前還沒有實現(沒有任何代碼包含在'{'和'}'之間,因此,按照我的說法我的回答是,以'account'作爲參數傳遞它,並將其存儲到'bank'類的'accounts'向量中。 –

    +0

    感謝您的快速回復。我已經完成並編譯現在的問題我似乎不知道如何編寫函數'print'的實現。以下是我嘗試的 'cout <<「accno:」<< accounts [0] .accno <<「餘額:「<< accounts [0] .balance << endl;' 我知道變量已被保護,但我被卡住了 – hafhaf100

    相關問題