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.