Function overloading in C++

By: Babbar Ankit Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

C++ permits the use of two function with the same name. However such functions essentially have different argument list. The difference can be in terms of number or type of arguments or both.

This process of using two or more functions with the same name but differing in the signature is called function overloading.

But overloading of functions with different return types are not allowed.

In overloaded functions , the function call determines which function definition will be executed.

The biggest advantage of overloading is that it helps us to perform same operations on different datatypes without  having the need to use separate names for each version.


using namespace std;

int abslt(int );
long abslt(long );
float abslt(float );
double abslt(double );

int main()

int intgr=-5;
long lng=34225;
float flt=-5.56;
double dbl=-45.6768;
cout<<\" absoulte value of \"<<intgr<<\" = \"<<abslt(intgr)<<endl;
cout<<\" absoulte value of \"<<lng<<\" = \"<<abslt(lng)<<endl;
cout<<\" absoulte value of \"<<flt<<\" = \"<<abslt(flt)<<endl;
cout<<\" absoulte value of \"<<dbl<<\" = \"<<abslt(dbl)<<endl;
int abslt(int num)
return num;
 return (-num);
long abslt(long num)
return num;
else return (-num);
float abslt(float  num)
return num;
else return (-num);
double abslt(double  num)
return num;
else return (-num);

 absoulte value of -5 = 5
 absoulte value of 34225 = 34225
 absoulte value of -5.56 = 5.56
 absoulte value of -45.6768 = 45.6768

the above function finds the absolute value of any number int, long, float ,double.

In  C, the above is implemented as a set of different function abs()-for int, fabs()-for double ,labs()-for long.

The use of overloading may not have reduced the code complexity /size but has definitely made it easier to understand and avoided the necessity of remembering different names for each version function which perform identically the same task.

Authors Url:

C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

practical example of funcation overloding

View Tutorial          By: yogesh at 2010-12-19 08:39:06
2. View Comment

Its very useful to me

View Tutorial          By: suba at 2011-07-04 11:21:19
3. View Comment

What's with the \ " ?
Your code doesn't even compile with that.

View Tutorial          By: Dan at 2011-12-29 10:20:08
4. View Comment

why we use int with main

View Tutorial          By: praveen at 2012-03-26 04:28:20
5. View Comment

why return&else statment repeating?

View Tutorial          By: sudhar at 2012-07-12 14:50:45
6. View Comment

through this i understand functional overloading easily and my concept is also cleared thanku...........

View Tutorial          By: mahwash at 2012-09-19 15:29:21
7. View Comment

thanks brother i hope this is most important for every one thanks for lot.

View Tutorial          By: manglesh upadhyay at 2012-09-26 04:40:41
8. View Comment

This example of function overloading is easy for Understanding.

View Tutorial          By: Pranay Gaikwad at 2012-10-30 14:02:15

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Babbar Ankit
Call by reference in C++ Functions
Default arguments in C++
Function overloading in C++

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
The Stack and the Free Store in C++
Getting User Input Using cin in C++
Function overloading in C++
Calculating total based on the given quantity and price in C++
Using Comments in a C++ Program
do...while Loops in C++
for Loops in C++
Using switch Statements in C++
The indirection operator (*) - dereference operator.
Dot (.) vs Arrow (->) to access data members in C++
Converting Pointer Parameters in C++ to Java
Converting C++ Abstract Classes into Java Interfaces
Converting C++ Multiple-Inheritance Hierarchies to Java
C++ Destructors Versus Java Finalization
Operator Precedence in C++
Most Emailed Articles (in C++)
Demonstrating global and local variables in C++
C++ and Object-Oriented Programming
Using cout in C++
Use of Conditional (Ternary) Operator in C++
const Member Functions in C++
Classes with Other Classes as Member Data in C++
What Is a Pointer in C++?
Manipulating Data by Using Pointers
Stray or Dangling Pointers in C++
C++ Destructors Versus Java Finalization
atoi(), itoa() in C++
strcat() and strncat() sample program in C++
Using #define, The Preprocessor and the Compiler in C++
Getting User Input Using cin in C++
Pass by Reference in C++ functions