【问题标题】:Object is initializing to unwanted value对象正在初始化为不需要的值
【发布时间】:2012-09-09 03:53:29
【问题描述】:

我一直在做一个琐碎的任务来习惯编码。我正在设计一台 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 :)

我试图研究的一个选项是创建一个属于 Transaction.cpp 的提示函数。这将完成 main 中所做的所有事情,当然除了初始化对象。

【问题讨论】:

    标签: c++


    【解决方案1】:

    你的问题是这一行:

    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++ 中,+ 几乎总是意味着“将这两个数字相加”。

    【讨论】:

    • 言归正传,试试: cout
    • @CraigNelson 取决于他们是否要刷新输出。如果您不在乎输出何时显示,以'\n' 结束行是完全有效的。
    • 真的,因为我总是在终端使用 crlf vs. cr vs. lf 的情况下使用它。你不知道,而使用 endl,你不必知道。这是它在那里的原因之一。我从没想过它会刷新流,因为当我想要它时,我会触发 flush()。
    • @Ihatesyntaxerrors 在您的情况下,克雷格可能是正确的,您应该更喜欢endl。刷新缓冲区意味着输出将立即出现。如果你有很多的输出,它可能会减慢你的程序,但它对于调试和具有可预测的输出非常有用。基本上,请使用endl,除非您有充分的理由不这样做。
    • @CraigNelson:如果您输出'\n',它将自动转换为托管环境的正确行尾。有关部分解释,请参阅here。 (编辑:仅适用于未打开二进制输出的流)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多