3

我一直在做一个琐碎的任务来习惯编码。我正在设计一台 ATM 机,目前它由 2 个类组成:

  1. 银行账户.cpp

    • 不同类型账户的构造函数
    • 仅作为会员有余额
  2. 事务.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 :)

我试图研究的一个选项是制作一个属于 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'10一个int,所以你得到这个:

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

您可能打算这样做:

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

请记住,在 C++ 中,+几乎总是意味着“将这两个数字相加”。

于 2012-09-09T03:59:20.783 回答