Advertisement
kutuzzzov

Урок 2 Отношения между классами

Apr 6th, 2023
1,152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <cassert>
  2. #include <string>
  3. #include <stdexcept>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. class House {
  9.     // Реализуйте самостоятельно
  10. public:
  11.     explicit House(int length, int width, int height) : length_(length), width_(width), height_(height) {}
  12.     int GetLength() const {
  13.         return length_;
  14.     }
  15.     int GetWidth() const {
  16.         return width_;
  17.     }
  18.     int GetHeight() const {
  19.         return height_;
  20.     }
  21. private:
  22.     int length_;
  23.     int width_;
  24.     int height_;
  25. };
  26.  
  27. class Resources {
  28.     // Реализуйте самостоятельно
  29. public:
  30.     explicit Resources(int brick_count) : brick_count_(brick_count) {}
  31.     void TakeBricks(int count) {
  32.         if (count < 0 || brick_count_ - count < 0) {
  33.             throw out_of_range("Num of bricks < 0, or > brick resources");
  34.         }
  35.         brick_count_ -= count;
  36.     }
  37.     int GetBrickCount() const {
  38.         return brick_count_;
  39.     }
  40. private:
  41.     int brick_count_ = 0;
  42. };
  43.  
  44. struct HouseSpecification {
  45.     int length = 0;
  46.     int width = 0;
  47.     int height = 0;
  48. };
  49.  
  50. class Builder {
  51.     // Реализуйте самостоятельно
  52. public:
  53.     explicit Builder(Resources& resources) : resources_(resources) {}
  54.     House BuildHouse(HouseSpecification spec) {
  55.         int brick_needs = (spec.length + spec.width) * 2 * spec.height * 32;
  56.         if (resources_.GetBrickCount() < brick_needs) {
  57.             throw runtime_error("Brick needs higher than resources");
  58.         }
  59.         resources_.TakeBricks(brick_needs);
  60.         return House{spec.length, spec.width, spec.height};
  61.     }
  62. private:
  63.     Resources& resources_;
  64. };
  65.  
  66. int main() {
  67.     Resources resources{10000};
  68.     Builder builder1{resources};
  69.     Builder builder2{resources};
  70.  
  71.     House house1 = builder1.BuildHouse(HouseSpecification{12, 9, 3});
  72.     assert(house1.GetLength() == 12);
  73.     assert(house1.GetWidth() == 9);
  74.     assert(house1.GetHeight() == 3);
  75.     cout << resources.GetBrickCount() << " bricks left"s << endl;
  76.  
  77.     House house2 = builder2.BuildHouse(HouseSpecification{8, 6, 3});
  78.     assert(house2.GetLength() == 8);
  79.     cout << resources.GetBrickCount() << " bricks left"s << endl;
  80.  
  81.     House banya = builder1.BuildHouse(HouseSpecification{4, 3, 2});
  82.     assert(banya.GetHeight() == 2);
  83.     cout << resources.GetBrickCount() << " bricks left"s << endl;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement