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.
#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();
}
Comments
Leave a comment