Advertisement
chevengur

СПРИНТ № 5 | Распределение кода по файлам | Урок 6: Проблема двойного включения

Mar 11th, 2024
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. wall.h
  2.  
  3. #pragma once
  4. class Wall {
  5. public:
  6.     enum class Color { BLUE, GREEN, RED, WHITE, YELLOW };
  7.  
  8.     Wall(double width, double height);
  9.  
  10.     double GetHeight() const;
  11.     double GetWidth() const;
  12.     void SetColor(Color color);
  13.     Color GetColor() const;
  14.  
  15. private:
  16.     double width_;
  17.     double height_;
  18.     Color color_;
  19. };
  20. ***************************************************************************************************************************************
  21.  
  22. wall.cpp
  23.  
  24. #include "wall.h"
  25.  
  26. Wall::Wall(double width, double height)
  27.     : width_(width)
  28.     , height_(height)
  29.     , color_(Color::WHITE) {
  30. }
  31.  
  32. double Wall::GetHeight() const {
  33.     return height_;
  34. }
  35. double Wall::GetWidth() const {
  36.     return width_;
  37. }
  38. void Wall::SetColor(Color color) {
  39.     color_ = color;
  40. }
  41. Wall::Color Wall::GetColor() const {
  42.     return color_;
  43. }
  44. ***************************************************************************************************************************************
  45.  
  46. square.calculation.h
  47.  
  48. #pragma once
  49. double CalcSquare(double width, double height);
  50. ***************************************************************************************************************************************
  51.  
  52. square.calculation.cpp
  53.  
  54. #include "square_calculation.h"
  55.  
  56. double CalcSquare(double width, double height) {
  57.     return width * height;
  58. }
  59. ***************************************************************************************************************************************
  60.  
  61. painter.h
  62.  
  63. #pragma once
  64. #include "square_calculation.h"
  65. #include "wall.h"
  66.  
  67. class Painter {
  68. public:
  69.     void Paint(Wall& wall, Wall::Color color) const {
  70.         wall.SetColor(color);
  71.     }
  72.     double CalcPaintNeeded(const Wall& wall) const {
  73.         double height = wall.GetHeight();
  74.         double width = wall.GetWidth();
  75.         return CalcSquare(width, height) * 0.4;
  76.     }
  77. };
  78. ***************************************************************************************************************************************
  79.  
  80. main.cpp
  81.  
  82. #include <iostream>
  83.  
  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.     Wall wall(3.5, 2.45);
  95.  
  96.     cout << tom.CalcBricksNeeded(wall) << endl;
  97.     cout << bill.CalcPaintNeeded(wall) << endl;
  98.     cout << jack.CalcShelves(wall) << endl;
  99.     return 0;
  100. }
  101. ***************************************************************************************************************************************
  102.  
  103. carpenter.h
  104.  
  105. #pragma once
  106. #include "wall.h"
  107.  
  108. class Carpenter {
  109. public:
  110.     int CalcShelves(const Wall& wall) const;
  111. };
  112. ***************************************************************************************************************************************
  113.  
  114. carpenter.cpp
  115.  
  116. #include "carpenter.h"
  117.  
  118. #include "square_calculation.h"
  119.  
  120. int Carpenter::CalcShelves(const Wall& wall) const {
  121.     double height = wall.GetHeight();
  122.     double width = wall.GetWidth();
  123.     return CalcSquare(width, height) / 2;
  124. }
  125. ***************************************************************************************************************************************
  126.  
  127. builder.h
  128.  
  129. #pragma once
  130. #include "square_calculation.h"
  131. #include "wall.h"
  132.  
  133. class Builder {
  134. public:
  135.     double CalcBricksNeeded(const Wall& wall) const {
  136.         double height = wall.GetHeight();
  137.         double width = wall.GetWidth();
  138.         return CalcSquare(width, height) * 5;
  139.     }
  140. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement