【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 <string>
using namespace std;

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(); // 显示账户所有基本信息
    friend void update(Account& a); // 友元函数:结息
    static float getCount(); // 获取账户总余额
    static void setInterestRate(float rate); // 设置利率
    static float getInterestRate(); // 获取利率
};

float Account::count = 0;
float Account::interestRate = 0;

Account::Account(string ac, string na, float ba) {
    accno = ac;
    accname = na;
    balance = ba;
    count += ba;
}

Account::~Account() {
    count -= balance;
}

void Account::deposit(float amount) {
    balance += amount;
    count += amount;
    cout<<" "<<balance; 
}

void Account::withdraw(float amount) {
    if (balance >= amount) {
        balance -= amount;
        count -= amount;
        cout<<" "<<balance; 
    }
    else {
        cout << "Insufficient balance" << endl;
    }
}

float Account::getBalance() {
    return balance;
}

void Account::show() {
    cout << accno << " " <<  accname;
}

void update(Account& a) {
    float interest = a.balance * Account::interestRate;
    a.balance += interest;
    Account::count += interest;
    cout << " " << a.balance;
}

float Account::getCount() {
    return count;
}

void Account::setInterestRate(float rate) {
    interestRate = rate;
}

float Account::getInterestRate() {
    return interestRate;
}

int main() {
    float rate;
    cin >> rate;
    Account::setInterestRate(rate);

    int n;
    cin >> n;

    Account* accounts[n]; // 定义Account类型的指针数组
    for (int i = 0; i < n; i++) {
        string ac, na;
        float ba, de, wi;
        cin >> ac >> na >> ba >> de >> wi;

        accounts[i] = new Account(ac, na, ba); // 动态分配Account对象,并将指针存入数组
 		accounts[i]->show();
        accounts[i]->deposit(de);
        update(*accounts[i]);
        accounts[i]->withdraw(wi);
        
        cout << endl;
    }

    cout << Account::getCount() << endl;

    for (int i = 0; i < n; i++) {
        delete accounts[i]; // 释放动态分配的内存
    }

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

谁的BUG最难改

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

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

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

打赏作者

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

抵扣说明:

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

余额充值