3

私はコーディングに慣れるために些細な課題に取り組んできました。私はATMマシンを設計していますが、現時点では2つのクラスで構成されています。

  1. BankAccount.cpp

    • さまざまなタイプのアカウントのコンストラクター
    • メンバーとしてのみバランスが取れている
  2. Transaction.cpp

    • BankAccountでメソッドを実行します(つまり、預金、引き出し、残高の取得)

問題: 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();


}

答えは目の前にあると思いますが、今は脳が揚げられています。私は引き続き解決策を探し、私の問題がまだ解決されているかどうかを皆さんに知らせます。

[編集]

了解しました。お客様が当座預金口座または普通預金口座のいずれかで取引を実行できるようにしようとしています。現在、main()で次のようになっています。

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化したいと思います:)

私が調べようとしていたオプションの1つは、Transaction.cppに属するプロンプト関数を作成することでした。これは、もちろんオブジェクトを初期化することを除いて、mainで行われるすべてのことを行います。

4

1 に答える 1

5

あなたの問題はこの行です:

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

コンパイラはこれを次のように認識します。

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

'\n'10intとしてであるため、次のようになります。

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

あなたはおそらくこれをするつもりでした:

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

+C ++では、ほとんどの場合、「これら2つの数値を加算する」ことを意味することを忘れないでください。

于 2012-09-09T03:59:20.783 に答える