Advertisement
evcamels

lab_8-13

Dec 12th, 2021
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. class Helicopter
  6.   {
  7.   friend std::ostream& operator<<(std::ostream&, const Helicopter&);
  8.  
  9.   public:
  10.       Helicopter() {}
  11.       Helicopter(int width, int height, int length, int windows = 0)
  12.       : _width(width), _height(height), _length(length), _windows(windows) {}
  13.  
  14.     int width()   const { return _width; }
  15.     int height()  const { return _height; }
  16.     int length()  const { return _length; }
  17.     int windows() const { return _windows; }
  18.  
  19.     int square()  const { return _width * _length; }
  20.  
  21.   private:
  22.     int _width;
  23.     int _height;
  24.     int _length;
  25.     int _windows;
  26.   };
  27.  
  28. class Airport
  29.   {
  30.   friend std::ostream& operator<<(std::ostream&, const Airport&);
  31.  
  32.   public:
  33.       Airport(int level) : _level(level) {}
  34.  
  35.       Helicopter* add_helicopter(Helicopter* helicopter)
  36.       {
  37.         _helicopter.push_back(helicopter);
  38.       return _helicopter.back();
  39.       }
  40.    
  41.       Helicopter* add_helicopter(int width, int height, int length)
  42.       {
  43.       _helicopter.push_back(new Helicopter(width, height, length));
  44.       return _helicopter.back();
  45.       }
  46.  
  47.     void remove_helicopter(Helicopter *room)
  48.       {
  49.       remove_if(_helicopter.begin(), _helicopter.end(),
  50.         [&room](Helicopter *it){ return room == it; });
  51.       }
  52.  
  53.     int helicopter_count() const { return _helicopter.size(); }
  54.  
  55.   private:
  56.     int _level;
  57.     vector<Helicopter*> _helicopter;
  58.   };
  59.  
  60. ostream& operator<<(ostream& out, const Helicopter& helicopter)
  61.   {
  62.     return out << "Scale's"
  63.                << " width: "  << helicopter.width()
  64.                << " height: " << helicopter.height()
  65.                << " length: " << helicopter.length();
  66.   }
  67.  
  68. ostream& operator<<(ostream& out, const Airport& airport)
  69.   {
  70.     out << "Helicopter level " << airport._level << endl;
  71.     out << "Contains cabin: " << endl;
  72.  
  73.     for_each(airport._helicopter.begin(), airport._helicopter.end(),
  74.       [&out](Helicopter *it)
  75.         {
  76.         out << *it << endl;
  77.         });
  78.  
  79.     return out;
  80.   }
  81.  
  82. int main()
  83.   {
  84.   vector<Helicopter*> other_helicopter;
  85.   other_helicopter.push_back(new Helicopter(10, 10, 10));
  86.  
  87.       Airport airport(2);
  88.  
  89.       Helicopter *helicopter = airport.add_helicopter(new Helicopter(20, 20, 20));
  90.   other_helicopter.push_back(helicopter);
  91.  
  92.   airport.add_helicopter(other_helicopter[0]);
  93.  
  94.   cout << "My new helicopter is " << airport << endl;
  95.   return 0;
  96.   }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement