+ 1

Operator Overloading (Example II)

class Box { public int Height { get; set; } public int Width { get; set; } public Box(int h, int w) { Height = h; Width = w; } public static Box operator+(Box a, Box b) { int h = a.Height + b.Height; int w = a.Width + b.Width; Box res = new Box(h, w); return res; } } static void Main(string[] args) { Box b1 = new Box(14, 3); Box b2 = new Box(5, 7); Box b3 = b1 + b2; Console.WriteLine(b3.Height); //19 Console.WriteLine(b3.Width); //10 } - How does this work? (please explain in 'real terms/English' not 'programming terms') - Some of my thoughts process/confusion: I get the top portion that refers to get/set. My biggest confusion is this section: "public static Box operator+(Box a, Box b)". Does "operator+" refer to a name and then the "+" means overload? Or does "operator+" have to be there, similar to public, static, and other keywords, etc.

21st Feb 2017, 6:07 AM
William Draper
William Draper - avatar
3 odpowiedzi
+ 3
operator is a keyword. + is the operator that is being overloaded. + is defined to add two numbers in general. but here you are trying to define the step that are two be taken in adding two objects of class. This is important thing in c++. say you write time class in c++ you cannot normally add it. you need to define the + operator to add seconds first then if it is greater than 60 then reduced it by 60 and increase minute by 1 and same for minutes.
21st Feb 2017, 7:51 AM
Megatron
Megatron - avatar
+ 3
class Box { public int Height { get; set; } public int Width { get; set; } public Box(int h, int w) { Height = h; Width = w; } public static Box operator+(Box a, Box b) { int h = a.Height + b.Height; int w = a.Width + b.Width; Box res = new Box(h, w); return res; } } static void Main(string[] args) { Box b1 = new Box(14, 3); Box b2 = new Box(5, 7); Box b3 = b1 + b2; Console.WriteLine(b3.Height); //19 Console.WriteLine(b3.Width); //10 } - How does this work? (please explain in 'real terms/English' not 'programming terms') - Some of my thoughts process/confusion: I get the top portion that refers to get/set. My biggest confusion is this section: "public static Box operator+(Box a, Box b)". Does "operator+" refer to a name and then the "+" means overload? Or does "operator+" have to be there, similar to public, static, and other keywords, etc. operator is a keyword. + is the operator that is being overloaded. + is defined to add two numbers in general. but here you are trying to define the step that are two be taken in adding two objects of class. This is important thing in c++. say you write time class in c++ you cannot normally add it. you need to define the + operator to add seconds first then if it is greater than 60 then reduced it by 60 and increase minute by 1 and same for minutes.
28th Mar 2019, 5:04 AM
Sridhar Raj.P
Sridhar Raj.P - avatar
+ 2
I think that helps a bit... I'm still a little confused, but I'll tinker with it some and figure it out. Thank you for getting back to me. :) Edit: *for replying to the above.
21st Feb 2017, 8:03 AM
William Draper
William Draper - avatar