2020春c++计算存款利息
2021-02-20 18:18
标签:gac double balance flag space save pre 数据 保存 创建一个SavingAccount类. 使用一个static数据成员annualInterestRate保存每个存款者的年利率。类的每个对象都包含一个private数据成员savingBalance,用以指示存款者目前的存款金额。该类提供成员函数calculateMonthlyInterest,它将余额乘以annualInterestRate再除以12来计算月利息,这个利息应该加到savingBalance中。该类还提供一个static成员函数modifyInterestRate,它将static的annualInterestRate设置为一个新值。在主函数中,实例化SavingAccount类的两个对象saver1和saver2,余额分别是2000和3000,将annualInterestRate设置为3%,然后计算月利率并打印每个存款者的新余额,接着再将annualInterestRate设置为4%,计算下一个月的利息并打印每个存款者的新余额。 class SavingsAccount private: int main(){ cout saver1.printBalance(); cout saver1.printBalance(); cout saver1.printBalance(); 2020春c++计算存款利息 标签:gac double balance flag space save pre 数据 保存 原文地址:https://www.cnblogs.com/zhaoyiyao/p/12681324.html
#include "stdafx.h"
#include
#include
#include
using namespace std;
{
public:
SavingsAccount(double save):savingBalance(save){}
double calculateMonthlyInterest();
void printBalance();
static double modifyInterestRate(double rate);
static double annualInterestRate;
double savingBalance;
};
double SavingsAccount::annualInterestRate=0;
double SavingsAccount::modifyInterestRate(double rate){
annualInterestRate=rate;
return annualInterestRate;
}
double SavingsAccount::calculateMonthlyInterest(){
savingBalance+=savingBalance*annualInterestRate/12;
return savingBalance;
}
void SavingsAccount::printBalance(){
cout}
SavingsAccount saver1(2000.0);
SavingsAccount saver2(3000.0);
SavingsAccount::modifyInterestRate(0.03);
cout saver2.printBalance();
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
cout saver2.printBalance();
SavingsAccount::modifyInterestRate(0.04);
//change interest rate
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
cout saver2.printBalance();
cout getchar();
return 0;
}