介绍银行账户类 - 奇怪的输出
Intro Bank Account Class - Strange Output
本文关键字:输出 更新时间:2023-10-16
这是为了类作业的介绍,我将创建一个银行账户,该账户能够通过存款或取款来操纵余额并查找账户余额。
我已经看了十几个不同的例子和操作方法,但现在我不知所措。
我从代码中获得的唯一输出是Account Balance: $-8589937190
我不知道这个值从何而来。关于我应该从哪里开始的任何想法?
#include <iostream>
using namespace std;
// Define Account class
class Account
{
public:
Account(int startingBal = 0){
m_startingBal = startingBal;
}
void credit(int amount);
void withdraw(int amount);
int getBalance() const;
private:
int m_startingBal;
int balance;
};
void Account::credit(int amount) // deposit money
{
balance += amount;
};
void Account::withdraw(int amount) // withdraw money
{
balance -= amount;
};
int Account::getBalance() const // return the current balance
{
cout << "Account Balance: $" << balance << endl;
return balance;
};
int main()
{
Account account(1500); // create an Account object named account with startingBal of $1500
account.credit(500); // deposit $500 into account
account.withdraw(750); // withdraw $750 from account
account.getBalance(); // display balance of account
system("PAUSE"); // to stop command prompt from closing automatically
return 0;
} // end main
balance
成员变量永远不会分配给(在构造函数中),因此包含垃圾值。
事实上,你似乎有一个错误。在您的构造函数中,您设置了m_startingBal
但不在其他任何地方使用它,而balance
不是在构造函数中设置的,而是在其他任何地方使用。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 如何根据单词在文本中出现的概率输出单词