Friday, October 7, 2016

Display Area(Solution To E Balagurusamy Problem 9.1)

Tags

Solution:

#include<iostream>
using namespace std;
class shape
{
protected:
double a,b;
public:
void getdata()
{
cout<<"\nEnter The Parameters \n";
cin>>a>>b;
}
virtual void display()
{

}
};
class triangle:public shape
{
public:
void display()
{
double area;
area=0.5*a*b;
cout<<"\nArea of Triangle is "<<area;
}
};
class rectangle:public shape
{
public:
void display()
{
double area;
area=a*b;
cout<<"\nArea of Rectangle is "<<area;
}
};
main()
{  int choice;
shape *p;
cout<<"\nPress 1 For Triangle\nPress 2 For Rectangle\n ";
cin>>choice;
if(choice==1)
{ triangle t;
p=&t;
}
else if(choice==2)
{
rectangle r;
p=&r;
}
p->getdata();
p->display();
}


Output: