【id:57】【20分】B. 银行账户(静态成员与友元函数)

时间限制
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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谁的BUG最难改

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值