Answer to Question #214010 in C++ for Hemambar

Question #214010

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.


1
Expert's answer
2021-07-15T00:10:41-0400
#include <iostream>
using namespace std;
class Shape{
    protected:
    float a, b;
    public:
    Shape(){}
    virtual void display_area(){}
};
class Triangle: public Shape{
    public:
    Triangle(float x, float h): Shape(){
        a = x;
        b = h;
    }
    void display_area(){
        cout<<0.5 * a * b;
    }
};
class Rectangle: public Shape{
    public:
    Rectangle(float x, float h): Shape(){
        a = x;
        b = h;
    }
    void display_area(){
        cout<<a * b;
    }
};
int main(){
    Rectangle rectangle(7, 5);
    Triangle triangle(5, 8);


    cout<<"Rectangle: ";rectangle.display_area();cout<<endl;
    cout<<"Triangle: ";triangle.display_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