[求助]友元函数
定义BOAT与CAR两个类,两者都有WEIGHT属性,定义两者的一个友元函数TOTALWEIGHT(),计算两者的重量和。这个友元函数应该怎么编写,希望提供源程序。
// forward declaration
class BOAT;
class CAR;
int TOTALWEIGHT(BOAT boat, CAR car);
class BOAT
{
public:
BOAT():WEIGHT(0){}
//......
private:
int WEIGHT;
friend int TOTALWEIGHT(BOAT boat, CAR car);
};
class CAR
{
public:
CAR():WEIGHT(0){}
//......
private:
int WEIGHT;
friend int TOTALWEIGHT(BOAT boat, CAR car);
};
int TOTALWEIGHT(BOAT boat, CAR car)
{
return boat.WEIGHT + car.WEIGHT;
}
极其简单的语法问题, 闲来无聊就帮你写了