1 / 17

Function Overloading. A function name having several definitions that are differentiable by the number or types of their arguments, is known as an overloaded function and this process is known as Function overloading. Function Overloading. Two functions have different no. of arguments

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

A function name having several definitions that are differentiable by the number or types of their arguments, is known as an overloaded function and this process is known as Function overloading.

Two functions have different no. of arguments

float area (float a)

{

return a*a;

}

float area (float a, float b)

{ return a*b;

}

Area(5.4);

Area(5.4, 6.3);

Two functions have different type of arguments

float area (float a, int b)

{

return a*b;

}

float area (float a, float b)

{ return a*b;

}

Area(5.4,10);

Area(5.4, 6.3);

Function overloading not only implements polymorphism but also reduce number of comparisons in a program and thereby makes the program run faster.

If the signatures of the two functions match the previous function’s then the second is treated as a re-declaration of the first

float area (float a,float b)

{

return a*b;

}

float area (float a, float b)

{ return a*b;

}

If the signatures of the two functions match exactly but the return types differ, the second declaration is treated as an erroneous re-declaration of the first and is flagged at compile time as an error.

float area (float a,float b)

{

return a*b;

}

int area (float a, float b)

{ return a*b;

}

Restrictions on overloaded functions number or type of their arguments, the two functions are considered to be overloaded

• Any two functions in a set of overloaded functions must have different argument list.

• Functions can not be overloaded on the bases of one being static and other non static

• Type def declaration don’t define different types

typedef char character

void print (char a)

void print (character a) both are same not overloaded

• ‘Array of’ and ‘pointer to’ taken as same

• Enumerated types are considered different.

Calling of overloaded functions number or type of their arguments, the two functions are considered to be overloaded

• These functions can be called like other functions.The no. & type of arguments determine which function should be invoked.

• For Example

float area (float a);

float area (float a, float b);

float area (float a, int b);

area(20.5); Ist fn will be called

area(20.5,10.2); IInd fn will be called

area(20.5,7); IIIrd fn will be called

Steps involved in finding Best Match number or type of their arguments, the two functions are considered to be overloaded1.Exact match

Void simple (int);

Void simple (double);

Simple(0); Found exact match

2 .promotion number or type of their arguments, the two functions are considered to be overloaded

If no exact match is found then attempt is made to achieve a match through promotion of actual arguments.

Void simple(int);

Void simple(float);

Simple(‘c’); here ‘c’ will be promoted to int type as no exact match is there.

3.Using C++ conversion rule number or type of their arguments, the two functions are considered to be overloaded

In this case an attempt is made to achieve a match by standard conversion of actual arguments.

Void simple(char);

Void simple(double);

Simple(471); here 471 will be converted to double

Void simple(long);

Void simple(double);

Simple(471); here confusion for compiler that 471 should be converted to long or double , so error will occur.

PRACTICE number or type of their arguments, the two functions are considered to be overloaded

A function printchar is defined as

Void printchar(char ch = ‘*’, int len=40)

{

For (int x=0; x<len; x++)

Cout<<ch;

}

Qn. How will you invoke the function printchar for following output:

• To print ‘*’ 40 times

• To print ‘*’ 20 times

• To print ‘=‘ 40 times

• To print ‘=‘30 times

Ans: number or type of their arguments, the two functions are considered to be overloaded

Printchar()

Printchar(‘*’, 20)

Printchar(‘=‘)

Printchar(‘=‘, 30)

• Default values to the function

Float area (float a=10.5, float b=20.5)

{ return a*b;

}

This fn can be called by:

Float x;

X=area();

X=area(12.3);

X=area(14.2,3.5);