Solved

what does "overloading operators" mean

Posted on 2004-10-08
9
191 Views
Last Modified: 2010-04-01
hi
i am looking for a short, sweet and SIMPLE explaination of what this c++ term means
i have been searching for one in text books and on the net
but its all so complicated!!!

thanks
lise
0
Comment
Question by:liseperu
9 Comments
 
LVL 6

Assisted Solution

by:msjammu
msjammu earned 20 total points
Comment Utility
Operator Overloading means:

Telling C++ operators about the classes that you create,
When you overload an operator you tell this operator how it can operate upon class you created.
C++ operators does not know how to performs on the objects of the classes that your create You nedd to tell them this.

Hope this help
0
 
LVL 6

Expert Comment

by:msjammu
Comment Utility
Suppose this,

int a ,b

int c = a + b;
--------------------
C++ has already told + operator how to operate upon variables of type int.


myclass a, b
myclass c = a + b

here + does not know how to perform on objects of type myclass, SO you need to tell + operator this. (The work that C++ did for int)
0
 
LVL 22

Accepted Solution

by:
grg99 earned 40 total points
Comment Utility
It's basically pretty simple.

"C++" defines many predefined operators, like "+".  By default, "+" is recognized by the compiler int he context of adding numbers, both integer and float.  

Now let's say you'd like to be able to add some other data types, maybe complex numbers.  C++ doesnt know about complex numbers, or how to add them, so you could define a datatype "COMPLEX" "Struct { float real,img; } and define some functions:   complex_add( COMPLEX a, b ), and so on.   But your program wouldnt look too natural, it would look like:

sum = complex_add( a,b );     // assuming you can return a struct !! good enough for this duiscussion!

wouldnt it be much clearer if you could jsut write:   result = a + b;   ?

You can if you "overload" the "+" sign operator.  Overloading means adding YET ANOTHER meaning on top of its existing meaning.

So you can tell the C compiler:  please run this function I've given you if you see the "+" sign with a COMPLEX data type on either side of it.   The complier then swallows up this definition of yours, and if it ever sees  a + b, where a and b are the right types, it calls your function, which if you've written it correctly, will add its parameters and return the right result.

---------------
This can be a wonderful thing.  It cam make programs much clearer.

It can also be a terrible thing, In My Humble Opinion, if overdone or done clumsily.

For example, it can silently turn the "+" sign into something the casual reader wont notice is now defined as doing something very un-pluslike.  Or if you accidentally pass the wrong types, it will silently call the overloaded function when you expected the original "+" sign behavior.

Also many a C compiler puts out quixotic error messages i fit gets partially confused by an overloading declaration.

Use it wisely.


0
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 20 total points
Comment Utility
Hi liseperu,
"Overloading" in general means that there are several versions of a function or method with an identical name. The compiler chooses the appropriate one by using the parameters supplied with the call.

A simple overloading example:
void increment(int& var){
    var++;
}

void increment(int& var, const int& by_what){
    var+=bywhat;
}

(You'd normally use only the 2nd version, with 1 as a default value for by_what)

Depending on whether you call the function with one or two parameters, the compiler either chooses the first or the second version.

Operators are just "syntactical sugar" for a function or method call. Those calls are identical:

a+b
a.operator+(b)     (method version)
operator+(a,b)     (function version)




Cheers!

Stefan
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Assisted Solution

by:GFinseth
GFinseth earned 20 total points
Comment Utility
Overloading means the same operator or function behaves differently depending on how you use it.

C actually did some of this, since operators like + could be used on more than one type.  If you wrote z = a + b, it works properly whether the variables are chars, ints, or floats.

C++ extends this to let you overload your own operators and functions.  You could add more versions of + to add together imaginary numbers, or vectors, or whatever you want.

In C, if you wrote a function to print two values, you'd need a differently named version for each type, like print_ints(int a, int b) and print_floats(float a, float b).

In C++, you can have print(int a,int b) and a seemingly identically named print(float a, float b).

The compiler changes the name behind the scenes based on the argument types, sort of like tacking on the _int and _float for you.  Then the linker can automatically grab the right one.

For your part, you have to write both versions.  The big deal with overloading is they can have the exact same name and the right one gets used automatically.
0
 
LVL 9

Expert Comment

by:jhshukla
Comment Utility
you tell the operator to __do_more_that_it_normally does__. thus, you "overload" the operator. I don't have much to add here. since othes experts have provided good enough explanation.
0
 
LVL 3

Assisted Solution

by:aravindtj
aravindtj earned 20 total points
Comment Utility

simple example.

string a = "experts-";
string b = "exchange";

int c = 10;
int d = 20;

string e;
int f;

e = a + b; // + operator is overloaded for concatination
f = c + d; // + is addition - normal.

for how to, refer some good books.
0
 
LVL 2

Expert Comment

by:dimitry_dimitry
Comment Utility
Overloading means that you create a new signature(parameters and optionally return value) to
function/class method. It may be used to provide user with option to use one "term" with varying/changing arguments, for example
int Add(int t,int t2);
long Add(long l,long ll);
instead of
int Add_2_integers(int t,int t2);
long Add_2_longs(long l,long ll);

Also consider this:
class foo
{
   int bar();
   void bar(); // error, differ only by return type
   int bar(int t); //ok
   int bar(long t); // if sizeof(long)!=sizeof(int)
   void bar(int t); // ok
};

overloading operators:
long operator+(long one,long two)
{
}

double operator+(double one,double two)
{
}
0
 

Author Comment

by:liseperu
Comment Utility
thanks for your help everyone
i have finally understand

:)
thanks
lis
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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 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…

744 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

15 Experts available now in Live!

Get 1:1 Help Now