Create a base class called 'SHAPE' having
a. two data members of type double
b. member function get-data( ) to initialize base class data members
c. pure virtual member function display-area( ) to compute and display the area of the geometrical object.
Derive two specific classes 'TRIANGLE' and 'RECTANGLE' from the base class. Using these three classes design a program that will accept dimension of a triangle/rectangle interactively and display the area.




#include<iostream.h>
#include<conio.h>
class shape
{
protected:double x, y;
public:void getdata(double a, double b)
{
x=a;
y=b;
}
virtual void display_area()=0;
};
class triangle:public shape
{
double triangle_area;
void display_area()
{
triangle_area=(1*x*y)/2;
cout<<"Area of triangle is:"<<triangle_area<<endl;
}
};
class rectangle:public shape
{
double rectangle_area;
void display_area()
{
rectangle_area=x*y;
cout<<"Area of rectangle is:"<<rectangle_area;
}
};
int main()
{
clrscr();
shape *p;
triangle t;
rectangle r;
p=&t;
p->getdata(10,3);
p->display_area();
p=&r;
p->getdata(20,3);
p->display_area();
getch();
return 0;
}




The output of the above program is:
Area of triangle is:15
Area of rectangle is:60


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: