#include <iostream>
#include <string>
using namespace std;
class Shape
{
public:
double a,b;
void get_data (){
cout<<"Enter a = ";
cin>>a;
cout<<"Enter b = ";
cin>>b;
}
virtual void display_area (){
cout<<"a = "<<a<<"\n";
cout<<"b = "<<b<<"\n";
}
};
class Triangle:public Shape
{
public:
void display_area (){
Shape::display_area();
double area=0.5*a*b;
cout<<"Area of triangle "<<area<<endl;
}
};
class Rectangle:public Shape
{
public:
void display_area (){
Shape::display_area();
double area=a*b;
cout<<"Area of rectangle: "<<area<<endl;
}
};
class Circle:public Shape
{
public:
void display_area (){
Shape::display_area();
double area=3.14*a*b;
cout<<"Area of Circle: "<<area<<endl;
}
};
int main()
{
Triangle triangle;
Shape *shape = ▵
cout<<"Enter (a is base, b is height) of the triangle:\n";
shape->get_data();
shape->display_area();
Rectangle rectangle;
shape = &rectangle;
cout<<"Enter lengths of two sides of the rectangle:\n";
shape->get_data();
shape->display_area();
Circle circle;
shape = &circle;
cout<<"Enter (a and b are the radius) of the circle:\n";
shape->get_data();
shape->display_area();
system("pause");
return 0;
}
Comments
Leave a comment