实现在一个类中写私有的成员变量,并包括相应的成员函数:一个返回初始余额;一个返回到期时的余额;一个返回利率;另一个返回存期。写一个构造将所有成员变量设为任何指定的值,同时包括一个默认构造函数。
其具体实现代码如下:
#include<iostream>
using namespace std;
class CDAccount
{
public:
CDAccount();
CDAccount(double blce1, double rate, int term);
double getBanlance1();
double getBanlance2();
double getRate();
int getTerm();
private:
double balance;
double interestRate;
int term;
};
CDAccount::CDAccount()
{
balance = 0;
interestRate = 0;
term = 0;
}
CDAccount::CDAccount(double blce1, double rate, int term1)
{
balance = blce1;
interestRate = rate;
term = term1;
}
double CDAccount::getBanlance1()
{
return balance;
}
double CDAccount::getRate()
{
return interestRate;
}
int CDAccount::getTerm()
{
return term;
}
double CDAccount::getBanlance2()
{
balance = getBanlance1() + getBanlance1()*getRate()*getTerm();
return balance;
}
int main()
{
CDAccount account1(1,1,10);
double blce11, rate;
int term;
cout << "请输入初始余额:" << account1.getBanlance1() << endl;
cout << "请输入返回利率:" << account1.getRate() << endl;
cout << "请输入返回存期:" << account1.getTerm() << endl;
cout << "到期时的余额为:" << account1.getBanlance2() << endl;
return 0;
}