Answer to Question #253184 in C++ for Rohit Swain

Question #253184

Write a program in C++, which perform two operator overloading function for A=2+B,where A and B are the objects of the same class.


1
Expert's answer
2021-10-18T13:03:39-0400
#include <iostream>


using namespace std;


class SomeClass
{
private:
    double value;


public:
    void setValue(double value)
    {
        this->value = value;
    }
    double getValue()
    {
        return value;
    }


    friend SomeClass operator +(double value, SomeClass other)
    {
        SomeClass result;
        result.setValue(other.getValue()+value);
        return result;
    }


    friend SomeClass operator +(SomeClass other, double value)
    {
        return value+other;
    }


};
int main()
{
    SomeClass A;
    SomeClass B;
    B.setValue(3);


    A = 2+B;
    cout << "A = " << A.getValue() << endl;
    cout << "B = " << B.getValue();


    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

Rohit Swain
21.10.21, 18:26

Thanks for solve my problem.

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS