Overloading functions (C++ only)
You overload a function name f by declaring more
than one function with the name f in the same scope.
The declarations of f must differ from each other
by the types and/or the number of arguments in the argument list.
When you call an overloaded function named f, the
correct function is selected by comparing the argument list of the
function call with the parameter list of each of the overloaded candidate
functions with the name f. A candidate function is a function that
can be called based on the context of the call of the overloaded function
name.
Consider a function
print, which displays an int.
As shown in the following example, you can overload the function print to
display other types, for example, double and char*.
You can have three functions with the same name, each performing a
similar operation on a different data type: #include <iostream>
using namespace std;
void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char* c) {
cout << " Here is char* " << c << endl;
}
int main() {
print(10);
print(10.10);
print("ten");
}The following is the output of the above example: Here is int 10
Here is float 10.1
Here is char* tenRelated information