Function overloading in C++

By: Babbar Ankit Emailed: 1605 times Printed: 2077 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.

Example

#include<iostream>
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)
{
if(num>=0)
return num;
else
 return (-num);
}
long abslt(long num)
{
if(num>=0)
return num;
else return (-num);
}
float abslt(float  num)
{
if(num>=0)
return num;
else return (-num);
}
double abslt(double  num)
{
if(num>=0)
return num;
else return (-num);
}

OUTPUT
 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: http://www.botskool.com/programming-tutorials


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

Comments(8)


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++ )
cin.ignore() in C++
Using cout.width() in C++
Dot (.) vs Arrow (->) to access data members in C++
strcat() and strncat() sample program in C++
Calculate average using Two-Dimensional Array in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Specifying default values to function parameters in C++
What Is a Pointer in C++?
The indirection operator (*) - dereference operator.
C++ Sample Program for Allocating, using, and deleting pointers.
strlen() sample program in C++
assert() example program in C++
Getting User Input Using cin in C++
Using peek() and putback() with cin in C++
Most Emailed Articles (in C++)
Constructors and Destructors in C++
Vector variables in C++
Programming errors a compiler will detect in C++
Uninitialized Variables Cause Run-Time Problems in C++
C++ and Object-Oriented Programming
Converting Pointer Parameters in C++ to Java
Operator Precedence in C++
Pass by Reference in C++ functions
for loop in C++
Compiling and Linking Multiple Source Files in C++
Calculate average using Two-Dimensional Array in C++
Matrix using nested for loops in C++
Calculating total based on the given quantity and price in C++
The if Statement in C++
do...while Loops in C++