#include <iostream>
using namespace std;
class car;
class boat
{
public:
boat(int weight)
{
this->weight = weight;
}
friend int getTotalWeight(boat &b,car &c);
private:
int weight;
};
class car
{
public:
car(int weight)
{
this->weight = weight;
}
friend int getTotalWeight(boat &b,car &c);
private:
int weight;
};
int getTotalWeight(boat &b,car &c)
{
return b.weight+c.weight;
}
int main()
{
boat b(3);
car c(2);
cout<<"总重量是"<<getTotalWeight(b,c)<<endl;
return 0;
}
定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数getTotalWeight, 计算二者的重量之和
最新推荐文章于 2023-12-19 14:16:28 发布