educative.io

Why do Operator Overloading?

I need to understand why do we perform operator overloading?
What are the specific cases to use it?
Is it language specific/dependent?

Is there any alternative to fix this?

Hi Leena,

Thank you for reaching out! We are happy to hear from you.

So while operator overloading may not seem to be useful, it actually is. So say you have a class with 2 objects, obj1 and obj2 and you wish to add them. You cannot exactly add them using the + operator so you will have to create a function called addObj() or something of the sort. Calling addObj() repeatedly in a lot of code would not only make the code bulkier but would also be less intuitive than overloading the + operator and using that. Also, operator overloading is langauge-syntax specific. You can overload operators in some major languages like C, C++, and Python, you cannot in Java or JavaScript. Hope this helped.

If you have any further queries, please let us know.

1 Like