定义car和boat两个类,两者皆有weight属性,定义两者的一个友元函数sum(),计算两者的重量之差。
解析:
class car;
class boat
{
public:
friend float weight(boat &,car &);
boat(float w){weight = w;}
virtual ~boat();
protected:
float weight;
};
class car
{
public:
friend float weight(boat &,car &);
car(float w){weight = w;}
virtual ~car();
protected:
float weight;
};
float weight(boat &,car &) {
return boat.weight-car.weight;
}
Int main()
{
car c(11.1);
boat b(22.2);
float w;
cout<<weight(c,b)<<endl;
}