avr39ripe

cppPolygonClassDemo

Aug 17th, 2021
833
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <math.h>
  5.  
  6. class Polygon
  7. {
  8. protected:
  9.     std::vector<float> sides;
  10. public:
  11.     Polygon& addSide(float side) { sides.push_back(side); return *this; }
  12.     Polygon& setSide(int sideId, float side)
  13.     {
  14.         sides[sideId] = side;
  15.         return *this;
  16.     }
  17.     float getSide(int sideId)const { return sides[sideId]; }
  18.  
  19.     virtual float perimeter() const
  20.     {
  21.         float per{ 0 };
  22.         for (const auto& side : sides) { per += side; }
  23.         return per;
  24.     }
  25.  
  26.     virtual float area()const = 0;
  27.  
  28.     const Polygon& sidesInfo() const
  29.     {
  30.         for (int i{ 0 }, maxSides{ (int)sides.size() }; i < maxSides; ++i)
  31.         {
  32.             std::cout << " Side " << char('A' + i) << " is " << sides[i] << " long\n";
  33.         }
  34.         return *this;
  35.     }
  36.  
  37.     std::string name() const
  38.     {
  39.         std::string nm{ typeid(*this).name() };
  40.         return nm.substr(5);
  41.     }
  42.  
  43.     const Polygon& info()
  44.     {
  45.         std::cout << name() << ":\n"
  46.             << " Perimeter: " << perimeter()
  47.             << " Area " << area() << '\n';
  48.         sidesInfo();
  49.         std::cout << '\n';
  50.  
  51.         return *this;
  52.     }
  53. };
  54.  
  55. class Rectangle : public Polygon
  56. {
  57. public:
  58.     Rectangle(float sideA, float sideB) { addSide(sideA); addSide(sideB); }
  59.     virtual float perimeter()const override { return Polygon::perimeter() * 2; }
  60.     virtual float area()const override { return getSide(0) * getSide(1); }
  61. };
  62.  
  63. class Square : public Rectangle
  64. {
  65. public:
  66.     Square(float sideA) : Rectangle{ sideA, sideA } {};
  67. };
  68.  
  69. class Triangle : public Polygon
  70. {
  71. public:
  72.     Triangle(float sideA, float sideB, float sideC)
  73.     {
  74.         addSide(sideA).addSide(sideB).addSide(sideC);
  75.     }
  76.  
  77.     virtual float area()const override
  78.     {
  79.         const float p{ perimeter() / 2 };
  80.         return sqrt(p * (p - getSide(0)) * (p - getSide(1)) * (p - getSide(2)));
  81.     }
  82. };
  83.  
  84. int main()
  85. {
  86.     //Polygon* polygons[]{ new Rectangle{4,5}, new Square{6}, new Triangle{3,4,5} };
  87.  
  88.     Rectangle rectangle{ 4,5 };
  89.     Square square{ 6 };
  90.     Triangle triangle{ 3,4,5 };
  91.  
  92.     Polygon* polygons[]{ &rectangle, &square,  &triangle};
  93.  
  94.     for (const auto& polygon : polygons)
  95.     {
  96.         polygon->info();
  97.         //delete polygon;
  98.     }
  99.  
  100.     return 0;
  101. }
  102.  
RAW Paste Data