Advertisement
AlexDanilin

Урок 7: Независимость заголовочных файлов 7.1

Jul 20th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. //Урок 7: Независимость заголовочных файлов 7.1
  2. -----------------------------------------------------------------------
  3. accountant.h
  4. -----------------------------------------------------------------------
  5. #pragma once
  6. #include "square_calculation.h"
  7. #include "wall.h"
  8.  
  9. class Accountant {
  10. public:
  11.     double CalcPaintNeeded(const Wall& wall) const {
  12.         double height = wall.GetHeight();
  13.         double width = wall.GetWidth();
  14.         return CalcSquare(width, height) * 0.4;
  15.     }
  16.  
  17.     double CalcBricksNeeded(const Wall& wall) const {
  18.         double height = wall.GetHeight();
  19.         double width = wall.GetWidth();
  20.         return CalcSquare(width, height) * 5;
  21.     }
  22. };
  23. -----------------------------------------------------------------------
  24. builder.h
  25. -----------------------------------------------------------------------
  26. #pragma once
  27.  
  28. class Builder {
  29. public:
  30.     void HoldDoor() const {
  31.         for (int i = 100; i != 0; --i) {
  32.             /* держит дверь */
  33.         }
  34.     }
  35. };
  36. -----------------------------------------------------------------------
  37. carpenter.cpp
  38. -----------------------------------------------------------------------
  39. #include "carpenter.h"
  40. #include "square_calculation.h"
  41.  
  42.  
  43. int Carpenter::CalcShelves(const Wall& wall) const {
  44.     double height = wall.GetHeight();
  45.     double width = wall.GetWidth();
  46.     return CalcSquare(width, height) / 2;
  47. }
  48.  
  49. void Carpenter::InstallDoor(Wall& wall, const Builder& builder) const {
  50.     // Hold the door, builder! Hold the door!
  51.     builder.HoldDoor();
  52.     wall.SetDoorInstalled();
  53. }
  54. -----------------------------------------------------------------------
  55. carpenter.h
  56. -----------------------------------------------------------------------
  57. #pragma once
  58. #include "builder.h"
  59. #include "wall.h"
  60.  
  61. class Carpenter {
  62. public:
  63.     int CalcShelves(const Wall& wall) const;
  64.     void InstallDoor(Wall& wall, const Builder& builder) const;
  65. };
  66. -----------------------------------------------------------------------
  67. main.cpp
  68. -----------------------------------------------------------------------
  69. #include <iostream>
  70.  
  71. #include "accountant.h"
  72. #include "builder.h"
  73. #include "carpenter.h"
  74. #include "painter.h"
  75.  
  76. using namespace std;
  77.  
  78. int main() {
  79.     Builder tom;
  80.     Painter bill;
  81.     Carpenter jack;
  82.     Accountant ray;
  83.     Wall wall(3.5, 2.45);
  84.  
  85.     cout << ray.CalcBricksNeeded(wall) << endl;
  86.     cout << ray.CalcPaintNeeded(wall) << endl;
  87.  
  88.     jack.InstallDoor(wall, tom);
  89.     cout << wall.IsDoorInstalled() << endl;
  90.     return 0;
  91. }
  92. -----------------------------------------------------------------------
  93. painter.h
  94. -----------------------------------------------------------------------
  95. #pragma once
  96. #include "wall.h"
  97.  
  98. class Painter {
  99. public:
  100.     void Paint(Wall& wall, Wall::Color color) const {
  101.         wall.SetColor(color);
  102.     }
  103. };
  104. -----------------------------------------------------------------------
  105. square_calculation.cpp
  106. -----------------------------------------------------------------------
  107. #include "square_calculation.h"
  108.  
  109. double CalcSquare(double width, double height) {
  110.     return width * height;
  111. }
  112. -----------------------------------------------------------------------
  113. square_calculation.h
  114. -----------------------------------------------------------------------
  115. #pragma once
  116. double CalcSquare(double width, double height);
  117. -----------------------------------------------------------------------
  118. wall.cpp
  119. -----------------------------------------------------------------------
  120. #include "wall.h"
  121.  
  122. Wall::Wall(double width, double height)
  123.     : width_(width)
  124.     , height_(height)
  125.     , color_(Color::WHITE)
  126.     , is_door_installed_(false) {
  127. }
  128.  
  129. double Wall::GetHeight() const {
  130.     return height_;
  131. }
  132. double Wall::GetWidth() const {
  133.     return width_;
  134. }
  135. void Wall::SetColor(Color color) {
  136.     color_ = color;
  137. }
  138. Wall::Color Wall::GetColor() const {
  139.     return color_;
  140. }
  141. bool Wall::IsDoorInstalled() const {
  142.     return is_door_installed_;
  143. }
  144.  
  145. void Wall::SetDoorInstalled() {
  146.     is_door_installed_ = true;
  147. }
  148. -----------------------------------------------------------------------
  149. wall.h
  150. -----------------------------------------------------------------------
  151. #pragma once
  152.  
  153. class Wall {
  154. public:
  155.     enum class Color { BLUE, GREEN, RED, WHITE, YELLOW };
  156.  
  157.     Wall(double width, double height);
  158.  
  159.     double GetHeight() const;
  160.     double GetWidth() const;
  161.     void SetColor(Color color);
  162.     Color GetColor() const;
  163.     bool IsDoorInstalled() const;
  164.     void SetDoorInstalled();
  165.  
  166. private:
  167.     double width_;
  168.     double height_;
  169.     Color color_;
  170.     bool is_door_installed_;
  171. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement