Function Overloading in C++
0 3597
Most of the time, it is a good idea to use different names for different functions but sometimes the set of functions are conceptually performing identical operations.
For example
int sub(int, int);
float subfloat(float, float);
We are already familiar with C language that if we have two functions. Then there will be two different names for those two functions. But in C++ we can use the same function name for n number of functions as long as the signature of the function is different.
Where, function signature includes names, no of parameters, type of parameters and order of parameters.
int add(int,int);
float add(float,float,float);
int add(int,int,int);
These are the three functions with different signatures. They all are basically adding values. These whole procedures when functions are conceptually doing the same task are called function overloading.
Now, lets now how the C++ compiler performs this function overloading through name mangling.
Name mangling is the internal process of the compiler that adds values after or before the name of the function.
For e.g.
The above functions might be stored internally as add-1, add-2, add-3 respectively this modification is done by the compiler internally before compilation to make sure that function names are actually different. i.e. The linker in c++ needs to mangle the function name to indicate which particular prototype it requires to call.
To prohibit certain function from being overloaded, in C++ there is extern "c" that is to be mentioned before function. i.e.
extern "c" int add(int, int); // it tells the compiler that this function is to be treated like C language and this function cannot be overload once it is marked extern "c".
A program to illustrate function overloading
#include <iostream>
using namespace std;
/* No of arguments are different */
void displays(char []); // print the string passed as argument
void displays(char [], char []);
int main()
{
char initial[] = "learn C in codingtag";
char final[] = "learn C++ in codingtag";
displays(initial);
displays(initial, final);
return 0;
}
void displays(char s[])
{
cout << s << endl;
}
void displays(char s[], char u[])
{
cout << s << endl << u << endl;
}
OUTPUT
learn C in codingtag
learn C in codingtag
learn C++ in codingtag
DEFAULT ARGUMENT IN C++
Suppose we are trying to write a simple program of the calculator in C++ in which we want to facilitate the functionality to add two numbers and after that we also want to extend that functionality or say that users can able to add three or more than three integers. This can also be performed with the help of function overloading i.e.
int add(int,int); // to add two integers
int add(int,int,int); // to add three integers
int add(int,int,int,int); to add more than three i.e. four integers
Instead of this method, there is one more method we can perform this task in C++. In this, we can assign default values to the last two parameters of the function. That is known as default arguments as in below example:
int add(int x1, int x2, int x3=0 ,int x4=0);
and if users want to make call i.e. y= add(6,9); then this 6 will be assigned to x1 and 9 will be assigned to x2 respectively and rest x3 and x4 will be by default 0. This function will return 15. If in another case where there is z=add(6,2,3); then 6 will be assigned to x1 and 2 will be assigned tox2 and 3 will be assigned to x3 respectively and x4 will be by default 0 and return 11 by adding three integers. For adding 4 integers s= add(2,4,5,3); again the similar process will apply on it also and return 14. Thus, three calls such as y= add(6,9); , z=add(6,2,3); , s= add(2,4,5,3); are possible with the same function.
The same function permits the users to add two integers, three integer or four integers with the use of default parameters in C++. There is a need to follow some rules while using default parameters which are given below:
1) Default arguments will be from right to left sequentially from the end. Breakage of the sequence will not be allowed.
2) Declare default argument values in function declarations.
Share:
Comments
Waiting for your comments