• Status: Solved
• Priority: Medium
• Security: Public
• Views: 286
• Last Modified:

# Please explain example that overloads the addition operator (+)

Following is link to example that overloads addition operator (+)

http://www.cplusplus.com/doc/tutorial/classes2/

Please explain in some detail.  Does statement 'c = a + b' call  the operator+ function.  Please
step through the operator+ function.  Is it called twice?  Once with passing a, and another time passing b.  How does this functions add (3+1, 1+2) = (4,3).
0
naseeam
Asked:
1 Solution

Commented:
The + operator is the same as calling a method of the class, in this case operator+,
so c = a + b is the same as c = a.operator+ (b)

So, if we were adding vectors, you would have c = (3,1) + (1,2)  which is the same as (3,1).operator+(1,2)

The method already knows its own value, so we just pass into it the data that is being (in this case) added to it.
0

Author Commented:
Nice concise explanation!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.