Thursday, 25 August 2011

Function overloading

Function overloading: 

more than one functions can have the same name but either the number of (parameters)arguments or the data type of arguments has to be different. Return type has no role because function will return a value when it is called and at compile time compiler will not be able to determine which function to call. Function overloading is also known as compile time polymorphism.



FUNCTION OVERLOADING

Function overloading enables us to have more than one function with the same name. But their method signatures are different. Method signature of a function includes: the number of formal arguments, the sequence and type of arguments. It is important to notice that method signature doesn’t include return type. The following example makes it clear.

#include<iostream.h>
class A
{
public:
            void show();
void show(int);  // Function show is overloaded.
};

void A::show()
{
cout<<”Function Overloading”;
}

void A::show(int x)
{
for(int i=0,; i<x;i++)
cout<<”Function Overloading”;
}

void main()
{
A A1;
A1.show();     //First Definition is called
A1.show(3);  // Second Definition is called
}

Function overloading allows two classes to have the same name.

 

No comments:

Post a Comment