对象正在初始化为不需要的值

Mak*_*sim 3 c++

我一直致力于一项微不足道的工作,习惯于编码.我正在设计一台ATM机,目前它由两个类组成:

  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();


}
Run Code Online (Sandbox Code Playgroud)

我确信答案就在我的眼前,但我的大脑现在才被炸好.我会继续寻找解决方案,让你们知道我的问题是否已经解决了.

[编辑]

好吧,现在我正在努力使客户可以选择在支票或储蓄账户上执行交易.目前我在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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

}

它工作正常,但我想让这个过程更加OOP-alized,如果这是有道理的:)

我试图研究的一个选项是创建一个属于Transaction.cpp的提示函数.除了初始化对象之外,这将完成在main中完成的所有操作.

Rei*_*ica 5

你的问题是这一行:

cout << "Current balance is now: " << m_bao.getBalance() + '\n';
Run Code Online (Sandbox Code Playgroud)

编译器认为:

cout << "Current balance is now: " << (m_bao.getBalance() + '\n');
Run Code Online (Sandbox Code Playgroud)

'\n'10一个int,所以你得到这个:

cout << "Current balance is now: " << (m_bao.getBalance() + 10);
Run Code Online (Sandbox Code Playgroud)

你可能打算这样做:

cout << "Current balance is now: " << m_bao.getBalance() << '\n';
Run Code Online (Sandbox Code Playgroud)

请记住,在C++中,+几乎总是意味着"添加这两个数字".

  • @CraigNelson:如果输出''\n'`,它将自动转换为托管环境的正确行尾.有关部分说明,请参阅[此处](http://stackoverflow.com/questions/5654067/how-to-make-cout-behave-as-in-binary-mode).(编辑:仅适用于未针对二进制输出打开的流) (2认同)