Please, explain me the algorithm operator-overloading. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please, explain me the algorithm operator-overloading.

for example, witch gave the app: We create two objects called obj1(15) and obj2(25) witch have class Sample. These objects have element int var, witch is filled in with constructor: Sample(int a){var = a}. Now we have obj1.var = 15, obg2.var = 25. Also in class Sample we have: MyClass operator+(MyClass &obj) { MyClass res; res.var = this->var+obj.var; return res; } and in void main: My Class res = obj1+obj2; What is going on after created object res? --(sorry for bad english)--

7th Aug 2017, 1:53 PM
Alexik
Alexik - avatar
7 Answers
0
When you declare a object this way, using the =operator, you are calling the constructor by copy, because you are returning a object of type myClass with the +operator. That constructor is made by default. You can make it myClass(const myClass & obj):var(obj.var) {std::cout<<"Copy";}; Please tell me if that was you question.
8th Aug 2017, 7:46 PM
Oscar Albornoz
Oscar Albornoz - avatar
0
@Oscar Albornoz, how compiler know that this->var is our obj1 and obj.var? I mean, where we input this? and if I create one more operator overloading for -, create new object called obj3 and write for example obj1+obj2-obj3, than in +operator: this->var is obj1.var, obj.var is obj2, and in -operator: this->var is (obj1.var + obj2.var), obj.var is obj3. Why this->car is the last result and obj.var is next object after operator?
8th Aug 2017, 8:07 PM
Alexik
Alexik - avatar
0
@Oscar Albornoz, "this->var is our obj1 and obj.var is obj2.var?"
8th Aug 2017, 8:09 PM
Alexik
Alexik - avatar
0
Ok, let's go to see this If you declare a method this way myClass add(const myClass & obj) { myClass res; res.var=this->var+obj.var; return res; } To use this method, we would something like myClass obj3 = obj1.add(obj2); Here, who called the method add was obj1, so he is 'this', obj2 is the parameter passed. Now, with the operator+ method , we could do this myClass obj3 = obj1+(obj2); or simply obj3 = obj1+obj2. In resume, who call to the method is the left object. Here, obj1+obj2-obj3, by operator precedence, that would tmp = obj1+(obj2) tmp-(obj3);
8th Aug 2017, 9:07 PM
Oscar Albornoz
Oscar Albornoz - avatar
0
@Oscar Albornoz, yes, this is my question. Thanks, but I have one more. I created five objects: obj1(10), obj2(20), obj3(30), obj4(40), obj5(50), and four operator overloading for +, -, * and/. And then, if I wrote obj5-obj4+obj3*obj2/obj1, there was no output, but if I wrote obj3*obj2/obj1-obj4+obj5, I had correct answer. Why? There is no problem with division, no float value.
8th Aug 2017, 10:16 PM
Alexik
Alexik - avatar
0
This code works for me, you can check out and compare with your solution https://code.sololearn.com/cy7LJ5eS9coE/?ref=app
9th Aug 2017, 1:05 AM
Oscar Albornoz
Oscar Albornoz - avatar
0
@Oscar Albornoz, repeatedly, this code output nothing, and, repeatedly, correct result. Maybe, sometimes compiler works incorrectly, at least, on my phone.
9th Aug 2017, 9:41 AM
Alexik
Alexik - avatar