Friday, May 1, 2009

FUNCTION OVERLOADING

Function Overloading

Same function name and different argument is called function overloading


Over loading refers to the use of the same thing for different purposes. C++ also permits function name to create functions that perform a variety of different tasks. This is known as function polymorphism in OOP.

Using the concept of function overloading, we can design a family of function with one function name but with different argument lists. The function would perform different operation depending on the argument list in the function call. The correct function to be invoked is determined by checking the number and type of the argument but not on the function type. For example, an overloaded addition( ) function handles different types of data shown below:

Declaration

int addition(int a,int b) // prototype 1

int addition(int a, int b) // prototype 2

double addition(double x, double y); // prototype 3

double addition( int p, double q); // prototype 4

double addition(double p, int q); // prototype 5

Function Calls

cout<< style=""> // uses prototype 1

cout<< style=""> // uses prototype 4

cout<< style=""> // uses prototype 3

cout<< style=""> // uses prototype 2

cout<< style=""> // uses prototype 5

A function calls first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique. The function selection involves the following steps:

  1. The compiler first tries to find an exact march in which the types of actual arguments are the same and use that function.
  2. If an exact match is not found, the compiler uses the integral promotions to the actual arguments such as,

char to int

float to double

  1. When either of them fails, the compiler tries to use the built-in conversions (the implicit assignment conversions) to the actual arguments and then uses the function whose match is unique. If the conversion is possible to have multiple matches, then the compiler will generate an error message. Suppose we use the following two functions:

long square(long n)

double square(double x)

A function such as

square(10)

Will cause an error because int argument can be converted to either long
or double, thereby creating an ambiguous situation as to which version of square( )
should be used

  1. If all of the steps fail, that the compiler will try the user defined conversions in combination with integral promotions and build-in conversions to find a unique match. User defined conversions are often used in handling class objects.

Example:

header files

iostrem.h
conio.h
string.h

class swap
{
int a,b,c;
float x,y,z;
char p[10];
char ch,ch1;
public:
swap()
{
a=20;
b=40
}
swap(int x, int y)
{
a=x;
b=y;
}
swap(float a, float b, int aa)
{
x=a;
y=b;
c=aa;
}
swap(char s[10])
{
strcpy(p,s);
}
void display1()
{
cout<< a <<"\n" << b <<"\n";
}
void display1()
{
cout<< x <<"\n" << y <<"\n"<< c <<"\n";
}
void display1()
{
cout<< p;
}
};
void main()
{
clrscr();
swap obi();
swap obj1(6,15);
swap obj2(2.4,5.6,6);
swap obj3("god");
obj.display1();
obj1.display2();
obj2.display3();
obj3.display1();

}


No comments:

Post a Comment