Saturday, 28 April 2018

Function Overloading in C++ with example program

Function Overloading in C++: In some programming languages, function overloading or method overloading is the ability to create multiple methods of the same name with different implementations in the same scope. Such implementations are more useful to do different task with same function.

Using the concept of function overloading; we can design a family of functions with one function name but with different argument lists. The function would perform different operations depending on the argument list in the function call. The correct function to be invoked is determined by checking the number of the arguments but not on the function type.

For example, consider the following c++ program where the function add() is overloaded.

int add(int, int);
int add(int,int,int);
double add(double, double);
int main()
return 0;
int add(int a, int b)
int add(int a, int b, int c)
double add( double x, double y)

How overloaded function are executed?

         A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique. It not matched arguments list is found, it tries to perform type conversion otherwise it generate an error message.

Overloading Considerations

Function Declaration ElementUsed for Overloading?
Function return typeNo
Number of argumentsYes
Type of argumentsYes
Presence or absence of ellipsisYes
Use of typedef namesNo
Unspecified array boundsNo
const or volatile (see below)Yes 

Although functions can be distinguished on the basis of return type, they cannot be overloaded on this basis. Const or volatile are only used as a basis for overloading if they are used in a class to apply to the this pointer for the class, not the function's return type. In other words, overloading applies only if the const or volatile keyword follows the function's argument list in the declaration.


2. OOPs with C++ E Balagurusamy 


Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

python tutorial


C Programming


Java Tutorial


Data Structures


MS Office


Database Management