作者:安南微 | 来源:互联网 | 2023-09-16 12:30
我想对银行中不同类型的账户进行一些基本的模拟。我已经创建了一个类Account
,Savings_Account
&Checking_Account
是一个Account
andTrust_Account
是一个Savings_Account
. 我只在Account
课堂上有问题,所以这个问题只会集中在Account
课堂上。我在Account
班级中只有两个属性:std::string name
和double balance
. 我在Account
课堂上也有两个方法:bool deposit(double amount)
和bool withdraw(double amount)
。问题:当我deposit(amount)
工作正常并更新balance
时,但当我withdraw(amount)
更新时balance
没有反映并amount
退出默认余额。
我的代码:
帐户.hpp
#pragma once
#include
#include
#include
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
//The Account class(parent class)
class Account{
friend std::ostream &operator<<(std::ostream &os, const Account &account); //Outputs the object
private:
static constexpr const char *def_name = "Empty"; //Default name
static constexpr double def_balance = 0.0; //Default balance
protected:
string name{}; //Name of the Account holder
double balance{}; //Balance of the Account
public:
Account(string n = def_name, double b = def_balance); //Constructor
bool deposit(double amount); //Method to deposit
bool withdraw(double amount); //Method to withdraw
};
帐户.cpp
#include "Accounts.hpp"
//Construtor
Account::Account(string n, double b)
:name{n}, balance{b} {
}
//Method for deposit
bool Account::deposit(double amount) {
if (amount <0)
return false;
else {
balance += amount;
return true;
}
}
//Method for withdraw
bool Account::withdraw(double amount) {
if (balance - amount >= 0) {
balance -= amount;
return true;
} else {
return false;
}
}
//Outputs the object
std::ostream &operator<<(std::ostream &os, const Account &account) {
os <<"A-> " < return os;
}
我也做了一些实用功能,让我display
,deposit
以及withdraw
所有Account
在vector acnt
AccountsUtil.hpp
#include "Accounts.hpp"
/* Util functions for Account */
//Displays all the Account
void display(const vector &a);
//Deposits to all the Account
void deposit(vector &a, double bl);
//Withdraws from all the Account
void withdraw(vector &a, double bl);
AccountsUtil.cpp
/* Utils for Account */
//Displays all the Account
void display(const vector &a) {
cout <<"--->Accounts----------------" < for (const auto &i: a)
cout < cout <<"=====================================n" <}
//Deposits to all the Account
void deposit(vector &a, double bl) {
cout <<"Depositing for Account=========================" < for (auto i: a) {
if (i.deposit(bl))
cout <<"Deposit successful for:=> " < else
cout <<"Deposit failed for:=> " < }
cout <<"==============================================n" <}
//Withdraws from all the Account
void withdraw(vector &a, double bl) {
cout <<"Withdrawing for Account============================" < for (auto i: a) {
if (i.withdraw(bl))
cout <<"Withdraw successful for :=> " < else
cout <<"Withdraw failed for :=> " < }
cout <<"===================================================n" <}
主程序
#include "AccountsUtil.hpp"
int main() {
vector acnt;
acnt.push_back(Account {});
acnt.push_back(Account {"Tony"});
acnt.push_back(Account {"Steve", 2000});
acnt.push_back(Account {"Thor", 5000});
display(acnt);
deposit(acnt,1000);
withdraw(acnt,3000);
return 0;
}
我的输出(错误/意外)
--->Accounts----------------
A-> Empty : 0
A-> Tony : 0
A-> Steve : 2000
A-> Thor : 5000
=====================================
Depositing for Account=========================
Deposit successful for:=> A-> Empty : 1000
Deposit successful for:=> A-> Tony : 1000
Deposit successful for:=> A-> Steve : 3000
Deposit successful for:=> A-> Thor : 6000
==============================================
Withdrawing for Account============================
Withdraw failed for :=> A-> Empty : 0
Withdraw failed for :=> A-> Tony : 0
Withdraw failed for :=> A-> Steve : 2000
Withdraw successful for :=> A-> Thor : 2000
===================================================
很明显,存款时余额已更新,但在提取时使用了默认余额。例如:在存入史蒂夫的余额为 3000 后,因此从史蒂夫那里提取 3000 应该可以正常工作,但对他而言提取失败表明他只有 2000,这是他的默认余额。我怎么解决这个问题?谢谢。
回答
问题是deposit
和withdraw
函数中的循环:
for (auto i: a)
这里i
将是vector 中元素的副本a
。所有修改(如调用i.deposit(bl)
)将仅在该副本上进行,而不是在向量中的元素上进行。
您需要使用对向量中元素的引用进行迭代:
// Note ampersand to make this a reference
// v
for (auto& i: a)