0
Can any explain me overloading concept you can help me out by explaining this code
class Shape attr_accessor :h, :w def initialize(h, w) self.h = h self.w = w end def +(other) Shape.new(self.h+other.h, self.w+other.w) end end a = Shape.new(7, 4) b = Shape.new(9, 18) c = a+b puts c.h # outputs 16 puts c.w # outputs 22
2 ответов
+ 2
As i understood, normally you can add with + only integers or floats. when you create a shape object, you could not add its element with + to an element of an other shape object.
overloading means, that you expand the possibility of +.
now you can add 2 quadrilateral for example.
shape.new(self.h + other.h, self.w+ other.w)
means, it takes the weight side of one shape and other shape, add it, and takes height of one shape and other shape, and add it.
the new shape will have the area that is the sum of the 2 shape.
sorry my english
0
Due to overloaded + operator you can calculate the sum of objects.