注意:
1、两个类要提前声明;
2、友元函数的定义和声明,参数是类的对象,要带引用。
3、看题目知要定义一个构造函数。
class boat;
class car;//类的声明
class boat
{
int weight;
friend int totalweight(boat &a,car &b);
public:
boat(int a)
{
weight=a;
}
};
class car
{
int weight;
friend int totalweight(boat &a,car &b);
public:
car(int a)
{
weight=a;
}
};
int totalweight(boat &a,car &b)
{
return a.weight+b.weight;
}