2012-09-09 4 views
3

저는 코딩에 익숙해 지도록 간단한 작업을하고 있습니다. 나는 ATM 기계를 설계하고 순간에 그것은 2 개의 클래스로 구성되어 있습니다 만 구성원으로 균형을이 계정 개체가 원치 않는 값으로 초기화되고 있습니다.

  • 다양한 유형의

    1. BankAccount.cpp

      • 생성자를
    2. Transaction.cpp

        BankAccount가 자동으로 바람직하지 (10)의 균형으로 초기화된다 :

    는 문제 (즉 입금, 철수 & GET 잔액을 확인)을는 BankAccount의 방법을 수행. 예를 들어, 당좌 예금 계좌를 만들고 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에 속할 프롬프트 기능을 만드는 것입니다. 이것은 물론 객체를 초기화하는 것을 제외하고는 main에서 수행되는 모든 작업을 수행합니다.

  • 답변

    5

    귀하의 문제는이 라인 :

    컴파일러로보고
    cout << "Current balance is now: " << m_bao.getBalance() + '\n'; 
    

    :

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

    당신을 : 당신이 얻을 수 있도록

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

    '\n'이 int로 10입니다 아마 이것을하기위한 것입니다 :

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

    C++에서 +은 거의 항상 "이 두 숫자 추가"를 의미한다는 것을 기억하십시오.

    +1

    더 많은 것을 시도해보십시오 : cout << "현재 잔액이되었습니다 :"<< m_bao.getBalance() << endl; – WhozCraig

    +1

    @CraigNelson 출력을 플러시할지 여부를 결정합니다.' '\ n' '으로 줄을 끝내면 출력이 나타나면 신경 쓰지 않으면 완전히 유효합니다. –

    +0

    정말, 항상 터미널이 crlf 대 cr 대 lf를 사용하는 기회를 이용합니다. 당신은 몰라. 그리고 endl을 사용하면, 당신은 알 필요가 없다. 그것의 이유 중 하나는 왜 거기에. 나는 그 물줄기를 홍조로 생각하지 않았다. 왜냐하면 내가 그것을 원할 때, 나는 내뿜는다. – WhozCraig

    관련 문제