Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <math.h>
- class Polygon
- {
- protected:
- std::vector<float> sides;
- public:
- Polygon& addSide(float size) { sides.push_back(size); return *this; };
- Polygon& setSide(int sideId, float size)
- {
- sides[sideId] = size; return *this;
- };
- float getSide(int sideId)const { return sides[sideId]; };
- virtual float perimeter()const
- {
- float per{ 0 };
- for (const auto & side : sides)
- {
- per += side;
- }
- return per;
- };
- virtual float area()const;
- const Polygon& sidesInfo()const
- {
- for (int i{ 0 }; i < sides.size(); ++i)
- {
- std::cout << " Side " << char('A' + i) << " is " << sides[i] << " long\n";
- }
- return *this;
- }
- std::string name()const
- {
- std::string nm{ typeid(*this).name() }; //"class Polygon"
- return nm.substr(5);
- }
- const Polygon& info()const
- {
- std::cout << name() << ":\n"
- << " Perimeter: " << perimeter()
- << " Area: " << area() << '\n';
- sidesInfo();
- std::cout << '\n';
- return *this;
- }
- };
- float Polygon::area()const
- {
- return -1;
- }
- class Rectangle : public Polygon
- {
- public:
- Rectangle(float sideA, float sideB) { addSide(sideA); addSide(sideB); };
- virtual float perimeter()const override{ return Polygon::perimeter() * 2; };
- virtual float area()const override { return getSide(0) * getSide(1); }
- };
- class Square : public Rectangle
- {
- public:
- Square(float sideA) : Rectangle{ sideA, sideA } {};
- };
- class Triangle : public Polygon
- {
- public:
- Triangle(float sideA, float sideB, float sideC)
- {
- addSide(sideA).addSide(sideB).addSide(sideC);
- };
- virtual float area()const override
- {
- const float p{ perimeter() / 2 };
- return sqrt(p * (p - getSide(0)) * (p - getSide(1)) * (p - getSide(2)));
- }
- };
- class Parent
- {
- int val;
- public:
- Parent(int valP) : val{ valP }
- {
- std::cout << "Parent constructed! for " << this << '\n';
- };
- Parent() : Parent{ 0 } {};
- int getVal()const { return val; };
- virtual const Parent& info()const { std::cout << "Parent with val = " << val << '\n'; return *this; };
- virtual ~Parent();
- };
- Parent::~Parent() { std::cout << "Parent destructed! for " << this << '\n'; };
- class Child : public Parent
- {
- int* storage;
- int size;
- public:
- Child(int sizeP, int valP) : Parent{ valP }, storage{ new int[sizeP] }, size{ sizeP }
- {
- std::cout << "Child constructed! for " << this << '\n';
- };
- Child() : Child{ 5, 42 } {};
- virtual const Child& info()const override { std::cout << "Child with val = " << getVal() << '\n'; return *this; };
- virtual ~Child()override { delete[] storage; std::cout << "Child destructed! for " << this << '\n'; };
- };
- void infoPrint(Parent par)
- {
- par.info();
- }
- int main()
- {
- //Parent* ptrP{ new Child };
- //std::cout << '\n';
- //ptrP->info();
- //std::cout << '\n';
- //infoPrint(*ptrP);
- //std::cout << '\n';
- //Parent father;
- //father = *ptrP;
- //father.info();
- //std::cout << '\n';
- //delete ptrP;
- /*Polygon* polygons[]{ new Rectangle{4,5}, new Square{6}, new Triangle{3,4,5} };
- for (const auto& polygon : polygons)
- {
- polygon->info();
- delete polygon;
- };*/
- std::vector<std::reference_wrapper<Polygon>> polygons;
- auto r{ Rectangle{ 4,5 } };
- auto s{ Square{ 6 } };
- auto t{ Triangle{ 3,4,5 } };
- polygons.push_back(r);
- polygons.push_back(s);
- polygons.push_back(t);
- for (const auto& polygon : polygons)
- {
- polygon.get().info();
- };
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement