Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Please explain example that overloads the addition operator (+)

Posted on 2013-06-12
2
Medium Priority
?
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

670 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