Anyone can describe this code please | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

16th Jul 2016, 12:21 PM
Aalap Patel
Aalap Patel - avatar
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.
16th Jul 2016, 2:55 PM
Tomek Cymes
Tomek Cymes - avatar
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
6th Sep 2016, 5:05 AM
Kyle Friedt
Kyle Friedt - avatar