+ 1
Is anyone can explain me Operator Overloading ?
Operator Overloading
2 odpowiedzi
+ 14
Please, Before asking a question on the Q/A, try to 🔍SEARCH. . . to avoid from posting duplicate threads!
https://www.sololearn.com/Discuss/1219051/?ref=app
https://www.sololearn.com/Discuss/1553895/?ref=app
https://www.sololearn.com/Discuss/196686/?ref=app
You should read the COMMENTS!
https://code.sololearn.com/cHJ0O7DW54OF/?ref=app
+ 4
This lesson has lots of good examples
https://www.sololearn.com/learn/CPlusPlus/1901/?ref=app
But if they aren't helping, then let me explain.
(First of all make sure you are familiar with classes and objects)
Suppose you have defined a class. What happens on operator overloading is that you are defining how to handle different operations (mathematic or bitwise)
For example - assume that you have a class called Money - which holds data about money. It have a field called value.
Suppose that we want to add two monetary values we can do following
int sum = money1.value + money2.value
But the problem here is that we are getting.an integer value instead of Money objects.
Then operator overloading comes in handy
Money operator+(Money other) {
Money newVal;
newVal.value = this->value+other.value;
return newVal;
after that we can simply perform money1 + money2 and get a Money object as a result.