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

aa

#include <iostream>
#include <string>
using namespace std;

class Account {
	friend void update(Account& a);
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){}

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

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

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

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

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

void update(Account& a) {
	a.count += a.balance * a.interestRate;
	a.balance += a.balance * a.interestRate;
	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 = new Account*[n]; 

	for (int i = 0; i < n; i++) {
		string no, na;
		float ba, cun, qu;
		cin >> no >> na >> ba >> cun >> qu;

		accounts[i] = new Account(no, na, ba); 
		accounts[i]->show();
		accounts[i]->deposit(cun);
		update(*accounts[i]);
		accounts[i]->withdraw(qu);

		cout << endl;
	}

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

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

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值