Networking, Programming and Graphics - Tutorials
ONLINEHOWTO.net Tutorials Category
Simple PHP, Development, PHP Programming lessnos

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: 38479 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..
Need a specific tutorial? Do not hesitate and submit a request!
Your e-mail: