1.定义一个FDAcount类,用于描述一个定期存折(fiked deposi), 实现现金支取、余额合计、信息显示等。存折基本信息包含账易,账户名称。存款余额、存款期限(以月为单位)、存款利率 (以百分点为单位)等。用c++
时间: 2024-12-12 10:25:17 浏览: 8
在C++中,我们可以创建一个名为`FDAcount`的类来表示定期存折,包括相关的属性和函数。以下是这个类的一个基础实现:
```cpp
#include <iostream>
#include <string>
class FDAcount {
public:
// 构造函数,初始化基本信息
FDAcount(const std::string& account_name, double initial_balance,
int deposit_term_months, double interest_rate)
: account_name(account_name), current_balance(initial_balance),
deposit_term(deposit_term_months), annual_interest_rate(interest_rate) {}
// 现金支取函数
void withdraw_cash(double amount) {
if (amount <= current_balance) {
current_balance -= amount;
std::cout << "Withdrawal successful! New balance: $" << current_balance << std::endl;
} else {
std::cerr << "Insufficient funds!" << std::endl;
}
}
// 余额合计
double calculate_total_balance() const {
return current_balance + total_interest();
}
// 计算总利息
double total_interest() const {
double monthly_interest = annual_interest_rate / 100.0 / 12.0;
return current_balance * monthly_interest * deposit_term;
}
// 信息显示
void display_info() const {
std::cout << "Account Name: " << account_name << "\n";
std::cout << "Current Balance: $" << current_balance << "\n";
std::cout << "Deposit Term (months): " << deposit_term << "\n";
std::cout << "Annual Interest Rate: " << annual_interest_rate << "%\n";
}
private:
std::string account_name;
double current_balance;
int deposit_term;
double annual_interest_rate; // 转换成每月利率
};
int main() {
FDAcount my_deposit("Alice's Savings", 5000.0, 12, 2.5);
my_deposit.display_info();
my_deposit.withdraw_cash(2000.0);
my_deposit.display_info();
std::cout << "Total Balance including interest: $" << my_deposit.calculate_total_balance() << std::endl;
return 0;
}
```
在这个例子中,`FDAcount`类包含了构造函数、提取现金方法、计算总余额方法以及显示账户信息的方法。`main`函数展示了如何创建一个`FDAcount`对象并操作它。
阅读全文