Solved

Please explain example that overloads the addition operator (+)

Posted on 2013-06-12
2
269 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 500 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
 

Author Closing Comment

by:naseeam
ID: 39241343
Nice concise explanation!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now