#include using namespace std; // Boat类定义 class Boat { private: double weight; // Boat的重量,使用double以支持小数 public: // Boat的构造函数 Boat(double w) : weight(w) {} // Boat的析构函数 ~Boat() { cout << "boat is destruction" << endl; } // 获取Boat的重量 double getWeight() const { return weight; } }; // Car类定义 class Car { private: double weight; // Car的重量,使用double以支持小数 public: // Car的构造函数 Car(double w) : weight(w) {} // Car的析构函数 ~Car() { cout << "car is destruction" << endl; } // 获取Car的重量 double getWeight() const { return weight; } }; // 友元函数,计算Boat和Car的重量和 double totalweight(const Boat& b, const Car& c) { return b.getWeight() + c.getWeight(); } int main() { double boatWeight, carWeight; // 从用户输入读取Boat和Car的重量 cin >> boatWeight >> carWeight; // 创建Boat和Car对象 Boat boat(boatWeight); Car car(carWeight); // 输出Boat和Car的重量 cout << "boat's weight:" << boat.getWeight() << endl; cout << "car's weight:" << car.getWeight() << endl; // 计算并输出总重量 cout.unsetf(ios::showpoint); cout << "total weight:" << totalweight(boat, car) << endl; // Boat和Car对象在main函数结束时自动析构 return 0; }