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.
#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;
}
Comments
Thanks for solve my problem.
Leave a comment