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
/******************************************************************************
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{
protected:
float x;
float y;
public:
void setX(float a){
x=a;
}
void setY(float b){
y=b;
}
float getX(){
return x;
}
float getY(){
return y;
}
virtual void display_area(){
cout<<"Area: "<<getX()*getY()<<endl;
}
};
class Triangle: public Shape{
public:
virtual void display_area(){
cout<<"Area of the Triangle: "<<0.5*getX()*getY()<<endl;
}
};
class Rectangle: public Shape{
public:
virtual void display_area(){
cout<<"Area of the Rectangle: "<<getX()*getY()<<endl;
}
};
int main()
{
Triangle t;
t.setX(15);
t.setY(23);
t.display_area();
Rectangle r;
r.setX(21);
r.setY(34);
r.display_area();
return 0;
}
Comments
Leave a comment