Advertisement
avr39ripe

PV913PolygonClassDemoAdvanced

Jul 28th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <math.h>
  5.  
  6.  
  7.  
  8. class Polygon
  9. {
  10. protected:
  11.     std::vector<float> sides;
  12.    
  13. public:
  14.     Polygon& addSide(float size) { sides.push_back(size); return *this; };
  15.     Polygon& setSide(int sideId, float size)
  16.     {
  17.         sides[sideId] = size; return *this;
  18.     };
  19.     float getSide(int sideId)const { return sides[sideId]; };
  20.     virtual float perimeter()const
  21.     {
  22.         float per{ 0 };
  23.         for (const auto & side : sides)
  24.         {
  25.             per += side;
  26.         }
  27.         return per;
  28.     };
  29.     virtual float area()const;
  30.  
  31.     const Polygon& sidesInfo()const
  32.     {
  33.         for (int i{ 0 }; i < sides.size(); ++i)
  34.         {
  35.             std::cout << " Side " << char('A' + i) << " is " << sides[i] << " long\n";
  36.         }
  37.         return *this;
  38.     }
  39.  
  40.     std::string name()const
  41.     {
  42.         std::string nm{ typeid(*this).name() }; //"class Polygon"
  43.         return nm.substr(5);
  44.     }
  45.  
  46.     const Polygon& info()const
  47.     {
  48.         std::cout << name() << ":\n"
  49.             << " Perimeter: " << perimeter()
  50.             << " Area: " << area() << '\n';
  51.         sidesInfo();
  52.         std::cout << '\n';
  53.  
  54.         return *this;
  55.     }
  56.  
  57. };
  58.  
  59. float Polygon::area()const
  60. {
  61.     return -1;
  62. }
  63.  
  64. class Rectangle : public Polygon
  65. {
  66. public:
  67.     Rectangle(float sideA, float sideB) { addSide(sideA); addSide(sideB); };
  68.     virtual float perimeter()const override{ return Polygon::perimeter() * 2; };
  69.     virtual float area()const  override { return getSide(0) * getSide(1); }
  70. };
  71.  
  72. class Square : public Rectangle
  73. {
  74. public:
  75.     Square(float sideA) : Rectangle{ sideA, sideA } {};
  76. };
  77.  
  78. class Triangle : public Polygon
  79. {
  80. public:
  81.     Triangle(float sideA, float sideB, float sideC)
  82.     {
  83.         addSide(sideA).addSide(sideB).addSide(sideC);
  84.     };
  85.     virtual float area()const override
  86.     {
  87.         const float p{ perimeter() / 2 };
  88.         return sqrt(p * (p - getSide(0)) * (p - getSide(1)) * (p - getSide(2)));
  89.     }
  90. };
  91.  
  92. class Parent
  93. {
  94.     int val;
  95. public:
  96.     Parent(int valP) : val{ valP }
  97.     {
  98.         std::cout << "Parent constructed! for " << this << '\n';
  99.     };
  100.     Parent() : Parent{ 0 } {};
  101.     int getVal()const { return val; };
  102.     virtual const Parent& info()const  { std::cout << "Parent with val = " << val << '\n'; return *this; };
  103.     virtual ~Parent();
  104. };
  105.  
  106. Parent::~Parent() { std::cout << "Parent destructed! for " << this << '\n'; };
  107.  
  108. class Child : public Parent
  109. {
  110.     int* storage;
  111.     int size;
  112. public:
  113.     Child(int sizeP, int valP) : Parent{ valP }, storage{ new int[sizeP] }, size{ sizeP }
  114.     {
  115.         std::cout << "Child constructed! for " << this << '\n';
  116.     };
  117.     Child() : Child{ 5, 42 } {};
  118.     virtual const Child& info()const override { std::cout << "Child with val = " << getVal() << '\n'; return *this; };
  119.     virtual ~Child()override { delete[] storage; std::cout << "Child destructed! for " << this << '\n'; };
  120. };
  121.  
  122. void infoPrint(Parent par)
  123. {
  124.     par.info();
  125. }
  126.  
  127. int main()
  128. {
  129.     //Parent* ptrP{ new Child };
  130.  
  131.     //std::cout << '\n';
  132.     //ptrP->info();
  133.     //std::cout << '\n';
  134.     //infoPrint(*ptrP);
  135.     //std::cout << '\n';
  136.     //Parent father;
  137.     //father = *ptrP;
  138.     //father.info();
  139.  
  140.     //std::cout << '\n';
  141.     //delete ptrP;
  142.  
  143.     /*Polygon* polygons[]{ new Rectangle{4,5}, new Square{6}, new Triangle{3,4,5} };
  144.  
  145.     for (const auto& polygon : polygons)
  146.     {
  147.         polygon->info();
  148.  
  149.         delete polygon;
  150.     };*/
  151.  
  152.     std::vector<std::reference_wrapper<Polygon>> polygons;
  153.     auto r{ Rectangle{ 4,5 } };
  154.     auto s{ Square{ 6 } };
  155.     auto t{ Triangle{ 3,4,5 } };
  156.  
  157.     polygons.push_back(r);
  158.     polygons.push_back(s);
  159.     polygons.push_back(t);
  160.  
  161.     for (const auto& polygon : polygons)
  162.     {
  163.         polygon.get().info();
  164.     };
  165.  
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement