A program to creat a base Class shape. Use this Class to store two double type
values that could be used to compute the area of figures. Derive two specific
classes triangle and rectangle from the base class shape.Add to the base class
a member function get_data() to initialize base class data members and another
function display_area() to compute and display the area of figures.Make the
display_area function a virtual function and redefine this function in the
derived classes to suit their requirements.
Using
these three classes design a program that will accept dimensions of a triangle
or a rectangle interactively and display the area.
#include<iostream.h>
#include<conio.h>
public:virtual void
displayarea()=0;
void
getdata()
{cout<<"\n
enter the parameters for area to be calculated:";
cout<<"\n enter length: ";
cin>>d1;
cout<<"\n enter width or height:
";
cin>>d2;
}
};
class triangle:public shape
{public:void displayarea()
{cout<<"\n
the area of triangle is:"<<d1*d2/2;
}
}t;
class rectangle:public shape
{public:void displayarea()
{cout<<"\n
the area of rectangle is:"<<d1*d2;
}
}r;
void main()
{int n=0,i;
shape *ptr;
clrscr();
while(n!=1)
{clrscr();
cout<<"\n
enter the choice of figure whose area is to be calculated
\n1-rectangle\n2-triangle\n3-exit\nenter your choice:";
cin>>i;
switch(i)
{case
1:cout<<"\nfor rectangle\n";
ptr=&r;
ptr->getdata();
ptr->displayarea();
break;
case
2:cout<<"\nfor triangle\n";
ptr=&t;
ptr->getdata();
ptr->displayarea();
break;
case 3:n=1;
break;
default:cout<<"\n sorry you have entered wrong
choice\n";
}
cout<<"\n
do you want to continue (yes:0,no:1):";
cin>>n;
}
getch();
}
No comments:
Post a Comment