Advertisement
chevengur

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

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