Why we use operator overloading? Explain some cases where we can use operator overloading.

Solution

Operator overloading makes the code cleaner and simpler architecture. 

If Operator overloading is not available then we have to write methods in the class to perform the same logic and have to call the method by name.

Usage

Adding or subtracting two complex numbers/ imaginary numbers (a + b) -  we can overload + and - operator to perform add and subtract operations respectively on complex number

Adding or subtracting rational numbers - we can overload + and - operator to perform add and subtract operations on rational number (a/b + c/d)

We can overload both unary and binary operators.

Share this thread

Comments

Comments
comments powered by Disqus

Navigation

Social Media