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

3rd Dec 2016, 4:42 PM
abhishek sharma
abhishek sharma - avatar
2 Réponses
+ 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
10th Dec 2016, 8:22 AM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
0
Due to overloaded + operator you can calculate the sum of objects.
3rd Dec 2016, 4:49 PM
Nick V. Kondratiev
Nick V. Kondratiev - avatar