Sunteți pe pagina 1din 12

STATIC BINDING OR EARLY BINDING

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.
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 parameters
c) 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.
1. int GetRandomValue();
2. double GetRandomValue();
But the compiler will flag this as an error. These two functions have the
same parameters (none), and consequently, the second
GetRandomValue() will be treated as an erroneous redeclaration of the
first. Consequently, these functions will need to be given different
names.
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);
#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");
}
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.
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.
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).
If no promotion is found, C++ tries to find a match through
standard 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),
thea is converted to a float and matched with Print(float).
Note that all standard conversions are considered equal. No
standard conversion is considered better than any of the others.
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 can
convert a to both an unsigned int and a floating point value. Because all standard
conversions 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 via
standard conversion.
Print(3.14159) might be a little surprising, as most programmers would assume
it matches Print(float). But remember that all literal floating point values are
doubles unless they have the f suffix. 3.14159 is a double, and there is no
Print(double). Consequently, it matches both calls via standard conversion.
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.
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.

S-ar putea să vă placă și