设计一个程序,其中有三个类,CBank,BBank,GBank.

设计一个程序,其中有三个类,CBank,BBank,GBank. 分别为中国银行类,工商银行类,农业银行类。每个类都包含一个私有数据balance,用于存放储户在该行的存款数,另有一个友元函数total,用于计算储户在这3家银行中的总存款数。

#include  
using namespace std;
class CBank          //中国银行类
{
private:
	float balance;
public:

	CBank(float balance = 0)
	{
		this->balance = balance;
	}
	friend class user;
};

class BBank   //工商银行类
{
private:
	float balance;
public:

	BBank(float balance = 0)
	{
		this->balance = balance;
	}
	friend class user;
};

class GBank       //农业银行类
{
private:
	float balance;
public:

	GBank(float balance = 0)
	{
		this->balance = balance;
	}

	friend class user;
};

class user
{
private:
	CBank C;
	BBank B;
	GBank G;
public:
	user(CBank c, BBank b, GBank g)
	{
		C = c;
		B = b;
		G = g;
	}
	void total()
	{
		cout << C.balance + B.balance + G.balance << endl;
	}
};
int main()
{
	CBank c(100.1);
	BBank b(100.2);
	GBank g(100.3);
	user u(c, b, g);
	u.total();
}

上一篇:对setw(x)函数的理解!

你可能感兴趣的:(C++,题)