Function overloading in C++
By: Babbar Ankit
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
Archived Comments
1. This example of function overloading is easy for Understanding.
View Tutorial By: Pranay Gaikwad at 2012-10-30 14:02:15
2. 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
3. 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
4. i felt some how better by reading this
View Tutorial By: janmejay barik at 2012-08-22 07:05:20
5. why return&else statment repeating?
View Tutorial By: sudhar at 2012-07-12 14:50:45
6. why we use int with main
View Tutorial By: praveen at 2012-03-26 04:28:20
7. What's with the \ " ?
Your code doesn't even compile with that.
View Tutorial By: Dan at 2011-12-29 10:20:08
8. very well
View Tutorial By: gowri at 2011-08-01 10:00:49
9. Its very useful to me
View Tutorial By: suba at 2011-07-04 11:21:19
10. practical example of funcation overloding
View Tutorial By: yogesh at 2010-12-19 08:39:06
11. i want this program:
Using function overloading to
(i). Increment the value of a varia
View Tutorial By: surya at 2010-10-18 01:12:16
12. Simply nice..
View Tutorial By: Sathish at 2010-09-06 04:43:24
13. it is very good continue it
View Tutorial By: nebil at 2010-06-06 09:20:58
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++