定义猫和老鼠:Cat与Mouse两个类,二者都有weight属性,定义二者的一个友元函数totalweight(),计算二者的重量和。
class Mouse;
class Cat
{
int weight;
public :
Cat(int m)
{
weight=m;
}
friend int totalWeight(Cat t1,Mouse j1);
};
class Mouse
{
int weight;
public:
Mouse(int m)
{
weight=m;
}
friend int totalWeight(Cat t1,Mouse j1);
};
int totalWeight(Cat t1,Mouse j1)
{
return t1.weight+j1.weight;
}