Write a program having classes Rectangle with data member length and width ,square with data member side .Equalitrial Triangle with data member sides.All these classes are derived from class shape.Write an independent function which will exhibit polymorphism and show thr area of the object passed to the function.All the data memebers of class are private you can write suitable function needed for you
#include <iostream>
using namespace std;
class Shape{
public:
Shape(){}
virtual float area(){return 0;}
};
class Rectangle:public Shape{
float length, width;
public:
Rectangle(): Shape(){}
Rectangle(float l, float w): length(l), width(w), Shape(){}
float area(){
return length * width;
}
};
class Square:public Shape{
float side;
public:
Square():Shape(){}
Square(float s):side(s), Shape(){}
float area(){
return side * side;
}
};
class EquilateralTriangle:public Shape{
float side;
public:
EquilateralTriangle(): Shape(){}
EquilateralTriangle(float s): side(s), Shape(){}
float area(){
return 0.5 * side * side;
}
};
template<typename object>
void showArea(object* Obj){
cout<<Obj->area()<<endl;
}
int main(){
Rectangle rect(5, 5);
Square sq(5.5);
EquilateralTriangle triangle(6);
showArea<Rectangle>(&rect);
showArea<Square>(&sq);
showArea<EquilateralTriangle>(&triangle);
return 0;
}
Comments
Leave a comment