function overloading

Hi ,
I am little bit confused over the fact as Why is function overloading used ?
When i googled the topic on function over the function overloading ,it only talks about function having the same name with different parameters . But i find nothing as why it is used ie i know what is the advantage of having the function with same name and having different parameters .
please let me know why is the function overloading is used for ?
Thanks in advance .
xxx
It makes it easier for me to program, by allowing me to use the same name for functions that do the same thing on different objects.

This also makes the code easier for someone else to read and understand.
It also makes template programming easier, since the same function can be called with different types.

A simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void func(int v)
{
  // do something as an int
}

void func(float v)
{
  // do something different with a float
}

template <typename T>
void DoStuff(T v)
{
  // do stuff that applies to both floats and ints here
  func( v );  // calls int or float version of func, to do type-specific stuff
}
I agree with the above, and here's another thing. Say you want to call a long equation on data multiple times in the program, on ints, floats, doubles,longs, etc. It would be possible to copy and paste the same equation, or you could have something like this:
1
2
3
4
5
6
7
8
9
10
11
12
void equation (int v)
{
//equation
}
void equation (float v)
{
//equation
}
int main ()
{
//stuff using the equation
}

That way, you can do the same thing (or nearly) no matter the type.
closed account (D80DSL3A)
You may wish to make a calculation using different sets of data, or construct an object given different data.

I'll give an example involving overloaded constructors.

I construct a path for an object to follow in a video game from a sequence of path segments, or legs (of the path). Drawing inspiration from writing linked lists I link the path segments together by giving each leg pointers to the previous and next legs (so the path may be traversed in either direction). Example below is for straight segments ( linLeg = linear leg )

Constructor for the 1st leg:
linLeg( float _x0, float _y0, float _xf, float _yf );// for 1st leg
Coordinates for the start and end points are given and prev = next = NULL; is assigned since there are no other legs yet.

Constructor for 2nd through next to last leg:
linLeg( Leg& prevLeg, float _xf, float _yf );// for mid Legs or dead end Leg
Here the begin point is taken as the end point of the previous leg and the endpoint is given.
The assignments prev =&prevLeg; and prevLeg.next = this; are made in the ctor to establish the leg linkage.

Constructor for the last connecting leg:
linLeg( Leg& prevLeg, Leg& nextLeg );// for last (connecting) Leg Here the enpoints are taken from the legs on either end and assignments are made to establish the full and final linkage.

I hope this long boring example was helpful.
Also operators are really functions.
Like the stream output operator << is actually a function
 
ostream& operator<<(ostream&,object&);

which is overloaded for different objects.

Handy that, you can write
 
cout << myint << mydouble << mystring;

without having to use different functions to output different things.
closed account (o1vk4iN6)
@Disch

Why not something like this?

1
2
3
4
5
6
7
8
9
10
11
12
template<typename T>
void funct(T v)
{
     // do something
}


template<>
void funct(float v)
{
     // do something float specific
}
Why not something like this?

You can do that as well. The difference between your example and Disch is that DoStuff does "stuff" to all types, a default operation, and then does special operations.
Template specialization is function overloading for templates.

Also syntax error on line 9, you need funct<float>
Thanks all ...
Topic archived. No new replies allowed.