Answer to Question #208195 in C++ for champu

Question #208195

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/-.


1
Expert's answer
2021-06-18T02:54:35-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS