Polymorphism  «Prev  Next»
Lesson 9 Selection algorithm for function overloading
Objective Steps of overloaded function selection algorithm in correct order.

Steps to overload function in C++

Whichever overloaded function is to be invoked, the invocation argument list must be matched to the declaration parameter list according to the function selection algorithm:
  1. Use an exact match if found.
  2. Try standard type promotions.
  3. Try standard type conversions.
  4. Try user-defined conversions.
  5. Use a match to ellipsis if found.

Standard promotions are better than other standard conversions. These are conversions from
  1. float to double,
  2. bool to int,
  3. char to int,
  4. short to int,
  5. enum to int
Standard conversions also include pointer conversions.

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);
}

Example Demonstrating Function Overloading

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

Standard Type Conversions

An exact match is clearly best.
Casts can be used to force such a match. The compiler will complain about ambiguous situations. Thus, it is poor practice to rely on subtle type distinctions and implicit conversions that obscure the overloaded function that is called. When in doubt, use explicit conversions to provide an exact match. Signature matching has many subtleties and pitfalls. The current ANSI C++ standard document has over 50 pages of discussion and examples of the complexities of this topic.

SEMrush Software Target 9SEMrush Software Banner 9