定义boat和car两个类,二者都有weight属性,定义二者的一个友元函数gettotalweight(),计算二者的重量和。
#include <iostream>
using namespace std;
class boat;
class car{
private:
int weight;
public:
car(int j){
weight=j;
}
friend int gettotalweight(car&acar,boat&aboat);
};
class boat{
private:
int weight;
public:
boat(int j){
weight=j;
}
friend int gettotalweight(car&acar,boat&aboat);
};
int gettotalweight(car&acar,boat&aboat){
return acar.weight+aboat.weight;
}
int main(){
car cl(4);
boat bl(5);
cout<<gettotalweight(cl,bl)<<endl;
return 0;
}