+ 2
Can you give an example Of practical use Of operators overloading?
3 Answers
+ 6
It allows you to code complex manipulation with objects as simple expressions with common operators instead of member functions. It increases the readability of your code. And makes it easier use your class.
Abstact example:
Instead of
MyClass a, b, c;
a.SetValue(b.GetValue()+c.GetValue());
You can write:
MyClass a, b, c;
a=b+c;
if you have operators = and + overloaded in MyClass.
There's more benefits in operator overloading than just that.
+ 2
For strings in C we should use strcmp to compare them because s1==s2 means u compare their addresses not their values. But in C++ we use == directly to compare that means he already used operator overloading to make it easier for us.
+ 1
If you create a class for complex numbers, you can overload the operators +, -, *, / to make calculations between complex numbers.