Why use Operator overloading ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why use Operator overloading ?

I can't understand why we should use operator overloading function and where we use it in program , is it necessary ?

16th Sep 2018, 8:38 AM
Atik🇧🇩
Atik🇧🇩 - avatar
3 Answers
+ 6
If default working of operator doesn't satisify you can override it for your own purpose. For example if you created Car object and it has float cost member. You can override (+) operator for this class, so if you add two cars actually their costs are added.
16th Sep 2018, 2:08 PM
Sharofiddin
Sharofiddin - avatar
+ 3
For example if you code a class and want to add or multiply two objects of that class. Then you need a function for the right way of adding/multiplying them. Think of matrix multiplication, where the standard * for single numbers does not work. Thus the operator * has to be overloaded to provide the correct result when multiplying two matrices.
16th Sep 2018, 8:57 AM
Matthias
Matthias - avatar
+ 1
to give additionalfunction so we use
17th Sep 2018, 11:56 AM
yaswanth
yaswanth - avatar