船和卡车的重量计算——友元函数——11-02.cpp 979 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <iostream>
  2. using namespace std;
  3. class car;
  4. class boat {
  5. private:
  6. int weight;
  7. public:
  8. boat(int w) : weight(w) {}
  9. friend int totalweight(const boat& b, const car& c);
  10. void display() const {
  11. cout << "boat's weight:" << weight << endl;
  12. }
  13. ~boat() {
  14. cout << "boat is destruction" << endl;
  15. }
  16. };
  17. class car {
  18. private:
  19. int weight;
  20. public:
  21. car(int w) : weight(w) {}
  22. friend int totalweight(const boat& b, const car& c);
  23. void display() const {
  24. cout << "car's weight:" << weight << endl;
  25. }
  26. ~car() {
  27. cout << "car is destruction" << endl;
  28. }
  29. };
  30. int totalweight(const boat& b, const car& c) {
  31. b.display();
  32. c.display();
  33. cout << "total weight:" << b.weight + c.weight << endl;
  34. return b.weight + c.weight;
  35. }
  36. int main() {
  37. int boat_weight, car_weight;
  38. cin >> boat_weight >> car_weight;
  39. boat b(boat_weight);
  40. car c(car_weight);
  41. totalweight(b, c);
  42. return 0;
  43. }