Program to add objects using '+' operator in Python.
Problem description: we will add objects using the '+' operator in Python.
In the program, we will create three objects and then add them using the '+' operator. And return the result of addition. Each object will contain two variables that will be added to the variables of other objects on calling the addition operator.
Class and methods used:
- Class : TwoNum
- Method: getValue() -> get input values from the user.
- Method: printValues() -> prints values and their sum.
- Operator: __add__() -> adds objects.
Program to illustrate the working of our solution
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer