请问这个存款程序的新余额显示不正确是什么原因??求解决
//时间:2016.4.8
#include <iostream>
#include<stdlib.h>
using namespace std;
class SavingAccount//定义一个名为SavingAccount的类
{
private:
double savingsBalance;//私有数据成员,用户的存款
public:
SavingAccount(double a);//声明构造函数原型
double calculateMothlyInterest();//声明非静态公有成员函数calculateMothlyInterest
static void ModifyInterestRate(double);//声明静态成员函数ModifyInterestRate
static double annualInterestRate;//定义静态数据成员annualInterestRate
double printsavingsbalance();//声明非静态公有成员函数printsavingsbalance
};
double SavingAccount::annualInterestRate = 0.03;//静态数据成员初始化
SavingAccount::SavingAccount(double a)//定义构造函数SavingAccount,作用是存储用户输入的本金
{
this->savingsBalance = a;
}
void SavingAccount::ModifyInterestRate(double b)//定义静态成员函数ModifyInterestRate,作用是存储利率
{
annualInterestRate = b;
}
double SavingAccount::calculateMothlyInterest()//定义非静态成员函数calculateMothlyInterest,作用是计算利息及统计新的余额
{
double interest = this->savingsBalance * this->annualInterestRate / 12;//计算利息并把值赋给interest
this->savingsBalance = this->savingsBalance+interest;//这里不是赋值了吗??!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
return interest;//返回interset的值
}
double SavingAccount::printsavingsbalance()//定义非静态成员函数printsavingsbalance,作用是返回savingsbalance的值
{
return this->savingsBalance;
}
int main()
{
SavingAccount person1(20000);//建立第一个SavingAccount对象person1
SavingAccount person2(30000);//建立第二个SavingAccount对象person2
SavingAccount::ModifyInterestRate(0.03);//调用静态成员函数ModifyInterestRate,给静态数据成员annualInterestRate赋初值
cout << "person1这个月的利息为:" << person1.calculateMothlyInterest() << "余额:" << person1.printsavingsbalance() << endl;
cout << "person2这个月的利息为:" << person2.calculateMothlyInterest() << "余额:" << person2.printsavingsbalance() << endl;
SavingAccount::ModifyInterestRate(0.04);//调用静态成员函数ModifyInterestRate,给静态数据成员annualInterestRate赋初值
cout << endl;
cout << "person1下个月的利息为:" << person1.calculateMothlyInterest() << "余额:" << person1.printsavingsbalance() << endl;
cout << "person2下个月的利息为:" << person2.calculateMothlyInterest() << "余额:" << person2.printsavingsbalance() << endl;
system("pause");
return 0;
}