Advertisement
plarmi

workcpp_7_2

Jun 23rd, 2023
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Airplane {
  4. private:
  5.     std::string type;
  6.     int passengers;
  7.  
  8. public:
  9.     Airplane(const std::string& t, int p) : type(t), passengers(p) {}
  10.  
  11.     // Перегруженный оператор ==
  12.     bool operator==(const Airplane& other) const {
  13.         return type == other.type;
  14.     }
  15.  
  16.     // Перегруженный оператор ++
  17.     Airplane& operator++() {
  18.         ++passengers;
  19.         return *this;
  20.     }
  21.  
  22.     // Перегруженный оператор --
  23.     Airplane& operator--() {
  24.         if (passengers > 0) {
  25.             --passengers;
  26.         }
  27.         return *this;
  28.     }
  29.  
  30.     // Перегруженный оператор >
  31.     bool operator>(const Airplane& other) const {
  32.         return passengers > other.passengers;
  33.     }
  34.  
  35.     // Геттер для количества пассажиров
  36.     int getPassengers() const {
  37.         return passengers;
  38.     }
  39. };
  40.  
  41. int main() {
  42.     Airplane a1("Boeing 747", 300);
  43.     Airplane a2("Airbus A380", 500);
  44.     Airplane a3("Boeing 747", 400);
  45.  
  46.     // Проверка на равенство типов самолетов (операция ==)
  47.     if (a1 == a2) {
  48.         std::cout << "a1 and a2 are of the same type." << std::endl;
  49.     } else {
  50.         std::cout << "a1 and a2 are of different types." << std::endl;
  51.     }
  52.  
  53.     if (a1 == a3) {
  54.         std::cout << "a1 and a3 are of the same type." << std::endl;
  55.     } else {
  56.         std::cout << "a1 and a3 are of different types." << std::endl;
  57.     }
  58.  
  59.     // Увеличение и уменьшение пассажиров в салоне самолета (операции ++ и --)
  60.     ++a1;  // Увеличение пассажиров на 1
  61.     --a2;  // Уменьшение пассажиров на 1
  62.  
  63.     std::cout << "New number of passengers in a1: " << a1.getPassengers() << std::endl;
  64.     std::cout << "New number of passengers in a2: " << a2.getPassengers() << std::endl;
  65.  
  66.     // Сравнение двух самолетов по максимально возможному количеству пассажиров на борту (операция >)
  67.     if (a1 > a2) {
  68.         std::cout << "a1 can accommodate more passengers than a2." << std::endl;
  69.     } else {
  70.         std::cout << "a1 cannot accommodate more passengers than a2." << std::endl;
  71.     }
  72.  
  73.     return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement