#chap5_14
定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数getTotalWeight(),计算二者的重量和
#include <iostream>
using namespace std;
class Car;
class Boat {
public:
friend void getTotalWeight(Boat& b, Car& c);
int height;
private:
int weight=10;
};
class Car {
public:
friend void getTotalWeight(Boat& b, Car& c);
private:
int weight=20;
};
void getTotalWeight(Boat& b, Car& c) {
cout << "Boat's weight:" << b.weight << endl;
cout << "Car's weight:" << c.weight << endl;
cout << "Total weight is:" << b.weight + c.weight << endl;
}
int main() {
Boat b;
Car c;
getTotalWeight(b, c);
return 0;
}
运行结果: