Answer to Question #178551 in C++ for aliza

Question #178551

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.


1
Expert's answer
2021-04-05T16:28:54-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS