+ 3

Anyone please explain what happens when we write Myclass res=obj1+obj2; I mean how the values are passed and how it works

#include <iostream> using namespace std; 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; }

15th Jul 2017, 8:10 AM
Tamil Selvan
Tamil Selvan - avatar
1 Answer
+ 3
Myclass res = obj1+obj2 is similar to Myclass res = obj1.operator+(obj2) That means obj1 is used to call the function 'operator+' whereas obj2 is passed as an argument.
15th Jul 2017, 9:11 AM
Mihir Patel