创建银行账户类SavingAccount,用静态成员变量存储年利率,用私有成员变量存储存款余额。提供计算年利息的方法和计算月利息(年利息/12)的方法。在主函数中建立SavingAccount的对象saver,从控制台输入存款额度和年利率,输出年利息和月利息。保留两位小数
时间: 2023-06-01 08:01:53 浏览: 143
设计一个类SavingsAccount 计算月息和本金和
4星 · 用户满意度95%
```
#include<iostream>
#include<iomanip>
using namespace std;
class SavingAccount{
private:
static double annualInterestRate;
double balance;
public:
SavingAccount(double b){
balance = b;
}
void calculateAnnualInterest(){
double interest = balance * annualInterestRate;
cout<<fixed<<setprecision(2)<<"Annual interest: "<<interest<<endl;
}
void calculateMonthlyInterest(){
double interest = balance * annualInterestRate / 12;
cout<<fixed<<setprecision(2)<<"Monthly interest: "<<interest<<endl;
}
static void modifyAnnualInterestRate(double rate){
annualInterestRate = rate;
}
};
double SavingAccount::annualInterestRate = 0.0;
int main(){
double balance, rate;
cout<<"Input balance: ";
cin>>balance;
cout<<"Input annual interest rate: ";
cin>>rate;
SavingAccount saver(balance);
SavingAccount::modifyAnnualInterestRate(rate);
saver.calculateAnnualInterest();
saver.calculateMonthlyInterest();
return 0;
}
```
输出样例:
```
Input balance: 1000
Input annual interest rate: 0.05
Annual interest: 50.00
Monthly interest: 4.17
```
阅读全文