Why i have to create 2 myclass constructors to use the operator overloading? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why i have to create 2 myclass constructors to use the operator overloading?

class MyClass { public: int var; MyClass(){}; MyClass(int a) : var(a) { } MyClass operator+(MyClass &obj) { MyClass res; res.var= this->var+obj.var; return res; } }; int main() { MyClass obj1(12), obj2(55); MyClass res = obj1+obj2; cout << res.var;

16th Dec 2018, 8:03 PM
Daniel
1 Answer
0
You don't. The code would work as coded without the first one. This is how to code it without the second. https://code.sololearn.com/c4y5VWVOSofP
16th Dec 2018, 11:58 PM
John Wells
John Wells - avatar