时间限制
1s
内存限制
128MB
题目描述
银行账户类的基本描述如下:
class Account
{
private:
static float count; // 账户总余额
static float interestRate;
string accno, accname;
float balance;
public:
Account(string ac, string na, float ba);
~Account();
void deposit(float amount); // 存款
void withdraw(float amount); // 取款
float getBalance(); // 获取账户余额
void show(); // 显示账户所有基本信息
static float getCount(); // 获取账户总余额
static void setInterestRate(float rate); // 设置利率
static float getInterestRate(); // 获取利率
};
要求如下:
实现该银行账户类
为账户类Account增加一个友元函数,实现账户结息,要求输出结息后的余额(结息余额=账户余额+账户余额*利率)。友元函数声明形式为 friend void update(Account& a);
在main函数中,定义一个Account类型的指针数组,让每个指针指向动态分配的Account对象,并调用成员函数测试存款、取款、显示等函数,再调用友元函数测试进行结息。
大家可以根据实际需求在类内添加其他方法,也可以修改成员函数的参数设置
输入
第1行:利率
第2行:账户数目n
第3行开始,每行依次输入一个账户的“账号”、“姓名”、“余额”、存款数,取款数。
输出
第1行开始,每行输出一个账户的相关信息,包括账号、姓名、存款后的余额、存款后结息余额、取款后余额。
最后一行输出所有账户的余额。
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
class Account
{
private:
static float count; // 账户总余额
static float interestRate;
string accno, accname;
float balance, balance1, balance2;
public:
Account(string ac, string na, float ba)
{
accno = ac;
accname = na;
balance = ba;
}
~Account()
{
}
void deposit(float amount) // 存款
{
balance1 = balance + amount;
}
void withdraw(float amount) // 取款
{
balance2 = balance1 + balance1 * interestRate - amount;
}
float sum()
{
return balance2;
}
float getBalance() // 获取账户余额
{
cin >> count;
return count;
}
void show() // 显示账户所有基本信息
{
cout << accno << " " << accname<<" ";
cout << balance1 << " " << balance1 + balance1 * interestRate << " " << balance2 << endl;
}
static float getCount() // 获取账户总余额
{
return count;
}
static void setInterestRate(float rate)// 设置利率
{
interestRate = rate;
}
static float getInterestRate()// 获取利率
{
cout << interestRate;
}
friend void update(Account& a);//友元函数申明;
};
float Account::interestRate = 0;
float Account::count = 0;
void update(Account& a)
{
cout << a.balance1 + a.balance1 * a.interestRate;
}
int main()
{
float rate;
cin >> rate;
int t;
float sum = 0;
cin >> t;
while (t--)
{
string ac, na;
float ba;
cin >> ac >> na >> ba;
Account acount(ac, na, ba);
acount.setInterestRate(rate);
float in, out;
cin >> in >> out;
acount.deposit(in);
acount.withdraw(out);
acount.show();
float left = acount.sum();
sum += left;
}
cout << sum << endl;
}