为什么在函数中传递参数时算术计算中出现错误?

时间:2016-04-28 01:42:53

标签: c++ constructor

  

下面的代码编译正确,但计算似乎是一个   问题。当打印出第一个扣除'退出'时,   customerBalance是2.07361e(8032.78 - 244.0 = 7788.78)?   数据成员是私有的,但它似乎不是一个问题   是公共的还是私人的。我不确定。任何建议都是   欢迎。谢谢。

#include <iostream>
#include "BankAccount.hpp"
using namespace std;

BankAccount::BankAccount(string name, string ID, double balance)
{
    customerName = name;
    customerID = ID;
    customerBalance = balance;

}

string BankAccount::getCustomerName()
{
    return customerName;
}

string BankAccount::getCustomerID()
{
    return customerID;
}

double BankAccount::getCustomerBalance()
{
    return customerBalance;
}

void BankAccount::withdraw(double w)
{

    customerBalance = (customerBalance - w);
}

void BankAccount::deposit(double d)
{
    customerBalance = (customerBalance + d);
}

int main()
{
    double customerBalance;

    BankAccount account1("Harry Potter", "K4637", 8032.78);
    account1.withdraw(244.0);
    cout << customerBalance;
    account1.withdraw(3012.58);
    account1.deposit(37.54);
    account1.withdraw(1807.12);
    account1.deposit(500.00);
    double finalBalance = account1.getCustomerBalance();
 }

1 个答案:

答案 0 :(得分:3)

您在cusotmerBalance中声明的mainBankAccount::customerBalance不同。您永远不会初始化customerBalance,因此在打印时它具有未定义的值。