Function overloading – C++
|
|
In this tutorial we will try to explain what the term function overloading is.
A function is overloaded when same name is given to different function. However, the two functions with the same name will differ at least in one of the following.
a) The number of parameters
b) The data type of parameters
c) The order of appearance
These three together are referred to as the function signature.
For example if we have two functions :
void foo(int i,char a);
void boo(int j,char b);
Their signature is the same (int ,char) but a function
void moo(int i,int j) ; has a signature (int, int) which is different.
While overloading a function, the return type of the functions need to be the same.
In general functions are overloaded when :
1. Functions differ in function signature.
2. Return type of the functions is the same.
Here s a basic example of function overloading
-
#include <iostream>
-
using namespace std;
-
-
class arith {
-
public:
-
void calc(int num1)
-
-
{
-
cout<<"Square of a given number: " <<num1*num1 <<endl;
-
}
-
-
void calc(int num1, int num2 )
-
-
{
-
cout<<"Product of two whole numbers: " <<num1*num2 <<endl;
-
}
-
};
-
-
-
int main() //begin of main function
-
{
-
arith a;
-
a.calc(5);
-
a.calc(6,7);
-
}
I will explain what we did in the function overloading example.
First the overloaded function in this example is calc. If you have noticed we have in our arith class two functions with the name calc. The fist one takes one integer number as a parameter and prints the square of the number. The second calc function takes two integer numbers as parameters, multiplies the numbers and prints the product. This is all we need for making a successful overloading of a function.
a) we have two functions with the same name : calc
b) we have different signatures : (int) , (int, int)
c) return type is the same : void
The result of the execution looks like this
-
Square of a given number: 25
-
Product of two whole numbers: 42
The result demonstrates the overloading concept. Based on the arguments we use when we call the calc function in our code :
-
a.calc(5);
-
a.calc(6,7);
The compiler decides witch function to use at the moment we call the function.
I hope that everything about function overloading in C++ is clear now. If you haven\”t got that I recommend to go buy some C++ book on Amazon.com.

