Write a class 'Example' that initializes two values 'v1' and 'v2' to '0' by default, and prints the values in this form: "Value 1: 20, Value 2: 30". When two objects of this class are added together using the '+' operator, the result is 'v1' of object 1 gets added to 'v1' of object 2 and 'v2' of object 1 gets added to 'v2' of object 2.

Write a class 'Example' that initializes two values 'v1' and 'v2' to '0' by default, and prints the values in this form: "Value 1: 20, Value 2: 30". When two objects of this class are added together using the '+' operator, the result is 'v1' of object 1 gets added to 'v1' of object 2 and 'v2' of object 1 gets added to 'v2' of object 2.


For example:
a = Example(20,30)
print(a)
Value 1: 20, Value 2: 30
b = Example(40,50)
c=a+b
print(c)
Value 1: 60, Value 2: 80



class Example(object):
  def __init__(self, v1 = 0, v2 = 0):
    self.v1 = v1
    self.v2 = v2
  def __add__(self, v1, v2):
    num = self.v1 + self.v2
    return num
 
def main():
  a = Example(20,30)
  print(a)
  b = Example(40,50)
  c=a+b
  print(c)
main()


Learn More :