Answer to Question #131248 in C++ for MD RASEL BHUYAN

Question #131248

(Single Inheritance) Write a program to create a class CIRCLE with one field as radius, used

to calculate the area of a Circle. Create another class RECTANGLE used to calculate the area

of the rectangle which is a subclass of CIRCLE class. Use the concept of single inheritance

such that the radius of circle class can be re-used as length in rectangle class. Take necessary

data members and member functions for both the classes to solve this problem. All the data

members are initialized through the constructors. Show the result by accessing the area

method of both the classes through the objects of rectangle class

1
Expert's answer
2020-08-31T07:00:19-0400
#include <iostream>

using namespace std;

class Circle
{
protected:
    double radius{};

public:
    void setRadius(double radius)
    {
        this->radius = radius;
    }

    virtual double getArea()
    {
        return 3.14 * radius * radius;
    }
};

class Rectangle : public Circle
{
public:
    double getArea() override
    {
        return radius * radius;
    }

};

int main()
{

    Rectangle r;
    r.setRadius(20);
    cout <<"Area of Rectangle: "<< r.getArea() <<endl;
    cout <<"Area of Circle: "<< r.Circle::getArea() ;

    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