Function Overloading in C++
What is Function Overloading ?
A function name having several definitions that are
differentiable by the number or types of their arguments in known as
overloading of function.
We know that a function consists of a prototype and a body.
The protype consists of the function’s return type, function’s name and its
argument list (also known as function signature). Any two functions that happen
to have the same name but differ in their function signature, are said to be
overloaded. This difference in their signature may be due to changed types of
arguments or by changed number of arguments in the argument list or both.
Function overloading implements polymorphism, which is
a key feature of Object Oriented Programming (OOP). The ability of a function
to have same name but different behaviours depending upon the situation is considered
as polymorphism.
Also, using overloaded functions reduces number of comparisons
in a program and thereby makes the program execute faster.
Declaring & Defining Overloaded Functions
The key to function overloading is a function’s argument list
which is also known as the function signature.
It is the signature, not the function return type that enables function
overloading.
If two functions are having same number and types of arguments
in the same order, they are said to have the same signature. Even if they are
using distinct variable names, it doesn’t matter. For instance, following two
functions have same signature.
void squar (int a, float b);
void squar (int x, float y);
C++ lets you overload a function name provided the functions
with same name are having different signatures. The signature can differ in the
number of arguments or in the type of arguments, or both.
To overload a function name, all you need to do is – declare and
define all the functions with the same name but with different signatures, separately.
For instance, following code fragment overloads a function name square().
void square (int i);
void square (char c);
void square (float f);
void square (double d);
After declaring, you must define them separately. This defining
can also be done directly at the time of declaration.
When a function name is declared more than once in a program,
the compiler will interpret the second (and the rest) declarations as :
- if the signatures of subsequent functions match the previous function’s, then the second is treated as a re-declaration of the first. In that case the compiler raises error.
void sum (int a, int b);
void sum (int x, int y); // error;
- if the signatures of two functions match exactly but the return types differ, the second declaration is still treated as an erroneous re-declaration of the first and is flagged at compile time as an error.
int square (float f);
float square (float x); // error;
Functions with the same signature but different return types are
not allowed in C++. You can have different return types, but only when the
function signatures are different.
- if the signatures of two functions (having same name) differ, the two functions are considered to be overloaded.
NOTE : While considering the function signature, int and unsigned
int or int and const int,
etc are considered different datatypes and so functions with arguments with
such differences are considered overloaded.
Read more >> Restrictions on Overloaded Functions.
Calling of
Overloaded Functions
Overloaded functions are called just like other functions. The
number and type of arguments determine which function should be invoked. A
function call first matches the prototypes available with the number and type
of arguments provided with the function call and calls the appropriate function
for execution.
Read more >> Steps involved in finding the Best Match.
Let us see a demo program in C++
Output screen :
Function 2 called
Function 1 called
Function 3 called
If you find something wrong or if you
wish to add some information in this article, feel free to comment on this post
or contact us at coding.nkcoder@gmail.com.
Which topics you want an article on? Comment down on this
post.
Do not stop here. Read all articles related to C++ here.
If you want to contribute any article for the website, feel
free to share it on coding.nkcoder@gmail.com. We will
publish it on the website with your name.
Post a Comment