Operator overloading | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Operator overloading

what is the benefit of overloading operators?

26th Oct 2018, 5:19 AM
1704086_ibnul
1704086_ibnul - avatar
2 Answers
+ 3
So you can treat user defined types as if they were built-in types, you can compare with ==, assign with =, +=, -=, increment-decrement with ++, --, or do other operations to the internal values encapsulated in the user defined type, and yet it looks and feels like working with built-in types. Hth, cmiiw
26th Oct 2018, 8:00 AM
Ipang
0
class BankAccount: def __init__(self, balance): self.balance = balance def _add_(self , other): return BankAccount(self.balance + other.balance) a=BankAccount(1024) b=BankAccount(42) result = a + b print(result.balance) This is ans for the code coach, try it to know more!
31st May 2022, 7:50 AM
Hrithika Reddy
Hrithika Reddy - avatar