Answer to Question #334817 in C++ for zizou

Question #334817

Considering the Abstraction Principle, perform the following:

a.       [2 Marks] Define an abstract class by the name of Shape, the class consists of an attribute (D) which refers to the dimension and a member function Area().

b.       [2 Marks] Make the function Area a pure virtual function 

c.     [2 Marks] Define a class Circle which is s subclass of the superclass Shape and define the Area inside the class.

d.       [1 Marks] Define a second class Square which is a subclass of Shape and define the area inside it.


1
Expert's answer
2022-04-28T13:21:58-0400
#include <iostream>


#define PI 3.14159
using namespace std;


class Shape
{
	int D;
public:
	virtual float Area()= 0;
};


class Rectangl :public Shape
{
	float length;
	float width;
public:
	Rectangl(float _length = 0, float _width = 0)
		:length(_length), width(_width) {}
	void Assign()
	{
		cout << "Please, enter a length: ";
		cin >> length;
		cout << "Please, enter a width: ";
		cin >> width;
	}
	float Area()
	{
		return length*width;
	}
};


class Circle :public Shape
{
	float radius;
public:
	Circle(float _radius = 0):radius(_radius){}
	void Assign()
	{
		cout << "Please, enter a radius: ";
		cin >> radius;
	}
	float Area()
	{
		return PI*radius*radius;
	}
};

int main()
{
	Shape *s;
	Rectangl r(10, 20);
	s = &r;
	cout << "Area of rectangle is " << s->Area()<<endl;
	Circle c;
	c.Assign();
	s = &c;
	cout << "Area of circle is " << s->Area();
}	

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