Networking, Programming and Graphics - Tutorials
ONLINEHOWTO.net Tutorials Category

Function overloading - C++

Type: Code Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Level: Expert Networking, Programming and Graphics - Tutorials Networking, Programming and Graphics - Tutorials Networking, Programming and Graphics - Tutorials 
Networking, Programming and Graphics - Tutorials
Date: 2010-Feb-18
Networking, Programming and Graphics - Tutorials
Visited: 56786 times
Networking, Programming and Graphics - Tutorials
Rating: Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Published: Stephen Think

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 num1int 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 number25
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.
Rate this tutorial:                    
Post Comment

    • ( youandashish [ at ] gmail . com ),

      posted on 2009-Jun-24 | 06:59:00 AM
      i want many programms in that function overloding in c
    • ( nagendraji534 [ at ] yahoo . com ),

      posted on 2009-Jul-22 | 12:33:01 AM
      function overloaing
    • ( arun . aruninwwb [ at ] gmail . com ),

      posted on 2009-Oct-10 | 08:54:53 PM
      Thanks to this tutorial i like it and i understood function overloading easily
    • ( tariq00001 [ at ] gmail . com ),

      posted on 2009-Oct-11 | 07:19:36 AM
      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".

    • posted on 2009-Oct-11 | 10:21:11 AM
      Thanks for the note. Fixed.
    • ( bhushan . backagain [ at ] gmail . com ),

      posted on 2009-Oct-23 | 07:43:45 AM
      its right way to explain
    • ( n . nambi [ at ] yahoo . com ),

      posted on 2009-Oct-27 | 12:29:26 AM
      thank you sir
    • ( befebeku [ at ] yahoo . com ),

      posted on 2009-Oct-27 | 11:54:09 PM
      i don't understand how two functions having two parameters with same type can overload eath other using order only.because it says if " ...the functions will differ at least in any one of the these..."
    • ( leang_bunchhai [ at ] yahoo . com ),

      posted on 2009-Nov-14 | 07:07:43 AM
      i want to have soft code c++
    • ( leang_bunchhai [ at ] yahoo . com ),

      posted on 2009-Nov-14 | 07:08:09 AM
      soft code C++
    • ( lovejeet0707 [ at ] gmail . com ),

      posted on 2009-Nov-23 | 09:03:01 PM
      Give me Some main and important programs of C++

    • posted on 2009-Dec-21 | 12:50:13 AM
      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?
      if you could help me with these Qs plz contact me via: saba.t.beautiful@gmail.com
    • ( zelekeyalewm [ at ] yahoo . com ),

      posted on 2010-Jan-05 | 11:30:59 PM
      continue your tutorials.
    • ( aneeshasdas [ at ] gmail . com ),

      posted on 2010-Feb-12 | 09:35:20 AM
      thanks a lot!
    • ( srikanta . basant [ at ] gmail . com ),

      posted on 2010-Feb-14 | 02:51:41 AM
      I need more program upon this function overloading and their dray-run
      and also thanks to provide this kind of education and information facility
    • ( yogebalu [ at ] gmail . com ),

      posted on 2010-Feb-16 | 09:17:57 PM
      very useful to learn......
    • ( vavilala . suresh [ at ] gmail . com ),

      posted on 2010-Feb-18 | 10:44:34 AM
      Excellent Explanation
    • ( fairy_mady [ at ] hotmail . com ),

      posted on 2010-Mar-06 | 07:38:20 AM
      thx alooooooooooooooooot vry informative tutorial..
    • ( nensigup8 [ at ] gmail . com ),

      posted on 2010-Mar-22 | 01:46:03 AM
      their is a very small quantity of explaination.this can be explained much better
    • ( gunjan . solanki1 [ at ] gmail . com ),

      posted on 2010-Mar-26 | 06:49:48 AM
      easy language.........
    • ( patilsiddharth8 [ at ] gmail . com ),

      posted on 2010-Mar-26 | 09:29:29 PM
      A perfect, simple and easy to apply Way to explain the logic of function overloading...
    • ( lv . westlife [ at ] yahoo . com . cn ),

      posted on 2010-Mar-29 | 12:35:22 AM
      great teaching!!
      thank you !
    • ( saikiran . 1017 [ at ] gmail . com ),

      posted on 2010-Mar-30 | 02:58:15 AM
      thank u very much
    • ( prabha . yalama [ at ] gmail . com ),

      posted on 2010-Mar-31 | 06:25:42 AM
      explanation is very clear
    • ( gourav [ at ] search-value . com ),

      posted on 2010-Apr-04 | 09:37:16 AM
      The example which u have provided is very simple and easy to understand for the concept of function overloading
    • ( www . abhijeet44gemini [ at ] yahoo . com ),

      posted on 2010-Apr-04 | 04:30:11 PM
      The program is quiet simple n easily understand
    • ( www . abhijeet44gemini [ at ] yahoo . com ),

      posted on 2010-Apr-04 | 04:31:01 PM
      nice!!!!!!!!!!!
    • ( chaitalipmistry [ at ] gmail . com ),

      posted on 2010-Apr-04 | 09:22:54 PM
      very simple and good exemple...
    • ( leonliangyu [ at ] yahoo . com ),

      posted on 2010-Apr-06 | 12:27:03 PM
      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"
    • ( stephen [ at ] onlinehowto . net ),

      posted on 2010-Apr-07 | 04:43:37 AM
      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.
    • ( naresh . thakur1987 [ at ] gmail . com ),

      posted on 2010-Apr-08 | 12:16:59 PM
      you should declare more function to show function overloading. like a function which is returning a value with different number of arguments.

    • posted on 2010-Apr-12 | 12:12:51 AM
      a very neat example

    • posted on 2010-Apr-16 | 08:10:35 AM
      Perfect Stephen.Thanks a lot.Its of great help to refresh the concepts.
    • ( klubbie [ at ] gmail . com ),

      posted on 2010-Apr-17 | 05:02:11 PM
      very good tutorial..thankxxxx..!!
    • ( sameer_kaulus [ at ] yahoo . com ),

      posted on 2010-May-16 | 04:26:02 AM
      ..i found it very interesting to forget even the simpler concepts... thanx 4 d BRAIN WASHHHHHHHHHH......
    • ( adnan_ali1991 [ at ] yahoo . com ),

      posted on 2010-May-17 | 09:07:34 AM
      this help me a lot to prepare my presentation at this topic. thank you for provoiding this
    • ( melkamu . ademe [ at ] yahoo . com ),

      posted on 2010-May-18 | 02:24:52 PM
      do it sample exampel
    • ( liet . kashifraza [ at ] gmail . com ),

      posted on 2010-May-28 | 11:31:50 AM
      how a function overload worked

    • posted on 2010-Jun-10 | 05:44:02 AM
      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
    • ( remy_ig [ at ] yahoo . com ),

      posted on 2010-Jul-16 | 12:26:58 AM
      Clear Explanation.
      Clear Examples.
    • ( priya_alices [ at ] yahoo . in ),

      posted on 2010-Jul-26 | 06:15:38 AM
      can u please tell me many complicated programs that involve a function overloading
    • ( priya_alices [ at ] yahoo . in ),

      posted on 2010-Jul-26 | 06:16:57 AM
      why we use using namespace std ,whats the purpose behind it mainly
    • ( milan99 . anuradha [ at ] gmail . com ),

      posted on 2010-Aug-02 | 08:24:01 PM
      class overloading char
    • ( inglesayali367 [ at ] gmail . com ),

      posted on 2010-Aug-11 | 06:50:59 AM
      very useful..
      need many programs in func overloading
    • ( vkishore221 [ at ] gmail . com ),

      posted on 2010-Aug-12 | 12:02:13 AM
      function overloading not depend on return type. two functions return type may be differ (not same)
    • ( mm [ at ] xyz . com ),

      posted on 2010-Aug-18 | 10:13:39 PM
      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.

    • posted on 2010-Aug-24 | 10:06:09 PM
      THANKS FOR VISITING
    • ( geetabharatiyemul [ at ] gmail . com ),

      posted on 2010-Aug-26 | 08:34:12 PM
      It is Very good. It is easy to
      understand.
    • ( ajaypanchwagh2 [ at ] gmail . com ),

      posted on 2010-Aug-27 | 10:56:34 PM
      very good and clear and lucid
    • ( ajaypanchwagh2 [ at ] gmail . com ),

      posted on 2010-Aug-27 | 10:57:07 PM
      good
Need a specific tutorial? Do not hesitate and submit a request!
Your e-mail: