Write a C++ program to design polygon-rectangle-square-parallelogram-triangle hierarchy with pure virtual function of getArea(). The base class now is an abstract base class and lots of other classes added to the hierarchy.
#include <iostream>
using namespace std;
class Polygon
{
public:
virtual void getArea() = 0;
virtual ~Polygon() {}
};
class Rectangle : public Polygon
{
public:
void getArea() { cout << "Area of Rectangle!" << endl; }
};
class Square : public Polygon
{
public:
void getArea() { cout << "Area of Square!" << endl; }
};
class Parallelogram : public Polygon
{
public:
void getArea() { cout << "Area of Parallelogram!" << endl; }
};
class Triangle : public Polygon
{
public:
void getArea() { cout << "Area of Triangle!" << endl; }
};
int main()
{
Polygon* Shape = new Square();
Shape->getArea();
Shape = new Rectangle();
Shape->getArea();
Shape = new Parallelogram();
Shape->getArea();
Shape = new Triangle();
Shape->getArea();
cout << endl;
return 0;
}
Comments
Leave a comment