evcamels

lab-10_1-variant

Nov 18th, 2021
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. class Room
  6.   {
  7.   friend ostream& operator<<(ostream&, const Room&);
  8.  
  9.   public:
  10.     Room() {}
  11.     Room(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 Flat
  29.   {
  30.   friend ostream& operator<<(ostream&, const Flat&);
  31.  
  32.   public:
  33.     Flat(int level) : _level(level) {}
  34.  
  35.     Room* add_room(Room* room)
  36.       {
  37.       _rooms.push_back(room);
  38.       return _rooms.back();
  39.       }
  40.    
  41.     Room* add_room(int width, int height, int length)
  42.       {
  43.       _rooms.push_back(new Room(width, height, length));
  44.       return _rooms.back();
  45.       }
  46.  
  47.     void remove_room(Room *room)
  48.       {
  49.       _rooms.erase(remove(_rooms.begin(), _rooms.end(), room),
  50.           _rooms.end());
  51.       }
  52.  
  53.     int rooms_count() const { return _rooms.size(); }
  54.  
  55.   private:
  56.     int _level;
  57.     vector<Room*> _rooms;
  58.   };
  59.  
  60. ostream& operator<<(ostream& out, const Room& room)
  61.   {
  62.     return out << "ширина: "  << room.width()
  63.                << " высота: " << room.height()
  64.                << " длина: " << room.length();
  65.   }
  66.  
  67. ostream& operator<<(ostream& out, const Flat& flat)
  68.   {
  69.     out << "Этаж квартиры:  " << flat._level << std::endl;
  70.     out << "Комнаты в квартире: " << endl;
  71.  
  72.     vector<Room*>::const_iterator cit = flat._rooms.begin();
  73.     for ( ; cit != flat._rooms.end(); ++cit)
  74.       out << **cit << std::endl;
  75.  
  76.     return out;
  77.   }
  78.  
  79. int main()
  80.   {
  81.   setlocale(LC_ALL, "rus");
  82.      
  83.   vector<Room*> other_rooms;
  84.   other_rooms.push_back(new Room(10, 10, 10));
  85.  
  86.   Flat flat(2);
  87.  
  88.   Room *room = flat.add_room(new Room(20, 20, 20));
  89.   other_rooms.push_back(room);
  90.  
  91.   flat.add_room(other_rooms[0]);
  92.  
  93.   cout << "Информация о кв: " << endl << flat << std::endl;
  94.  
  95.   return 0;
  96.   }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment