Create a base class called Shape which has two double type values. Use member functions get() and set() to get and set the values. Use a virtual function display_area() to display the area. Derive two specific classes called Triangle and Rectangle from the base class Shape. Redefine display_area() in the derived classes. Write a program to display the rectangle or triangle area.
#include<iostream>
using namespace std;
class Shape {
private:
double first, second;
public:
void setFirst(double f)
{ first =f;}
void setSecond(double s)
{second = s;
}
double getFirst(){
return first;
}
double getSecond(){
return second;
}
virtual void display_area(){}
};
class Triangle: public Shape{
public:
void display_area(){
cout<<"The area of the triangle is \t"<< 0.5*getFirst()*getSecond();
}
};
class Rectangle: public Shape{
public:
void display_area(){
cout<<"The area of the rectangle is \t"<< getFirst()*getSecond()<<endl;}
};
int main(){
Rectangle rec;
rec.setSecond(9);
rec.setFirst(12);
rec.display_area();
Triangle tr;
tr.setSecond(9);
tr.setFirst(12);
tr.display_area();}
Comments
Leave a comment