Ad Hoc Polymorphism  «Prev 

Selection Algorithm for Function Overloading

Explicitly casting arguments can be both an aid to documentation and a useful way to avoid poorly understood conversion sequences.
It is not an admission of ignorance to cast or parenthesize arguments or expressions that could be converted or evaluated properly otherwise.
Function overloading is the practice of declaring the same function with different signatures.
The same function name will be used with different number of parameters and parameters of different type. But overloading of functions with different return types are not allowed.
For example in this C++ Tutorial let us assume an AddAndDisplay function with different types of parameters.

//C++ Tutorial - Sample code for function overloading
void AddAndDisplay(int x, int y){
  cout << " C++ Tutorial - Integer result: " << (x+y);
}
void AddAndDisplay(double x, double y){
  cout<< " C++ Tutorial - Double result: "<<(x+y);
}
void AddAndDisplay(float x, float y){
  cout<< " C++ Tutorial - float result: "<<(x+y);
}

Following code demonstrates function overloading.
#include <iostream>
using namespace std;
void repchar(); //declarations
void repchar(char);
void repchar(char, int);
int main()
{
repchar();
repchar('=');
repchar('+', 30);
return 0;
}

//--------------------------------------------------------------
// repchar()
// displays 45 asterisks
void repchar()
{
for(int j=0; j <45; j++) // always loops 45 times
cout << '*''; // always prints asterisk
cout << endl;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
// repchar()
// displays 45 copies of specified character
void repchar(char ch)
{
for(int j=0; j <45; j++) // always loops 45 times
cout << ch; // prints specified character
cout << endl;
}
//--------------------------------------------------------------
// repchar()
// displays specified number of copies of specified character
void repchar(char ch, int n)
{
for(int j=0; j <n; j++) // loops n times
cout << ch; // prints specified character
cout << endl;
}
This program prints out three lines of characters. Here isthe output:
*********************************************
=============================================
++++++++++++++++++++++++++++++
The first two lines are 45 characters long, and the third is 30. The program contains three functions with the same name. There are three declarations, three function calls, and three function definitions. What keeps the compiler from becoming hopelessly confused?
It uses the function signature, the number of arguments, and their data types to distinguish one function from another.
In other words, the declaration void repchar(); which takes no arguments, describes an entirely different function than the declaration void repchar(char); which takes one argument of type char, or the declaration void repchar(char, int); which takes one argument of type char and another of type int.
The compiler, seeing several functions with the same name but different numbers of arguments, could decide the programmer had made a mistake (which is what it would do in C). Instead, it very tolerantly sets up a separate function for every such definition.
Which one of these functions will be called depends on the number of arguments supplied in the call. The figure below shows this process.
Overloaded Functions
Overloaded Functions