Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Please explain example that overloads the addition operator (+)

Posted on 2013-06-12
2
Medium Priority
?
282 Views
Last Modified: 2013-06-12
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
Comment
Question by:naseeam
2 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 2000 total points
ID: 39241262
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
 
LVL 1

Author Closing Comment

by:naseeam
ID: 39241343
Nice concise explanation!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question