+ 2
Anyone can describe this code please
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; }
2 Answers
+ 7
You're overloading operator "+" in order to be able to add boxes (objects of class Box).
1st line: this is a static function (ie only one per program), it's public. It will return object of class Box. "operator+" means you'll be overloading "+" and the bit in parentheses means it would take two objects of class box as arguments.
body: you add height to height and width to width, then you create a new object of class Box with the height and width being the results of the additions. Then you return the object you created.
0
I keep getting an error from the first line in the code above. It says " Inconsistent accessibility: return type ' Program.Box' is less accessible than operator ' Program.operator +(Program.Box, Program.Box )'.
One of the parameters of a binary operator must be the containing type