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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

what does "overloading operators" mean

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
liseperu
Asked:
liseperu
5 Solutions
 
msjammuCommented:
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
 
msjammuCommented:
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
 
grg99Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
stefan73Commented:
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
 
GFinsethCommented:
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
 
jhshuklaCommented:
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
 
aravindtjCommented:

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

:)
thanks
lis
0

Featured Post

Independent Software Vendors: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now