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

题目描述

银行账户类的基本描述如下:

class Account
{private:
    staticfloat count; // 账户总余额staticfloat 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行开始,每行输出一个账户的相关信息,包括账号、姓名、存款后的余额、存款后结息余额、取款后余额。

最后一行输出所有账户的余额。

样例查看模式

正常显示查看格式

输入样例1 <-复制

输出样例1

语言: 编译选项

主题:

#include<iostream>
using namespace std;
class Account
{
private:
    static float count; // 账户总余额
    static float interestRate;
    string accno, accname;
    float balance;
public:
    Account() {};
    Account(string ac, string na, float ba);
    ~Account(){};
    void set(string ac, string na, float ba);
    void deposit(float amount); // 存款
    void withdraw(float amount); // 取款
    float getBalance(); // 获取账户余额
    void show(); // 显示账户所以基本信息
    static float getCount(); // 获取账户总余额
    static void setInterestRate(float rate); // 设置利率
    static void setcount(int co);
    static float getcount();
    static float getInterestRate(); // 获取利率
    friend void update(Account& a);
    void print()
    {
        cout << accno << " ";
        cout << accname << " ";
    }
    void print1()
    {
        cout << balance;
    }
};
//静态变量类内声明类外初始化
float Account::count = 0;
float Account::interestRate = 0;
void Account::setcount(int co)
{
    count += co;
}
float Account::getcount()
{
    return count;
}
Account::Account(string ac, string na, float ba)
{
    accno = ac;
    accname = na;
    balance = ba;
}
void Account::set(string ac, string na, float ba)
{
    accno = ac;
    accname = na;
    balance = ba;
}
void Account::deposit(float amount)
{
    balance += amount;
}
void Account::withdraw(float amount)
{
    balance -= amount;
}
float Account::getBalance()
{
    return balance;
}
float Account::getCount()
{
    return count;
}
void Account::setInterestRate(float rate)
{
    interestRate = rate;
}
float Account::getInterestRate()
{
    return interestRate;
}
//实现账户结息,要求输出结息后的余额(结息余额=账户余额+账户余额*利率)
void update(Account& a)
{
    a.balance += (a.balance * a.getInterestRate());
    cout << a.balance;
}
int main()
{
    //义一个Account类型的指针数组,让每个指针指向动态分配的Account对象
    //,并调用成员函数测试存款、取款、显示等函数,
    //再调用友元函数测试进行结息。
    //第一行设置利率
    float rate;
    cin >> rate;
    Account::setInterestRate(rate);
    int t;
    cin >> t;
    Account* a = new Account[t];
    string name, num;
    float bal, jia, jian;
    float count = 0;
    for (int i = 0; i < t; i++)
    {
        cin >> num;
        cin >> name;
        cin >> bal;
        cin >> jia;
        cin >> jian;
        a[i].set(num, name, bal);
        a[i].print();
        a[i].deposit(jia);
        a[i].print1();
        cout << " ";
        update(a[i]);
        cout << " ";
        a[i].withdraw(jian);
        a[i].print1();
        cout << endl;
        count += a[i].getBalance();
    }
    cout << count;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值