2013-06-01 54 views
0

這是我到目前爲止的代碼,它編譯並運行良好,但我需要修改它的幫助。這是一個銀行應用程序,目前僅適用於一個帳戶。指向類實例C++的指針數組

它需要適應兩個新文件:bank.h和bank.cpp,main應該包含一個指向bank的指針,bank應該包含一個指向account的實例的指針數組。

因此新的接口將工作成才類似於:

帳戶> 1 12

1是帳​​戶#和12是被depositted的ammount的。

我真的需要幫助,以適應我的代碼做到這一點,我迷失在如何創建銀行指針數組帳戶的實例。任何幫助深表感謝。包含與共享變量和函數

class account{ 

    private: 

    int balance; 

    public: 

     account(); 
     ~account(); 
     int value; 
     int account_balance(); 
     int deposit(); 
     int withdraw(); 
     int init(); 

}; 

//account.cpp實現文件

using namespace std; 

#include <iostream> 
#include "account.h" 

account::account(){ 
} 

account::~account(){ 
} 

//balance overwrite function 
int account::init(){ 

    balance = value;  
} 

//balance function 
int account::account_balance() { 

    return balance; 
} 

//deposit function 
int account::deposit(){ 

    balance += value; 
} 

//withdraw function 
int account::withdraw(){ 

    //error handling 
    if(value>balance){ 
     cout << "Error! insufficient funds." << endl; 
     return 0; 
     } 

    balance -= value; 
} 

回答

0

對於數組可以使用std類

//main.cpp file 
using namespace std; 

#include <iostream> 
#include <stdlib.h> 
#include "account.h" 

//allocate new space for class pointer 
account* c = new account; 

//function for handling I/O 
int accounting(){ 

string command; 

cout << "account> "; 
cin >> command; 

    //exits prompt 
    if (command == "quit"){ 
     exit(0); 
     } 

    //overwrites account balance 
    else if (command == "init"){ 
     cin >> c->value; 
     c->init(); 
     accounting(); 
     } 

    //prints balance 
    else if (command == "balance"){ 
     cout << "" << c->account_balance() << endl; 
     accounting(); 
     } 

    //deposits value 
    else if (command == "deposit"){ 
     cin >> c->value; 
     c->deposit();   
     accounting(); 
     } 

    //withdraws value 
    else if (command == "withdraw"){ 
     cin >> c->value;  
     c->withdraw(); 
     accounting(); 
     } 

    //error handling  
    else{ 
     cout << "Error! Invalid operator." << endl; 
     accounting(); 
     } 
//frees memory   
delete c;   
} 


int main() { 

accounting(); 

return 0; 
} 

//account.h頭文件:: vector類。

std::vector<account *>MyAccounts; 
MyAccounts.push_back(new account()); 

然後,你可以像使用數組一樣正常訪問它。

MyAccounts[i]->accountFunction(); 

更新

我不知道有足夠的瞭解你的代碼,所以我給這裏只是一些普通的例子。

在你的銀行課上,你有一個像上面顯示的成員)MyAccounts。現在,當您向銀行添加新賬戶時,您可以使用推回功能來完成此操作。

例如,添加一個新帳戶並設置100個子項的初始金額。

MyAccounts.push_back(new account()); 
size_t i = MyAccounts.size(); 
MyAccounts[i]->setAmount(100); 
+0

你能幫我在bank.h和bank.cpp中實現這個嗎 – Ianschramm

+0

我在這裏向你展示了一些示例代碼。如果你有多個帳戶,你需要一些方法來識別它。或者給它一個名字或者一個id,比如一個銀行賬號。所以當你想存一些錢的時候,你必須指定id來使用正確的賬戶。 – Devolus

+0

對不起,我應該更清楚。它應該有20個指數,每個指數都是一個帳戶。所以帳戶[0]是第一個帳戶。 – Ianschramm

0

你可以做類似下面

class Bank 
{ 
public: 
int AddAccount(Account act){ m_vecAccts.push_back(act);} 
.... 
private: 
... 
std:vector<account> m_vecAccts; 
} 

更新: 這僅僅是一個銀行類賬戶的私有成員變量的載體。 AddAccount是公開的功能,可以添加帳戶載體

+0

這似乎是在我的頭上,你能解釋更多嗎? – Ianschramm

+0

@lanschramm你能解釋一下你腦袋上的事情嗎? – anand

0

首先,

//error handling  
else{ 
    cout << "Error! Invalid operator." << endl; 
    accounting(); 
    } 

這個長得不好看,你是遞歸調用計費功能,每一個錯誤的輸入之後。設想一種情況,用戶輸入1 000 000x的錯誤輸入......然後,您將嘗試釋放內存1 000 000次 - 一次輸入成功!

//frees memory   
delete c; 

整個會計功能設計錯誤。我想你不想在某種交易後破壞賬戶,對吧?我想,誰收回10塊錢從他們10萬美元的帳戶將會被銷燬的人,會立即改變銀行:)

因此,與持續一段時間週期可能是解決

//function for handling I/O 
int accounting(){ 

string command; 

while(true) { 
cout << "account> "; 
cin >> command; 

    //exits prompt 
    if (command == "quit"){ 
     break; 
     } 

    //overwrites account balance 
    else if (command == "init"){ 
     cin >> c->value; 
     c->init(); 
     continue; 
     } 

    //prints balance 
    else if (command == "balance"){ 
     cout << "" << c->account_balance() << endl; 
     continue; 
     } 

    //deposits value 
    else if (command == "deposit"){ 
     cin >> c->value; 
     c->deposit();   
     accounting(); 
     } 

    //withdraws value 
    else if (command == "withdraw"){ 
     cin >> c->value;  
     c->withdraw(); 
     continue; 
     } 

    //error handling  
    else{ 
     cout << "Error! Invalid operator." << endl; 
     continue; 
     }   
} 

然後,

int value; 

不是類成​​員,它應該是的方法參數撤回和存款,這樣

//deposit function 
void account::deposit(int value){ //int changed to void, you are not returning anything! 

    balance += value; 
} 

//withdraw function 
bool account::withdraw(int value){ 
//error handling 
if(value>balance){ 
    cout << "Error! insufficient funds." << endl; 
    return false; 
    } 
if(value<0) { 
    cout << "Haha, nice try!" << endl; 
    return false; 
} 
balance -= value; 
return true; 

}