Advertisement
kutuzzzov

Урок 4 Полиморфизм

Apr 7th, 2023
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. // Макрос _USE_MATH_DEFINES необходим, чтобы при подключении <cmath> была объявлена константа M_PI
  2. #define _USE_MATH_DEFINES
  3. // Макрос _USE_MATH_DEFINES следует объявить ДО подключения других заголовочных файлов,
  4. // которые могут подключить <cmath> неявно
  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.     Color color_;
  33.  
  34. public:
  35.     explicit Shape(const Color color) : color_(color){};
  36.     //explicit Shape(double area, const Color color) : color_(color), area_(area) {}
  37.  
  38.     virtual double GetArea() const {
  39.         return 0.0;
  40.     }
  41.  
  42.     virtual std::string GetType() const {
  43.         return "Shape";
  44.     }
  45.  
  46.     virtual Color GetColor() const {
  47.         return color_;
  48.     }
  49.  
  50.     virtual void SetColor(Color color) {
  51.         color_ = color;
  52.     }
  53. };
  54.  
  55. class Rectangle : public Shape {
  56.     double width_;
  57.     double height_;
  58.  
  59. public:
  60.     explicit Rectangle(const double width, const double height, Color color)
  61.         : Shape(color)
  62.         , width_(width)
  63.         , height_(height) {}
  64.  
  65.     virtual double GetArea() const override {
  66.         return width_ * height_;
  67.     }
  68.  
  69.     virtual std::string GetType() const override {
  70.         return "Rectangle";
  71.     }
  72.  
  73.     virtual Color GetColor() const override {
  74.         return Shape::GetColor();
  75.     }
  76.  
  77.     int GetWidth() const {
  78.         return width_;
  79.     }
  80.  
  81.     int GetHeight() const {
  82.         return height_;
  83.     }
  84.  
  85.     virtual void SetColor(Color color) override {
  86.         Shape::SetColor(color);
  87.     }
  88.  
  89.     void SetSize(int width, int height) {
  90.         width_ = width;
  91.         height_ = height;
  92.     }
  93. };
  94.  
  95. class Circle : public Shape {
  96.     double radius_;
  97.  
  98. public:
  99.     explicit Circle(const double radius, const Color color)
  100.         : Shape(color)
  101.         , radius_(radius) {}
  102.  
  103.     virtual double GetArea() const override {
  104.         return radius_ * radius_ * M_PI;
  105.     }
  106.  
  107.     virtual std::string GetType() const override {
  108.         return "Circle";
  109.     }
  110.  
  111.     virtual Color GetColor() const override {
  112.         return Shape::GetColor();
  113.     }
  114.  
  115.     virtual void SetColor(Color color) override {
  116.         Shape::SetColor(color);
  117.     }
  118.  
  119.     inline void SetRadius(double radius) {
  120.         radius_ = radius;
  121.     }
  122.  
  123.     double GetRadius() const {
  124.         return radius_;
  125.     }
  126. };
  127.  
  128. // Возвращает суммарную площадь фигур, указатели на которые находятся в переданной коллекции collection
  129. template <typename ShapeCollection>
  130. double CalcSumArea(const ShapeCollection& collection) {
  131.     double sum = 0.0;
  132.     for (const Shape* shape : collection) {
  133.         sum += shape->GetArea();
  134.     }
  135.     return sum;
  136. }
  137.  
  138. void PrintShapeInfo(const Shape& shape) {
  139.     cout << shape.GetType() << ": color: "s << shape.GetColor() << ", area:"s << shape.GetArea() << endl;
  140. }
  141.  
  142. int main() {
  143.     Circle c{10.0, Color::RED};
  144.     Rectangle r{10, 20, Color::BLUE};
  145.     Shape sh{Color::GREEN};
  146.  
  147.     const Shape* shapes[] = {&c, &r, &sh};
  148.  
  149.     assert(sh.GetType() == "Shape"s);
  150.     assert(c.GetType() == "Circle"s);
  151.     assert(r.GetType() == "Rectangle"s);
  152.  
  153.     assert(sh.GetColor() == Color::GREEN);
  154.     assert(c.GetColor() == Color::RED);
  155.     assert(r.GetColor() == Color::BLUE);
  156.     sh.SetColor(Color::BLUE);
  157.     c.SetColor(Color::GREEN);
  158.     r.SetColor(Color::RED);
  159.     assert(sh.GetColor() == Color::BLUE);
  160.     assert(c.GetColor() == Color::GREEN);
  161.     assert(r.GetColor() == Color::RED);
  162.  
  163.     assert(std::abs(r.GetArea() - 200.0) < 1e-5);
  164.     assert(std::abs(c.GetArea() - 314.15) < 1e-2);
  165.     c.SetRadius(1.0);
  166.     assert(std::abs(c.GetArea() - 3.1415) < 1e-4);
  167.     r.SetSize(5, 7);
  168.     assert(r.GetWidth() == 5);
  169.     assert(r.GetHeight() == 7);
  170.     assert(std::abs(r.GetArea() - 35.0) < 1e-5);
  171.  
  172.     assert(abs(CalcSumArea(shapes) - 38.1416) < 1e-4);
  173.  
  174.     for (const Shape* shape : shapes) {
  175.         PrintShapeInfo(*shape);
  176.     }
  177.     cout << "Total area: " << CalcSumArea(shapes) << endl;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement