用C++编写程序,定义一个FDAccount类,用于描述一个定期存折(fixeddeposit),实现现金支取、余额合计、信息显示、存款的增加功能等。存折基本信息包括账号、账户名称、存款余额、存款期限(以月为单位)、存款利率(以百分点为单位)等。
时间: 2024-11-09 17:24:57 浏览: 19
在C++中,我们可以定义一个`FDAccount`类来表示定期存折,包含如账号(account_number)、账户名称(account_name)、当前余额(balance)、存款期限(deposit_term)、利率(interest_rate)等属性。以下是这个类的一个简单实现:
```cpp
class FDAccount {
public:
// 构造函数,初始化基本属性
FDAccount(int account_num, const std::string& name, double initial_balance,
int deposit_term, double interest_rate)
: account_number(account_num), account_name(name),
balance(initial_balance), deposit_term(months_to_years(deposit_term)),
interest_rate(interest_rate / 100) {}
// 现金支取功能
void withdraw(double amount) {
if (amount > balance) {
throw std::runtime_error("Insufficient funds.");
}
balance -= amount;
}
// 余额合计
double totalBalance() const {
return balance + calculateInterest();
}
// 计算利息
double calculateInterest() const {
return balance * interest_rate * deposit_term;
}
// 信息显示
void displayInfo() const {
std::cout << "Account Number: " << account_number << "\n";
std::cout << "Account Name: " << account_name << "\n";
std::cout << "Current Balance: $" << balance << "\n";
std::cout << "Deposit Term (months): " << deposit_term << "\n";
std::cout << "Annual Interest Rate: " << interest_rate * 100 << "%\n";
}
// 存款增加
void deposit(double amount) {
balance += amount;
}
private:
// 将月数转换为年数(假设每年12个月)
static int months_to_years(int months) {
return months / 12;
}
int account_number;
std::string account_name;
double balance;
int deposit_term;
double interest_rate;
};
```
阅读全文