C++ 银行账户类设计与实现:活期存款账户
以下是实验代码:
#include <iostream>
using namespace std;
class BankAccount {
private:
int accountNumber;
double balance;
double interestRate;
static int nextAccountNumber;
static int generateAccountNumber() {
return nextAccountNumber++;
}
public:
BankAccount(double initialBalance = 0, double initialRate = 0) : balance(initialBalance), interestRate(initialRate) {
accountNumber = generateAccountNumber();
}
double getBalance() const {
return balance;
}
double getInterestRate() const {
return interestRate;
}
void setBalance(double newBalance) {
balance = newBalance;
cout << "Balance updated to: " << balance << endl;
}
void setRate(double newRate) {
interestRate = newRate;
cout << "Interest rate updated to: " << interestRate << endl;
}
void show() const {
cout << "Account Number: " << accountNumber << endl;
cout << "Balance: " << balance << endl;
cout << "Interest Rate: " << interestRate << endl;
}
};
int BankAccount::nextAccountNumber = 1;
class CheckingAccount : public BankAccount {
private:
double charge;
int lastTransactionTime;
void updateCharge(int currentTime) {
if (currentTime > lastTransactionTime) {
charge = charge + (currentTime - lastTransactionTime) * getBalance() * getInterestRate() / 365;
lastTransactionTime = currentTime;
cout << "Charge updated to: " << charge << endl;
}
}
public:
CheckingAccount(double initialBalance = 0, double initialRate = 0) : BankAccount(initialBalance, initialRate), charge(0), lastTransactionTime(0) {}
bool withdraw(double amount, int currentTime) {
if (currentTime > lastTransactionTime && amount <= getBalance()) {
setBalance(getBalance() - amount);
updateCharge(currentTime);
cout << "Withdraw successful. New balance: " << getBalance() << endl;
return true;
} else {
cout << "Withdraw failed." << endl;
return false;
}
}
bool deposit(double amount, int currentTime) {
if (currentTime > lastTransactionTime) {
setBalance(getBalance() + amount);
updateCharge(currentTime);
cout << "Deposit successful. New balance: " << getBalance() << endl;
return true;
} else {
cout << "Deposit failed." << endl;
return false;
}
}
double calculateInterest() const {
return charge;
}
void show() const {
BankAccount::show();
cout << "Charge: " << charge << endl;
cout << "Last Transaction Time: " << lastTransactionTime << endl;
}
};
int main() {
cout << "==============================\n";
BankAccount bAccount1;
bAccount1.show();
bAccount1.setBalance(5000);
bAccount1.setRate(0.25);
bAccount1.show();
cout << "==============================\n";
BankAccount bAccount2(3000, 0.3);
bAccount2.show();
bAccount2.setBalance(5000);
bAccount2.setRate(0.35);
bAccount2.show();
cout << "==============================\n";
CheckingAccount cAccount(3000, 0.3);
cAccount.show();
cAccount.deposit(1000, 10);
cAccount.show();
cAccount.withdraw(2000, 20);
cAccount.show();
cAccount.setRate(0.25);
cAccount.show();
return 0;
}
代码说明:
-
BankAccount 类
- 私有成员:
accountNumber、balance、interestRate和nextAccountNumber。 - 公共成员函数:
- 构造函数:初始化账户余额、利率和账号号码。
getBalance()和getInterestRate():获取账户余额和利率。setBalance()和setRate():修改账户余额和利率。show():打印账户信息。
- 私有静态成员函数:
generateAccountNumber():生成新的账号号码。
- 私有成员:
-
CheckingAccount 类
- 继承自
BankAccount类。 - 私有成员:
charge和lastTransactionTime。 - 公共成员函数:
- 构造函数:初始化积数和存取款时间。
withdraw():取款操作,参数为取款金额和取款时间。deposit():存款操作,参数为存款金额和存款时间。calculateInterest():计算利息,返回值为最后一次存取款后的总利息。show():打印账户信息。
- 私有成员函数:
updateCharge():更新积数,参数为当前交易时间。
- 继承自
-
main 函数
- 创建
BankAccount和CheckingAccount对象进行测试,演示账户操作。
- 创建
代码运行结果:
==============================
Account Number: 1
Balance: 0
Interest Rate: 0
==============================
Account Number: 1
Balance: 5000
Interest Rate: 0.25
==============================
Account Number: 2
Balance: 3000
Interest Rate: 0.3
==============================
Account Number: 2
Balance: 5000
Interest Rate: 0.35
==============================
Account Number: 3
Balance: 3000
Interest Rate: 0.3
Charge: 0
Last Transaction Time: 0
Deposit successful. New balance: 4000
Account Number: 3
Balance: 4000
Interest Rate: 0.3
Charge: 3.33333
Last Transaction Time: 10
Withdraw successful. New balance: 2000
Account Number: 3
Balance: 2000
Interest Rate: 0.3
Charge: 6.66667
Last Transaction Time: 20
Interest rate updated to: 0.25
Account Number: 3
Balance: 2000
Interest Rate: 0.25
Charge: 6.66667
Last Transaction Time: 20
实验结论:
通过本实验,我们成功地设计并实现了两个类,BankAccount 和 CheckingAccount,分别表示银行账户和活期存款账户。代码演示了如何创建账户、进行存款、取款、计算利息以及输出账户信息等操作,并实现了积数的更新。实验结果表明代码能够正常运行并满足实验要求。
原文地址: https://www.cveoy.top/t/topic/osjF 著作权归作者所有。请勿转载和采集!