Use the concepts of inheritance and abstract base class to compute the fencing cost for a
triangular, circular and rectangular region. The fencing cost for 1m is 200/-.
#include <iostream>
using namespace std;
class Shape{
protected:
const int price = 200;
public:
Shape(){}
virtual float perimeter(){return 0;}
virtual float cost(){return 0;}
};
class Triangle: public Shape{
float base, height;
public:
Triangle(float b, float h):base(b), height(h), Shape(){}
float perimeter(){
return 0.5 * base * height;
}
float cost(){
return this->perimeter() * price;
}
};
class Circle: public Shape{
float radius;
public:
Circle(float r):radius(r), Shape(){}
float perimeter(){
return 3.14159 * 2 * radius;
}
float cost(){
return this->perimeter() * price;
}
};
class Rectangle: public Shape{
float length, width;
public:
Rectangle(float l, float w): length(l), width(w), Shape(){}
float perimeter(){
return length * width;
}
float cost(){
return this->perimeter() * price;
}
};
int main(){
Circle circle(7);
Triangle triangle(5, 8);
Rectangle rectangle(5, 5);
cout<<"Circle: "<<circle.cost()<<endl;
cout<<"Rectangle: "<<rectangle.cost()<<endl;
cout<<"Triangle: "<<triangle.cost()<<endl;
return 0;
}
Comments
Leave a comment