what does "overloading operators" mean

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!!!

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

grg99Connect With a Mentor Commented:
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.

msjammuConnect With a Mentor Commented:
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
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)
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

stefan73Connect With a Mentor Commented:
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){

void increment(int& var, const int& by_what){

(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.operator+(b)     (method version)
operator+(a,b)     (function version)


GFinsethConnect With a Mentor Commented:
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.
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.
aravindtjConnect With a Mentor Commented:

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.
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)
liseperuAuthor Commented:
thanks for your help everyone
i have finally understand

All Courses

From novice to tech pro — start learning today.