i want many programms in that function overloding in c
I think function overloading is not available in C
function overloaing
Thanks to this tutorial i like it and i understood function overloading easily
I am so many confuse by seeing above example. There is something is wrong in above example..
That is you wrote the “count” instead of the “cout”.
Thanks for the note. Fixed.
its right way to explain
thank you sir
i want to have soft code c++
soft code C++
Give me Some main and important programs of C++
continue your tutorials.
thanks a lot!
I need more program upon this function overloading and their dray-run
and also thanks to provide this kind of education and information facility
very useful to learn……
Excellent Explanation
thx alooooooooooooooooot vry informative tutorial..
their is a very small quantity of explaination.this can be explained much better
easy language………
A perfect, simple and easy to apply Way to explain the logic of function overloading…
great teaching!!
thank you !
thank u very much
explanation is very clear
The example which u have provided is very simple and easy to understand for the concept of function overloading
The program is quiet simple n easily understand
nice!!!!!!!!!!!
very simple and good exemple…
Is the return type must be the same of overloaded functions? I saw some website describe as: “The parameter list or type or the number must be different, or different return types”
Hello Leon, the return type in must be the same if we are talking about overloading in C++. I know that in some other programming languages you can do function overloading with only the return type.
you should declare more function to show function overloading. like a function which is returning a value with different number of arguments.
a very neat example
Perfect Stephen.Thanks a lot.Its of great help to refresh the concepts.
very good tutorial..thankxxxx..!!
..i found it very interesting to forget even the simpler concepts… thanx 4 d BRAIN WASHHHHHHHHHH……
this help me a lot to prepare my presentation at this topic. thank you for provoiding this
do it sample exampel
how a function overload worked
really sir you can give the ans in very simple or standard language with eg of program this is the very good method beneficial for student
Clear Explanation.
Clear Examples.
can u please tell me many complicated programs that involve a function overloading
why we use using namespace std ,whats the purpose behind it mainly
class overloading char
very useful..
need many programs in func overloading
function overloading not depend on return type. two functions return type may be differ (not same)
The return type of the function can differ in the overloaded functions. any point ot note, the function overloading does not depend on the return type of the function.
THANKS FOR VISITING
It is Very good. It is easy to
understand.
very good and clear and lucid
good
it can be better…..
Its easy to learn function overloading by this tutorial….thanx
i want program of binary operator using function overloading
it is too worst to explain the overloading functionality…where is the above example explaining..
i never understand the concept of function overloading. But this examlpe helped me too much and i understand the concept of function overloading
,,i want some more exAmplE of Function oVerloAding,,AnyWay,,tnX,,
its good site for studing c+++
thank u …….
Please i want more programs and guiding exaples from c++
i need what are all the demerits and restrictions associated with function overloading.
i want many programs in overloading
good overloading vivid
i want many programms in c++ by using function overloading
Thanks guys this gave me a clearer understanding of function overloading using a class.
i want more example in function overloading
thank u sir…
thanx for your lesson i have appreciated plz but the program writtten can not run why?
thankx sir but i request plz i want many programms in that funtion overloading in c
excellent.it is very clear now.thank you
by seeing above examle
m getting confised.
give me more examle of function overloading
i want more programs related to function overloading
using namespace std;
class arith {
public:
void calc(int num1)
{
}
cout<<“Square of a given number: ” <
void calc(int num1, int num2 )
{
}
cout<<“Product of two whole numbers: ” <
};
int main() //begin of main function
{
arith a;
a.calc(5);
a.calc(6,7);
}
I will explain what we did in the function overloading example.
First the overloaded function in this example is calc. If you have noticed we have in our arith class two functions with the name calc. The fist one takes one integer number as a parameter and prints the square of the number. The second
i want the program for (a-b)^2=a^2+b^2-2ab using overloading concept in c++
I would like to thank the organising team who took lead in arranging all this toturials and making them avaible on the internet.
you should declare more function to show function overloading. like a function which is returning a value with different number of arguments.
program of function overloading
//usins porator.
i make number vof programe overloading
send mathticla program
concept expressed clearly in simple manner.
i still have 2 questions!
1.what’s the use of overloading?
2.are their functions (i mean what they do)the same as eatch other? or they can be tow completely different functions only with the same name?
@oil change miami, the idea in general is to do the same but over a different set(type) of parameters. One of the most commonly used examples is a function to calculate the area but of a different geometrical figures
good tutor! will b with u!
‘Function Overloading by Return Type in C++’
Another reference using the cast operators
and other object-oriented programming concepts
to accomplish this _with the same syntax_
can be found here: [1].
[1]: http://mfoliveira.org/blog/2011/03/12/overload-by-return-type-in-c-plus-plus/
Give me Some main and important programs of C++
i need a simple program for method overloading
Thank you for sharing to us.there are many person searching about that now they will find enough resources by your post.I would like to join your blog anyway so please continue sharing with us
create one class with one variable and variable value as something. if in void main we create one object and print the object. what happen.
However, the two functions with the same name will differ at least in one of the following.
I’ve read through a number of the articles in your website , and I love the way you blog. I included it to my favorites blog site list and will also be checking quickly.
cn u help me in program,,.. overload a function called getmax providin 2 parameters 1st ov int n 2nd ov char,,. n takes input 4m da user n displays da greater ov dm..
plz do answer me,…
Hello aliza, the task is not very clear to me. Could you provide example input and example output of the two functions.
I truly like your posting as well as the webpage all in all! The article is incredibly clearly written and also without difficulty understandable.
abe clear cut likhna aise likhega to kisiko nahi samjhega.
samjha be gandu
kya dost;
abe saale kuch bhi fekte rehta hai.
kuch padhai kar le or jarurat hai tere ko usk baad batana kya hota hai FUNCTION OVERLOADING……
abe or ek chiz batana TRUCK OVERLOADING….
thanks that guide and function overload i will understand but main program i will not understand
good tutorial
Hi All,
Will the function name is also included in signature or not for function overloading.
For function overloading, the function names must be same.