C++ Learning Resource Network!

 
 
 C++ Tutorials  >  Functions  >  Overloading Functions in C++
 

Function overloading is the process of using the same name for two or more functions. Each redefinition of a function must use different type of parameters, or different sequence of parameters, or different number of parameters. The number, type and sequence of parameters for a function is called the function signature.

The following program illustrates a function using different type of parameters:

//Program 7.9
//This program calculates sum of two numbers
#include<iostream.h>
int add(int iNum1, int iNum2)
{
return iNum1+iNum2;
}
float add(float fNum1, float iNum1)
{
return fNum1+iNum1;
}

void main()
{
cout<<add(20, 30)<<endl; //The function add(int, int) is called
float i= 9.6;
float b= 8.8;
cout<<add(i, b)<<endl; //The function add(float, float) is called
}


Two functions differing only in their return type cannot be overloaded. For example, overloading int add(int, int) and float add(int, int) would flag an error.

 
 

| Bookmark This Site |   | Links |   | Search |   | Submit |   | Contact |

  Copyright 2008 - 3008  CppTutor.com All rights reserved.