08-E. 银行账户(静态成员与友元函数)

这篇博客介绍了如何使用C++实现一个银行账户类,包括存款、取款、显示账户信息等功能,并利用友元函数实现账户结息。通过一个示例展示了如何在main函数中操作账户数组,动态分配内存并调用各类方法,最后输出所有账户的余额。博客还涵盖了静态成员变量和友元函数的概念及其应用。

08-静态成员与友元
题目描述
银行账户类的基本描述如下:
在这里插入图片描述
要求如下:

实现该银行账户类

为账户类Account增加一个友元函数,实现账户结息,要求输出结息后的余额(结息余额=账户余额+账户余额*利率)。友元函数声明形式为 friend void Update(Account& a);

在main函数中,定义一个Account类型的指针数组,让每个指针指向动态分配的Account对象,并调用成员函数测试存款、取款、显示等函数,再调用友元函数测试进行结息。

大家可以根据实际需求在类内添加其他方法,也可以修改成员函数的参数设置

输入
第1行:利率
第2行:账户数目n
第3行开始,每行依次输入一个账户的“账号”、“姓名”、“余额”、存款数,取款数。

输出
第1行开始,每行输出一个账户的相关信息,包括账号、姓名、存款后的余额、存款后结息余额、取款后余额。

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

输入样例
0.01
3
201501 张三 10000 1000 2000
201502 李四 20000 2000 4000
201503 王二 80000 4000 6000

201501 张三 11000 11110 9110
201502 李四 22000 22220 18220
201503 王二 84000 84840 78840
106170

#include <iostream>
using namespace std;

class account
{
    static int count;//账户数量
    static float rate;//利率
    string accno,name;//账户/户名
    float balance;//余额
public:
    void setaccount(string a,string n,float b)
    {accno=a;name=n;balance=b;count++;}//账户数量增加
    ~account(){};
    void deposit(float amount)
    {balance+=amount;}
    void withdraw(float amount)
    {balance-=amount;}
    float getbalance()
    {return balance;}
    
    friend void update(account &a)//实现账户利息
    {
        a.balance=a.balance+a.balance*a.rate;
    }
    void show()
    {cout<<accno<<" "<<name<<" ";}
    
    static int getcount();//获取账户数
    static float getrate();//获得利率
    static void setrent(float r);//设定利率
};

int account::count=0;
float account::rate=0;

void account::setrent(float r)
{rate=r;count=0;}
float account::getrate()
{return rate;}
int account::getcount()
{return count;}

int main()
{
    float rate;
    int n,i;
    cin>>rate>>n;
    account::setrent(rate);
    
    string accno,name;
    float balance,deposit,withdraw,sum=0;
    account a[n];
    for(i=0;i<n;i++)
    {
        cin>>accno>>name>>balance>>deposit>>withdraw;
        a[i].setaccount(accno, name, balance);
        
        a[i].show();
        a[i].deposit(deposit); cout<<a[i].getbalance()<<" ";
        update(a[i]); cout<<a[i].getbalance()<<" ";
        a[i].withdraw(withdraw); cout<<a[i].getbalance()<<endl;
        sum+=a[i].getbalance();
    }
    cout<<sum<<endl;
    return 0;
}
c
好的,我知道了,不会泄漏您的信息。以下是银行账户的设计,实现了封装、继承、友元、静态成员变量和函数: ```cpp #include <iostream> using namespace std; class Account { // 基 protected: string name; int balance; int number; public: void deposit(int amount) { balance += amount; } virtual void withdraw(int amount) = 0; // 纯虚函数 virtual void display() { cout << "Number: " << number << endl; cout << "Name: " << name << endl; cout << "Balance: " << balance << endl; } friend class ATM; // 将 ATM 设为友元 }; class CheckingAccount : public Account { // 储蓄账户,继承自 Account private: static double fee; public: CheckingAccount(string n, int b) { name = n; balance = b; number = rand() % 10000; } void withdraw(int amount) { if (balance >= amount + fee) { balance -= (amount + fee); } else { cout << "Insufficient balance!" << endl; } } void display() { Account::display(); cout << "Checking Account Fee: " << fee << endl; } static void setFee(double f) { fee = f; } // 静态函数 }; double CheckingAccount::fee = 5.0; // 静态成员变量的初始化 class SavingsAccount : public Account { // 存款账户,继承自 Account private: int rate; public: SavingsAccount(string n, int b, int r) { name = n; balance = b; rate = r; number = rand() % 10000; } void withdraw(int amount) { if (balance >= amount) { balance -= amount; } else { cout << "Insufficient balance!" << endl; } } void display() { Account::display(); cout << "Savings Account Rate: " << rate << "%" << endl; } static void compoundInterest(SavingsAccount& sa) { // 静态函数 double interest = sa.balance * sa.rate / 100.0; sa.balance += interest; } }; class ATM { // 友元 public: void withdraw(Account& acc, int amount) { acc.withdraw(amount); } }; int main() { CheckingAccount::setFee(10.0); // 使用静态函数设置所有储蓄账户的手续费 Account* acc[3]; acc[0] = new CheckingAccount("Tom", 2000); acc[1] = new SavingsAccount("Jerry", 3000, 5); acc[2] = new CheckingAccount("Alice", 1000); for (int i = 0; i < 3; i++) { acc[i]->display(); cout << endl; } SavingsAccount::compoundInterest(*(SavingsAccount*)acc[1]); // 强制型转换,调用静态函数计算利息 cout << "After compound interest: " << endl; for (int i = 0; i < 3; i++) { acc[i]->display(); cout << endl; } ATM atm; atm.withdraw(*acc[0], 500); // 取款 atm.withdraw(*acc[1], 2000); // 取款 cout << "After withdraw: " << endl; for (int i = 0; i < 3; i++) { acc[i]->display(); cout << endl; } return 0; } ``` 以上代码演示了一个银行账户管理的例子,基为 Account,其中包括了存款和取款操作,及展示账户信息的 display 函数。储蓄账户 CheckingAccount 继承自 Account,多加了一个手续费 fee,不足以支付手续费时,取款操作将失败。存款账户 SavingsAccount 同理,多包括了一个利率 rate。ATM 用于实现取款操作,需要将 Account 设为友元,避免取款操作需要知道私有成员的信息。当然,ATM 技术已经很老,这里只是为了演示的友元。 设计模式上,以上代码主要应用了模板方法模式,封装了储蓄和取款操作,便于扩展新的账户型。同时,利用了静态成员变量和函数,使得储蓄账户的手续费费率同步控制,利率定时计算。友元是典型的抽象工厂模式的应用,可以方便地扩展其他银行卡和业务功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值