shushus

Спринт 11. Полиморфизм.

Mar 26th, 2023
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. shapes.cpp
  3. -----------------------------------------------------------------------
  4. #define _USE_MATH_DEFINES
  5. #include <algorithm>
  6. #include <cassert>
  7. #include <cmath>
  8. #include <iostream>
  9. #include <numeric>
  10. #include <string>
  11.  
  12. using namespace std;
  13.  
  14. enum class Color { RED, GREEN, BLUE };
  15.  
  16. ostream& operator<<(ostream& out, Color color) {
  17.     switch (color) {
  18.         case Color::RED:
  19.             out << "red"s;
  20.             break;
  21.         case Color::GREEN:
  22.             out << "green"s;
  23.             break;
  24.         case Color::BLUE:
  25.             out << "blue"s;
  26.             break;
  27.     }
  28.     return out;
  29. }
  30.  
  31. class Shape {
  32. public:
  33.     Shape(Color color) : color_(color) {}
  34.    
  35.     virtual Color GetColor() const {
  36.         return color_;
  37.     }
  38.    
  39.     virtual void SetColor(Color color) {
  40.         color_ = color;
  41.     }
  42.    
  43.     virtual double GetArea() const {
  44.         return area_;
  45.     }
  46.    
  47.     virtual std::string GetType() const {
  48.         return "Shape";
  49.     }
  50.    
  51. private:
  52.     Color color_;
  53.     double area_ = 0.0;
  54. };
  55.  
  56. class Rectangle : public Shape {
  57. public:
  58.     Rectangle(double width, double height, Color color) : Shape(color),
  59.                                                           width_(width),
  60.                                                           height_(height) {}
  61.     double GetWidth() const {
  62.         return width_;
  63.     }
  64.    
  65.     double GetHeight() const {
  66.         return height_;
  67.     }
  68.    
  69.     double GetArea() const override {
  70.         return width_ * height_;
  71.     }
  72.    
  73.     std::string GetType() const override {
  74.         return "Rectangle";
  75.     }
  76.  
  77.     void SetSize(double width, double height) {
  78.         width_ = width;
  79.         height_ = height;
  80.     }
  81.    
  82. private:
  83.     double width_;
  84.     double height_;
  85. };
  86.  
  87. class Circle : public Shape {
  88. public:
  89.     Circle(double radius, Color color) : Shape(color),
  90.                                          radius_(radius) {}
  91.    
  92.     double GetArea() const override {
  93.         return radius_ * radius_ * M_PI;
  94.     }
  95.    
  96.     string GetType() const override {
  97.         return "Circle";
  98.     }
  99.    
  100.     double GetRadius() const {
  101.         return radius_;
  102.     }
  103.  
  104.     void SetRadius(double radius) {
  105.         radius_ = radius;
  106.     }
  107.    
  108. private:
  109.     double radius_ = 0.0;
  110. };
  111.  
  112. template <typename ShapeCollection>
  113. double CalcSumArea(const ShapeCollection& collection) {
  114.     double sum = 0.0;
  115.     for (const Shape* shape : collection) {
  116.         sum += shape->GetArea();
  117.     }
  118.    
  119.     return sum;
  120. }
  121.  
  122. void PrintShapeInfo(const Shape& shape) {
  123.     cout << shape.GetType() << ": color: "s <<
  124.             shape.GetColor() << ", area:"s <<
  125.             shape.GetArea() << endl;
  126. }
  127.  
  128. int main() {
  129.     Circle c{10.0, Color::RED};
  130.     Rectangle r{10, 20, Color::BLUE};
  131.     Shape sh{Color::GREEN};
  132.  
  133.     const Shape* shapes[] = {&c, &r, &sh};
  134.  
  135.     assert(sh.GetType() == "Shape"s);
  136.     assert(c.GetType() == "Circle"s);
  137.     assert(r.GetType() == "Rectangle"s);
  138.  
  139.     assert(sh.GetColor() == Color::GREEN);
  140.     assert(c.GetColor() == Color::RED);
  141.     assert(r.GetColor() == Color::BLUE);
  142.     sh.SetColor(Color::BLUE);
  143.     c.SetColor(Color::GREEN);
  144.     r.SetColor(Color::RED);
  145.     assert(sh.GetColor() == Color::BLUE);
  146.     assert(c.GetColor() == Color::GREEN);
  147.     assert(r.GetColor() == Color::RED);
  148.  
  149.     assert(std::abs(r.GetArea() - 200.0) < 1e-5);
  150.     assert(std::abs(c.GetArea() - 314.15) < 1e-2);
  151.     c.SetRadius(1.0);
  152.     assert(std::abs(c.GetArea() - 3.1415) < 1e-4);
  153.     r.SetSize(5, 7);
  154.     assert(r.GetWidth() == 5);
  155.     assert(r.GetHeight() == 7);
  156.     assert(std::abs(r.GetArea() - 35.0) < 1e-5);
  157.  
  158.     assert(abs(CalcSumArea(shapes) - 38.1416) < 1e-4);
  159.  
  160.     for (const Shape* shape : shapes) {
  161.         PrintShapeInfo(*shape);
  162.     }
  163.     cout << "Total area: " << CalcSumArea(shapes) << endl;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment