Function Overloading

In C++, functions can be overloaded. When a function is overloaded,two or more functions share the same name. However, each version of an overloaded function must have a different number and/or type of parameters. (The function return types may also differ, but this is not necessary.) When an overloaded function is called, the compiler decides which version of the function to use based upon the type and/or number of arguments, calling the function that has the closest match. For example, given these three overloaded functions,

void myfunc(int a) {  cout << "a is " << a << endl; } // overload myfunc void myfunc(int a, int b) {   cout << "a is " << a << endl;   cout << "b is " << b << endl; } // overload myfunc, again void myfunc(int a, double b) {   cout << "a is " << a << endl;   cout << "b is " << b << endl; }

the following calls are allowed:

myfunc(10); // calls myfunc(int) myfunc(12, 24); // calls myfunc(int, int) myfunc(99, 123.23); // calls myfunc(int, double)

In each case, the type and number of arguments determine which version of myfunc( ) is actually executed.

Function overloading is not supported by C.

C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539 © 2008-2017.
If you may any questions please contact us: