C++ 银行账号类设计与实现:活期存款账户
以下是实现代码:
#include <iostream>
using namespace std;
// 表示银行账号的类
class BankAccount {
private:
int accountNumber; // 账号号码
double balance; // 余额
double rate; // 利率
static int nextAccountNumber; // 静态成员变量,用于生成新的账号号码
// 私有静态成员函数,用于生成新账号号码
static int generateAccountNumber() {
return nextAccountNumber++;
}
public:
// 构造函数
BankAccount(double initialBalance = 0, double initialRate = 0) : balance(initialBalance), rate(initialRate) {
accountNumber = generateAccountNumber();
}
// 返回余额
double getBalance() const {
return balance;
}
// 返回利率
double getRate() const {
return rate;
}
// 修改余额
void setBalance(double newBalance) {
balance = newBalance;
cout << "余额已修改为: " << balance << endl;
}
// 修改利率
void setRate(double newRate) {
rate = newRate;
cout << "利率已修改为: " << rate << endl;
}
// 打印账号信息
void show() const {
cout << "账号号码: " << accountNumber << endl;
cout << "余额: " << balance << endl;
cout << "利率: " << rate << endl;
}
};
// 初始化静态成员变量
int BankAccount::nextAccountNumber = 1;
// 表示活期存款账号的类
class CheckingAccount : public BankAccount {
private:
double charge; // 积数
int lastTransactionTime; // 上一次存取款时间
public:
// 构造函数
CheckingAccount(double initialBalance = 0, double initialRate = 0) : BankAccount(initialBalance, initialRate), charge(0), lastTransactionTime(0) {}
// 更新积数
void updateCharge(int currentTime) {
if (currentTime > lastTransactionTime) {
double previousBalance = getBalance(); // 获取上一次存款余额
charge += previousBalance * getRate() * (currentTime - lastTransactionTime) / 365; // 计算并更新积数
lastTransactionTime = currentTime; // 更新时间
cout << "积数已更新为: " << charge << endl;
} else {
cout << "交易时间无效!无法更新积数。" << endl;
}
}
// 取款函数
bool withdraw(double amount, int currentTime) {
if (currentTime > lastTransactionTime && amount <= getBalance()) {
setBalance(getBalance() - amount); // 修改余额
updateCharge(currentTime); // 更新积数
cout << "取款成功!" << endl;
return true;
} else {
cout << "取款失败!" << endl;
return false;
}
}
// 存款函数
bool deposit(double amount, int currentTime) {
if (currentTime > lastTransactionTime) {
setBalance(getBalance() + amount); // 修改余额
updateCharge(currentTime); // 更新积数
cout << "存款成功!" << endl;
return true;
} else {
cout << "存款失败!" << endl;
return false;
}
}
// 计算利息
double calculateInterest() const {
return charge; // 使用积数计算利息
}
// 输出账户信息
void show() const {
BankAccount::show(); // 输出父类信息
cout << "积数: " << charge << endl;
cout << "上一次存取款时间: " << 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;
}
原文地址: https://www.cveoy.top/t/topic/osjE 著作权归作者所有。请勿转载和采集!