Function Overloading

12
STATIC BINDING OR EARLY BINDING FUNCTION OVERLOADING

Transcript of Function Overloading

Page 1: Function Overloading

STATIC BINDING OR EARLY BINDING

FUNCTION OVERLOADING

Page 2: Function Overloading

To see why function overloading is important,

first consider three functions defined by the C

subset: abs(), labs(), and fabs(). The abs()

function returns the absolute value of an integer,

labs() returns the absolute value of a long, and

fabs() returns the absolute value of a double.

Although these functions perform almost

identical actions, in C three slightly different

names must be used to represent these essentially

similar tasks.

Page 3: Function Overloading

A function is overloaded when same name is given to different function. However, the two functions with the same name will differ at least in one of the following.

a) The number of parameters b) The data type of parametersc) The order of appearance

These three together are referred to as the function signature.

Overloading functions with argument lists of the same types, based on return type alone, is an error.

Page 4: Function Overloading

1. int GetRandomValue();

2. double GetRandomValue();

But the compiler will flag this as an error. These two functions have thesame parameters (none), and consequently, the secondGetRandomValue() will be treated as an erroneous redeclaration of thefirst. Consequently, these functions will need to be given differentnames.

Declaring a typedef does not introduce a new type — consequently, the following the two declarations of Print() are considered identical:

1. typedef int no;

2. void Print(no szValue);

3. void Print(int szValue);

Page 5: Function Overloading

#include <iostream.h>

void print(int i) { cout << " Here is int " << i << endl; }

void print(double f) { cout << " Here is float " << f << endl; }

void print(char* c) { cout << " Here is char* " << c << endl; }

int main()

{ print(10);

print(10.10);

print("ten");

}

Page 6: Function Overloading

Making a call to an overloaded function results in one of three possible outcomes:

1. A match is found. The call is resolved to a particular overloaded function.

2. No match is found. The arguments can not be matched to any overloaded function.

3. An ambiguous match is found. The arguments matched more than one overloaded function.

Page 7: Function Overloading

First, C++ tries to find an exact match. This is the case

where the actual argument exactly matches the

parameter type of one of the overloaded functions. For

example:

1 void Print(char szValue);

2 void Print(int nValue);

Print(0); // exact match with Print(int)

Although 0 could technically match Print(char), it

exactly matches Print(int). Thus Print(int) is the best

match available.

Page 8: Function Overloading

If no exact match is found, C++ tries to find a match through

promotion.

Char, unsigned char, and short is promoted to an int.

Unsigned short can be promoted to int or unsigned int,

depending on the size of an int

Float is promoted to double

Enum is promoted to int

For example:

void Print(char szValue[ ]);

void Print(int nValue);

Print('a'); // promoted to match Print(int)

In this case, because there is no Print(char), the char ‘a’ is

promoted to an integer, which then matches Print(int).

Page 9: Function Overloading

If no promotion is found, C++ tries to find a match throughstandard conversion. Standard conversions include: Any numeric type will match any other numeric type, including

unsigned (eg. int to float) Enum will match the formal type of a numeric type (eg. enum to float) Zero will match a pointer type and numeric type (eg. 0 to char*, or 0

to float) A pointer will match a void pointer

For example: void Print(float fValue); void Print(struct sValue);

Print('a'); // promoted to match Print(float)

In this case, because there is no Print(char), and no Print(int),the ‘a’ is converted to a float and matched with Print(float).

Note that all standard conversions are considered equal. Nostandard conversion is considered better than any of the others.

Page 10: Function Overloading

void Print(unsigned int nValue);

void Print(float fValue);

//calls Print('a'); Print(0); Print(3.14159);

In the case of Print('a'), C++ can not find an exact match. It tries promoting ‘a’to an int, but there is no Print(int) either. Using a standard conversion, it canconvert ‘a’ to both an unsigned int and a floating point value. Because all standardconversions are considered equal, this is an ambiguous match.

Print(0) is similar. 0 is an int, and there is no Print(int). It matches both calls viastandard conversion.

Print(3.14159) might be a little surprising, as most programmers would assumeit matches Print(float). But remember that all literal floating point values aredoubles unless they have the ‘f’ suffix. 3.14159 is a double, and there is noPrint(double). Consequently, it matches both calls via standard conversion.

Page 11: Function Overloading

Default arguments versus Function

Overloading float amount(float principle, int time=2, float rate=0.08)

cout<<amount(3000); //1

cout<<amount(3000, 4); //2

cout<<amount(2500,5,01.2); //3

cout<<amount(2000, 0.13); //4

In case 4 even though middle argument time is skipped

but compiler makes no attempt at this type of

interpretation. C++ will consider 0.13 as 0 and will

invoke amount(2000,0) where time will be 0.

Page 12: Function Overloading

Default arguments versus Function

Overloading

Default arguments might not work for all possible

combinations of arguments whereas a function may be

overloaded for all possible combinations of arguments.

With function overloading, multiple function

definitions can be executed but with default arguments

exactly one function definition is executed.

By declaring an overloaded function, you save the

compiler from the trouble of pushing the default

argument value on the function call stack and save the

function from the trouble of testing the default value.