by Dinesh Thakur Category: Functions

In some cases when a similar action is to be performed on different types of data, different functions having different names are to be defined for all types of data. This makes the program very complex as the programmer must keep a track of the names of all the functions defined in the program. To prevent such situations, C++ allows the functions to be overloaded.

Overloading affirms the role of a single entity for multiple tasks. Function overloading is a type of polymorphism that allows multiple functions to share the same name with different parameters. The compiler identifies the function either on the basis of the number of parameters, the data type of the parameters or the order of the data type of the parameters passed to the function. Moreover, functions with different return type but with similar function signature are not considered as overloaded functions.

To understand the concept of function overloading, consider these function declarations.

void func (int) ;

void func (int, int) ;

In these statements, the two functions named, func () are different as the number of arguments passed are different. Now, consider these statements.

void func (int) ;

void func (char) ;

In these statements, the two functions named, func () are different as the data type of arguments passed is different. Now, consider these statements.

void func (int , float) ;

void func (float, int) ;

In these statements, two functions named, func () are different as the order of the data type of the arguments passed is different.

The compiler follows these steps to perform a comparison between the actual and the formal arguments to find the best match (the most appropriate overloaded function).

• Exact match: If the number and type of the arguments exactly match the number and type of parameters of anyone of the overloaded functions then that function is called.

• Match through type promotions: If an exact match is not found, the compiler tries to promote the type of the argument to the type of the parameter of an overloaded function. For example, if the argument is of type char, the compiler promotes it to type int or to the equivalent type unsigned into

• Match through standard conversions: If a match is also not found after performing type promotions, the compiler tries to convert the type of the argument to the type of the parameter through standard conversion rules. For example, if the argument is of type int, the compiler can convert it to type float, double or long double.

• Match through user-defined conversions: If a match is also not found after performing standard type conversions, the compiler tries to convert the type of the argument to the type of the parameter through the user-defined conversions.

• Match through ellipsis: If a match is not found after performing the previous steps, the compiler tries to find an ellipsis in the definition (or declaration) of any of the overloaded functions.

To understand the concept of function overloading, consider this example.

 

Example : A program to demonstrate the concept of function overloading

#include<iostream>

#include<math>

using namespace std;

int area (int);    //function prototype 1

float area (float, float); //function prototype 2

float area (float, float, float); //function prototype 3

int main ()

{

int side;

float length, width, a, b, c;

cout<<"Enter side of a square: ";

cin>>side;

cout<<"Area of a square is : "<<area (side)<<endl;

cout<<"Enter length and width of a rectangle: ";

cin>>length>>width;

cout<<"Area of a rectangle is :"<<area(length,width)<<endl;

CQut<<"Enter three sides of a triangle: ";

cin>>a>>b>>c;

cout<<"Area of a triangle is : "<<area(a, b, c)<<endl;

return 0;

}

int area(int s)

{

return (s*s);

}

float area(float len, f]oat wid)

{

return (len*wid);

}

float area(float a, float b, float c)

{

float s,area;

s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c)) ;

return area;

}

 

The output of the program is

 

Enter side of a square : 12

Area of a square is : 144

Enter length and width,of a rectangle: 34.5 67.7

Area of a rectangle is : 2335.65

Enter three sides of a triangle: 12.5 20.5 25.5

Area of a triangle is : 126.791

 

In this example, three functions are declared with the same name area (),however, accepting different arguments. The compiler examines the number, type of arguments or order of data type in the function call and calls the appropriate function.

In some situations, when only the number of arguments is different then instead of function overloading, default arguments can be used. This is because default arguments combine different operations into one function and hence, reduce the overhead of writing multiple functions. To understand this concept, consider this example.

Example : A program to demonstrate the use of default arguments as an alternative to function overloading

 

#include<iostream>

using namespace std;

void disp(char= '*',int=4);

int main ()

{

disp ();

disp ( ‘^’) ;

disp ('@' ,5) ;

return 0;

}

void disp(char ch, int n)

{

for(int i=0; i<=n; i++)

{

for(int j=0; j<I; j++)

{

Cout<<ch;

Cout<<" ";

}

Cout<<endl;

}}

 

The output of the program is

*

* *

* * *

* * * *

^

^^

^^^

^^^^

@

@@

@@@

@@@@

@@@@@

In this example, three calls with different number of arguments are made to the same function void disp (char='*’,int=4) . In this way, default arguments are used to reduce the number of functions and hence, serve as an alternative to function overloading.





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles