Answer to Question #181892 in C++ for VARUN

Question #181892

Compile time polymorphism using operator overloading


Create the Circle class and overload the + operator so that you can add two Circle objects. Adding two Circle object should give another Circle whose radius the sum of the radii of the two Circle objects.

for example circle1 radius is 7, circle2 radius is 9


1
Expert's answer
2021-04-15T23:21:25-0400
#include <iostream>


using namespace std;


class Circle
{
private:
    double radius;


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


    friend Circle operator +(Circle circle1, Circle circle2)
    {
        Circle result;
        result.setRadius(circle1.getRadius()+circle2.getRadius());
        return result;
    }


};


int main()
{
    Circle circle1;
    circle1.setRadius(7);


    Circle circle2;
    circle2.setRadius(9);


    Circle circle3 = circle1+circle2;


    cout << "radius of first circle: " << circle1.getRadius() << endl;
    cout << "radius of second circle: " << circle2.getRadius() << endl;
    cout << "radius of third circle: " << circle3.getRadius() << 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