#include<iostream>
using namespace std;
class car;
class boat{
public:
boat(int w=10):weight(w){}
friend int totalweight(boat b,car c);
private:
int weight;
};
class car{
public:
car(int w=20):weight(w){}
friend int totalweight(boat b,car c);
private:
int weight;
};
int totalweight(boat b,car c){
return (b.weight+c.weight);
}
int main(){
car c;
boat b;
cout<<totalweight(b,c);
return 0;
}
【无标题】定义一boat与car两个类,二者都有weight属性,定义二者的一个友元函数totalweight(),计算二者的重量和。
于 2022-04-16 22:04:42 首次发布