求救啊!!!!C++高手来啊!关于友元函数题。。

2024-12-30 15:40:29
推荐回答(1个)
回答1:

呵呵都是小问题楼主要细心呦~注意看注释
class Car;
class Boat
{
private:
double weight;
friend double totalweight(Boat&boat,Car&car);
public:
double goin1(double a)
{
weight=a;
return 1;
}

};
class Car
{
private:
double weight;
friend double totalweight(Boat&boat,Car&car);
public:
double goin2(double b)
{
weight=b;
return 1;
}

};
double totalweight(Boat &boat,Car &car)
{
return( boat.weight)+(car.weight);//注意这里用对象调用私有数据成员不是类!
}

int main()
{
double a,b,c;
cout<<"请输入Car,Boat重量"< cin>>a>>b;
Boat A;
Car B;
A.goin1(a);
B.goin2(b);
cout<<"总重量为:"<}