Answer to Question #209945 in C++ for Ahmed Ali

Question #209945

Write a program to compute the area of circle and triangle by using the pure virtual function.


1
Expert's answer
2021-06-23T05:21:34-0400
#include <iostream>
using namespace std;
class Shape{
    protected:
    public:
    Shape(){}
    virtual void area(){}
};
class Triangle: public Shape{
    float base, height;
    public:
    Triangle(float b, float h):base(b), height(h), Shape(){}
    void area(){
        cout<<0.5 * base * height;
    }
};
class Circle: public Shape{
    float radius;
    public:
    Circle(float r):radius(r), Shape(){}
    void area(){
        cout<<3.14159 * radius * radius;
    }
};
int main(){
    Circle circle(7);
    Triangle triangle(5, 8);


    cout<<"Circle: ";circle.area();cout<<endl;
    cout<<"Triangle: ";triangle.area();cout<<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