Advertisement
chevengur

СПРИНТ № 5 | Распределение кода по файлам | Урок 8: Шаблоны в многофайловых проектах

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