Advertisement
chevengur

СПРИНТ № 5 | Распределение кода по файлам | Урок 7: Независимость заголовочных файлов 2/2

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