How does operator-overloading work? Step by step | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How does operator-overloading work? Step by step

What are the components of operator-overloading? How does it work? What values does it calculate and return? I need someone who can answer me step by step the logic of this part of C++ - order of objects' definitions - time of operator 's execution - void constructor

13th Jun 2018, 6:34 PM
OrHy3
OrHy3 - avatar
23 Answers
+ 3
please read this for all the information and after that if you have any question then comment here. I am here to assist you. https://www.programiz.com/cpp-programming/operator-overloading
17th Jun 2018, 4:29 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 2
//sample code #include <iostream> using namespace std; class Check { private: int i; public: Check(): i(0) { } Check operator ++() { Check temp; ++i; temp.i = i; return temp; } void Display() { cout << "i=" << i << endl; } }; int main() { Check obj, obj1; obj.Display(); ++obj; obj.Display(); obj1 = ++obj; obj1.Display(); return 0; }
17th Jun 2018, 5:00 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
OrHy3 I did some change here for better understanding. from prints you will get this-> var represents the object using which operator overloading method called and obj.var represents the object which we are passing. please note the other change, MyClass res = obj1+obj2; Internally compiler will call like below, MyClass res = obj1.operator+(obj2); so now you can easily get way this->var has value of obj1 and obj.var has obj2 value. we can also use internal code which compiler used to resolved operator overload function call. #include <iostream> using namespace std; class MyClass { public: int var; MyClass() { } MyClass(int a) : var(a) { } MyClass operator+(MyClass &obj) { MyClass res; cout<<this->var<<endl; cout<<obj.var<<endl; res.var= this->var+obj.var; return res; } }; int main() { MyClass obj1(12), obj2(55); MyClass res = obj1.operator+(obj2); cout << res.var; }
17th Jun 2018, 5:30 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
thanks
17th Jun 2018, 4:53 PM
OrHy3
OrHy3 - avatar
0
How can I use more than one object in an operation?
17th Jun 2018, 5:03 PM
OrHy3
OrHy3 - avatar
17th Jun 2018, 5:04 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
https://code.sololearn.com/cH179plEBuKq/?ref=app
17th Jun 2018, 5:05 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
https://code.sololearn.com/cQgU53Wm0kU2/?ref=app I don 't understand why in the operator function this->var and obj.var don 't have the same value
17th Jun 2018, 5:10 PM
OrHy3
OrHy3 - avatar
0
OrHy3 try to run above code and you will get the idea.
17th Jun 2018, 5:32 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
So you 're saying to me that the operator function is executed by obj1 and use obj2 as its method, right?
17th Jun 2018, 5:34 PM
OrHy3
OrHy3 - avatar
0
You can more then two object for operation by using grouping via parentheses. example: #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), obj3(100); MyClass res = (obj1 + obj2 )+ obj3; cout << res.var; }
17th Jun 2018, 5:38 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
thanks too much
17th Jun 2018, 5:39 PM
OrHy3
OrHy3 - avatar
0
no, not method obj2 as argument. operator+ method is called for obj1 so this represents obj1. obj2 is passed in argument so obj.var represents obj2.
17th Jun 2018, 5:40 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
OrHy3 You are always wel come. Still if you have any questions then comment here. I will try my best to make it understandable.
17th Jun 2018, 5:44 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
ok, thanks
17th Jun 2018, 5:44 PM
OrHy3
OrHy3 - avatar
0
Just one question more: if there is more than one object, is the operator function called for the first object?
17th Jun 2018, 5:49 PM
OrHy3
OrHy3 - avatar
0
OrHy3 No not like that. example: (o1 + o2 ) + o3. first parentheses should execute. so ( o1 + o 2) , in this case operator method is called for o1 and o2 is passed as argument. returned object is stored in temporary object because + o3 is pending to execute. now the expression becomes, temp + o3. so operator method is called for temp and o3 is passed into argument.
17th Jun 2018, 6:17 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
I thought in (o1 + o2) the function is called for o1, not for o2
17th Jun 2018, 7:01 PM
OrHy3
OrHy3 - avatar
0
https://code.sololearn.com/cQgU53Wm0kU2/?ref=app Why does this code need a void constructor to work?
18th Jun 2018, 5:03 PM
OrHy3
OrHy3 - avatar
0
OrHy3 check line no 12 and 20,which needs default constructor. if you don't want to do that try like below, #include <iostream> using namespace std; class MyClass { public: int var; // MyClass() { } MyClass(int a) : var(a) { } MyClass operator+(MyClass &obj) { MyClass res(0); res.var= this->var+obj.var; return res; } }; int main() { MyClass obj1(12), obj2(55); MyClass res (obj1+obj2); cout << res.var; }
19th Jun 2018, 1:54 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar