Answer to Question #213204 in C++ for Hemambar

Question #213204

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-24T07:06:04-0400
#include <iostream>




using namespace std;




class Shape{
    protected:
        float base_length;
        float height_width;
    public:
        
        void setbaseLength(float a){
            base_length=a;
        }
        void setHeightWidth(float b){
            height_width=b;
        }
        float getbaseLength(){
            return base_length;
        }
        float getHeightWidth(){
            return height_width;
        }
        virtual void display_area(){
            cout<<"Area of the shape: "<<getbaseLength()*getHeightWidth()<<endl;
        }
};
class Triangle: public Shape{
    public:
        virtual void display_area(){
            cout<<"Area of Triangle: "<<0.5*getbaseLength()*getHeightWidth()<<endl;
        }
    
};
class Rectangle: public Shape{
    public:
    
        virtual void display_area(){
                cout<<"Area  of  Rectangle: "<<getbaseLength()*getHeightWidth()<<endl;
            }
};
int main()
{
    Triangle t;
    t.setbaseLength(15);
    t.setHeightWidth(23);
    t.display_area();




    Rectangle r;
    r.setbaseLength(21);
    r.setHeightWidth(34);
    r.display_area();
    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