shushus

Спринт 5. Шаблоны в многофайловых проектах.

Dec 12th, 2022
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.76 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. accountant.cpp
  3. -----------------------------------------------------------------------
  4.  
  5. empty
  6.  
  7. -----------------------------------------------------------------------
  8. accountant.h
  9. -----------------------------------------------------------------------
  10. #pragma once
  11. #include <utility>
  12. #include "wall.h"
  13. #include "square_calculation.h"
  14.  
  15. using namespace std;
  16.  
  17. class Accountant {
  18. public:
  19.     template <class BuildingObject>
  20.     double CalcPaintNeeded(const BuildingObject& building_object) const{
  21.         pair<double, double> sizes = building_object.GetSizes();
  22.         return CalcSquare(sizes.first, sizes.second) * 0.4;
  23.     }
  24.     template <class BuildingObject>
  25.     double CalcBricksNeeded(const BuildingObject& building_object) const{
  26.         pair<double, double> sizes = building_object.GetSizes();
  27.         return CalcSquare(sizes.first, sizes.second) * 5;
  28.     }
  29. };
  30. -----------------------------------------------------------------------
  31. builder.h
  32. -----------------------------------------------------------------------
  33. #pragma once
  34. #include "square_calculation.h"
  35.  
  36. class Builder {
  37. public:
  38.     void HoldDoor() const {
  39.         // Просто держит дверь 100 условных секунд
  40.         int i = 100;
  41.         while (0 != i) {
  42.             i--;
  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. carpenter.h
  66. -----------------------------------------------------------------------
  67. #pragma once
  68. #include "builder.h"
  69. #include "wall.h"
  70.  
  71. class Carpenter {
  72. public:
  73.     int CalcShelves(const Wall& wall) const;
  74.     void InstallDoor(Wall& wall, const Builder& builder) const;
  75. };
  76. -----------------------------------------------------------------------
  77. ceiling.cpp
  78. -----------------------------------------------------------------------
  79. #include "ceiling.h"
  80.  
  81. Ceiling::Ceiling(double length, double width)
  82.     : length_(length)
  83.     , width_(width) {
  84. }
  85.  
  86. std::pair<double, double> Ceiling::GetSizes() const {
  87.     return {length_, width_};
  88. }
  89. -----------------------------------------------------------------------
  90. ceiling.h
  91. -----------------------------------------------------------------------
  92. #pragma once
  93. #include <utility>
  94.  
  95. class Ceiling {
  96. public:
  97.     Ceiling(double length, double width);
  98.     std::pair<double, double> GetSizes() const;
  99.  
  100. private:
  101.     double length_;
  102.     double width_;
  103. };
  104. -----------------------------------------------------------------------
  105. main.cpp
  106. -----------------------------------------------------------------------
  107. #include <iostream>
  108.  
  109. #include "accountant.h"
  110. #include "ceiling.h"
  111. #include "roof.h"
  112.  
  113. using namespace std;
  114.  
  115. int main() {
  116.     Accountant ray;
  117.     Wall wall(3.5, 2.45);
  118.     Roof roof(5, 7);
  119.     Ceiling ceiling(5, 7);
  120.  
  121.     cout << "Требуется кирпичей: "s
  122.          << ray.CalcBricksNeeded<Wall>(wall) + ray.CalcBricksNeeded<Roof>(roof)
  123.             + ray.CalcBricksNeeded<Ceiling>(ceiling)
  124.          << endl;
  125.  
  126.     cout << "Требуется краски: "s
  127.          << ray.CalcPaintNeeded<Wall>(wall) + ray.CalcPaintNeeded<Roof>(roof)
  128.             + ray.CalcPaintNeeded<Ceiling>(ceiling)
  129.          << endl;
  130.     return 0;
  131. }
  132.  
  133. -----------------------------------------------------------------------
  134. painter.h
  135. -----------------------------------------------------------------------
  136. #pragma once
  137. #include "square_calculation.h"
  138. #include "wall.h"
  139.  
  140. class Painter {
  141. public:
  142.     void Paint(Wall& wall, Wall::Color color) const {
  143.         wall.SetColor(color);
  144.     }
  145. };
  146. -----------------------------------------------------------------------
  147. roof.cpp
  148. -----------------------------------------------------------------------
  149. #include "roof.h"
  150.  
  151. Roof::Roof(double length, double width)
  152.     : length_(length)
  153.     , width_(width) {
  154. }
  155.  
  156. std::pair<double, double> Roof::GetSizes() const {
  157.     return {length_, width_};
  158. }
  159. -----------------------------------------------------------------------
  160. roof.h
  161. -----------------------------------------------------------------------
  162. #pragma once
  163. #include <utility>
  164.  
  165. class Roof {
  166. public:
  167.     Roof(double length, double width);
  168.     std::pair<double, double> GetSizes() const;
  169.  
  170. private:
  171.     double length_;
  172.     double width_;
  173. };
  174. -----------------------------------------------------------------------
  175. square_calculation.cpp
  176. -----------------------------------------------------------------------
  177. #include "square_calculation.h"
  178.  
  179. double CalcSquare(double width, double height) {
  180.     return width * height;
  181. }
  182. -----------------------------------------------------------------------
  183. square_calculation.h
  184. -----------------------------------------------------------------------
  185. #pragma once
  186. double CalcSquare(double width, double height);
  187. -----------------------------------------------------------------------
  188. wall.cpp
  189. -----------------------------------------------------------------------
  190. #include "wall.h"
  191.  
  192. Wall::Wall(double width, double height)
  193.     : width_(width)
  194.     , height_(height)
  195.     , color_(Color::WHITE)
  196.     , is_door_installed_(false) {
  197. }
  198.  
  199. double Wall::GetHeight() const {
  200.     return height_;
  201. }
  202. double Wall::GetWidth() const {
  203.     return width_;
  204. }
  205. std::pair<double, double> Wall::GetSizes() const {
  206.     return {width_, height_};
  207. }
  208. void Wall::SetColor(Color color) {
  209.     color_ = color;
  210. }
  211. Wall::Color Wall::GetColor() const {
  212.     return color_;
  213. }
  214. bool Wall::IsDoorInstalled() const {
  215.     return is_door_installed_;
  216. }
  217. void Wall::SetDoorInstalled() {
  218.     is_door_installed_ = true;
  219. }
  220. -----------------------------------------------------------------------
  221. wall.h
  222. -----------------------------------------------------------------------
  223. #pragma once
  224. #include <utility>
  225.  
  226. class Wall {
  227. public:
  228.     enum class Color { BLUE, GREEN, RED, WHITE, YELLOW };
  229.  
  230.     Wall(double width, double height);
  231.  
  232.     double GetHeight() const;
  233.     double GetWidth() const;
  234.     std::pair<double, double> GetSizes() const;
  235.     void SetColor(Color color);
  236.     Color GetColor() const;
  237.     bool IsDoorInstalled() const;
  238.     void SetDoorInstalled();
  239.  
  240. private:
  241.     double width_;
  242.     double height_;
  243.     Color color_;
  244.     bool is_door_installed_;
  245. };
Advertisement
Add Comment
Please, Sign In to add comment