1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #include <iostream>
- using namespace std;
- //Car类的定义与实现
- class Boat;
- class Car {
- private:
- int weight;
-
- public:
- Car(int w) : weight(w) {}
-
- friend int getTotalWeight(Car& aCar, Boat& aBoat);
- };
- //Boat的定义与实现
- class Boat {
- private:
- int weight;
-
- public:
- Boat(int w) : weight(w) {}
- friend int getTotalWeight(Car &aCar, Boat &aBoat);
- };
- //友元函数getTotalWeight()
- int getTotalWeight(Car& aCar, Boat& aBoat) {
- return aCar.weight + aBoat.weight;
- }
- int main()
- {
- int carWeight, boatWeight;
- cout << "卡车重量:";
- cin >> carWeight;
- cout << "轮船重量:"<<endl;
- cin >> boatWeight;
-
- Car myCar(carWeight);
- Boat myBoat(boatWeight);
-
- cout << "卡车和轮船的总重量为:" << getTotalWeight(myCar, myBoat) << endl;
-
- return 0;
- }
|