2012-09-09 40 views
3

我一直在研究一個微不足道的工作以適應編碼。我設計一個自動取款機和此刻它是由2類:對象正在初始化爲不需要的值

  1. BankAccount.cpp

    • 構造不同類型的帳戶
    • 只有具有平衡作爲成員
  2. Transaction.cpp

    • 對所述的BankAccount的方法(即使存款,使戒斷& GET平衡)

問題:的BankAccount被自動初始化爲10的平衡這是不希望。例如,如果我創建了一個支票賬戶並選擇存入10美元,餘額將打印出20美元。

//BankAccount.h 
//This class will simply take in a bank account 
//with a balance, other classes will use a bank account object 
//such as saving/checkings and perform operations on the 
//balance 

#ifndef BANK_ACCOUNT_H 
#define BANK_ACCOUNT_H 
class BankAccount { 

private: 
    float balance; 
public: 
    BankAccount(); 
    float getBalance(); 
    void makeDeposit(); 
    void makeWithdrawl(); 

}; 

#endif 

//BankAccount.cpp 
#include "BankAccount.h" 
#include <iostream> //remove once done *not to self 
using namespace std; //remove once done *note to self 


BankAccount::BankAccount() { 
    balance = 0.00; 
} 

float BankAccount::getBalance() { 
    return balance; 
} 

void BankAccount::makeDeposit() { 
    cout << "How much would you like to deposit: "; 
    float deposit_value; 
    cin >> deposit_value; 
    balance += deposit_value; 
} 

void BankAccount::makeWithdrawl() { 
    cout << "How much would you like to withdrawl: "; 
    float withdrawl_value; 
    cin >> withdrawl_value; 
    balance -= withdrawl_value; 
} 

//Transaction.h 
#ifndef TRANSACTION_H 
#define TRANSACTION_H 

class Transaction { 
private: 
    BankAccount m_bao; 
public: 
    Transaction(BankAccount&); 
    void displayOptions(); 
    void printReciept(); 
}; 

#endif 

//Transaction.cpp 
#include "BankAccount.h" 
#include "Transaction.h" 
#include <iostream> 
using namespace std; 

Transaction::Transaction(BankAccount& bao) { 
    m_bao = bao; 
} 

void Transaction::displayOptions() { 
    cout << "\nPlease make a choice\n\n"; 
    cout << "1: Make Deposit\n"; 
    cout << "2: Make Withdrawl\n"; 
    cout << "3: Check Balance\n"; 

    int choice; 
    cin >> choice; 
    switch (choice) { 
    case 1: 
     m_bao.makeDeposit(); 
     break; 
    case 2: 
     m_bao.makeWithdrawl(); 
     break; 
    case 3: 
     m_bao.getBalance(); 
     break; 
    } 
} 

void Transaction::printReciept() { 
    cout << "Current balance is now: " << m_bao.getBalance() + '\n'; 
} 


int main() { 

    BankAccount checking; 
    Transaction q(checking); 
    q.displayOptions(); 
    q.printReciept(); 


} 

我相信答案是正確的在我眼前,但我的大腦現在只是炒。我會繼續尋找解決方案,並讓你們知道我的問題是否已經解決。

[編輯]

好了,現在我試圖讓這個客戶可以選擇在任何支票或儲蓄賬戶進行交易。目前,我得到了它看起來像這樣在我的主要():

int main() { 

    BankAccount checking(0.00); 
    BankAccount savings(0.00); 
    Transaction c(checking); 
    Transaction s(savings); 
    for(int i = 0; i < 10 ; i++) { 
     cout << "Make an option" << endl; 
     cout << "1. Checking " << endl; 
     cout << "2. Savings"  << endl; 

     int choice; 
     cin >> choice; 
     if (choice == 1) { 
      c.prompt(); 
      c.printReciept(); 
     } 
     else { 
      s.prompt(); 
      s.printReciept(); 
     } 
    } 

}

它工作正常,但我想使這個過程更OOP-alized,如果是有道理的:)

我試圖查看的一個選項是創建一個屬於Transaction.cpp的提示函數。這將完成所有主要工作,除了初始化對象。

回答

5

你的問題是這樣的一行:

cout << "Current balance is now: " << m_bao.getBalance() + '\n'; 

,編譯器看到的是:

cout << "Current balance is now: " << (m_bao.getBalance() + '\n'); 

'\n'10爲int,所以你得到這樣的:

cout << "Current balance is now: " << (m_bao.getBalance() + 10); 

你大概是這樣做的:

cout << "Current balance is now: " << m_bao.getBalance() << '\n'; 

請記住,在C++中,+幾乎總是意味着「添加這兩個數字」。

+1

更重要的是,嘗試:cout <<「當前餘額現在是:」<< m_bao.getBalance()<< endl; – WhozCraig

+1

@CraigNelson取決於他們是否想要衝洗輸出。如果你不關心輸出何時出現,用'\ n''結束這一行是完全有效的。 –

+0

真的,因爲我總是在終端使用crlf vs. cr vs. lf時使用它。你不知道,並使用endl,你不必知道。它是其中的原因之一。我從來沒有想過它衝過溪流,因爲當我想要這樣的時候,我會沖洗()。 – WhozCraig