下面的代码编译正确,但计算似乎是一个 问题。当打印出第一个扣除'退出'时, 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();
}
答案 0 :(得分:3)
您在cusotmerBalance
中声明的main
与BankAccount::customerBalance
不同。您永远不会初始化customerBalance
,因此在打印时它具有未定义的值。