+ 2

Can someone help me 😰😰

i am learning C++ Overloaded Operator and https://code.sololearn.com/c1nFXi7IY7Kg/?ref=app In this above code i don't understand how does this function work and Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } . volume = Box3.getVolume(); call overloaded operator function but volume = Box2.getVolume(); is not call overloaded operator function.I don't know why this happened đŸ˜„đŸ˜„ Can someone Explain me because i am stuck here 😱😱😭

13th Nov 2020, 4:04 AM
Arianna Grace
Arianna Grace - avatar
3 Answers
+ 6
Question 1: volume = Box3.getVolume(); call overloaded operator function but volume = Box2.getVolume(); is not call overloaded operator function. Ans: Because Box3 object parameters are set by adding two other objects parameters. Box3=Box1+Box2 When we perform operations between two objects overload operator was called. Question 2: In this above code i don't understand how does this function work and Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } Answer: When + operator was used between functions it will add right side operand (b.height) to left side operand (this->height). After adding result value assigned to Box3. Hope this helps â˜ș
13th Nov 2020, 4:36 AM
AzhagesanăƒŸ(✿)
AzhagesanăƒŸ(✿) - avatar
+ 9
Arianna Grace here you are overloading the " + " operator to operate on objects of class "box" When called {on line 60 of your code } " Box3 = Box1 + Box2;" In the function the following process is taking place :- 1) create a new object of class "box" 2) set the length of this new box to sum of box1 and box2 3) the other two lines set the breadth and height similarly. 4) return the new object with the set dimentions When the value returned be overloaded "+" operator function is assigned to box3 whose volmue is calculated in the next line
13th Nov 2020, 4:35 AM
Arsenic
Arsenic - avatar
0
Oof...i agree...this is complicated
13th Nov 2020, 4:06 AM
mnaeem08
mnaeem08 - avatar