0% found this document useful (0 votes)
37 views

Function Overloading

Polymorphism allows functions to have the same name but different implementations depending on the arguments passed, and is achieved through function overloading where a function name has multiple definitions that are differentiated by number and type of arguments. Function overloading implements polymorphism, reduces comparisons, and improves program efficiency by allowing the compiler to select the best match based on argument types during function calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Function Overloading

Polymorphism allows functions to have the same name but different implementations depending on the arguments passed, and is achieved through function overloading where a function name has multiple definitions that are differentiated by number and type of arguments. Function overloading implements polymorphism, reduces comparisons, and improves program efficiency by allowing the compiler to select the best match based on argument types during function calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Polymorphism

The polymorphism refers to ‘one name having many


forms’ ‘different behaviour of an instance
depending upon the situation’. C++ implements
polymorphism through overloaded functions and
overloaded operators. The term ‘overloading’ means
a name having two or more distinct meanings.
Thus, an ‘overloaded function’ refers to a function
having (one name and) more than one distinct
meanings. Similarly, when two or more distinct
meanings are defined for an operator, it is said to
be an ‘overloaded operator’.
Function Overloading
A function name having several definitions that are
differentiable by the number or types of their arguments.
OR
Function Overloading not only implements
polymorphism but also reduces number of comparisons
in a program and thereby makes the program run faster.
The key to function overloading is a function’s argument
list which is also known as the function signature
For example;
float divide (int a, int b);
float divide (float x, float y);
Function Overloading..contd
When a function name is declared more than once in a program,
the compiler will interpret the second (and subsequent)
declaration(s) as follows:
1. If the signatures of subsequent functions match the previous
function’s, then the second is treated as a re-declaration of the
first.
2. If the signatures of two functions match exactly but the return
type differ, the second declaration is treated as an erroneous re-
declaration of the first and is flagged at compile time as an error.
3. If the signature of the two functions differ in either the number
or type of their arguments, the two functions are considered to
be overloaded.

For example,
float square (float f);
double square (float x); // error
Steps Involved in Finding the Best Match
A call to an overloaded function is resolved to a particular
instance of the function through a process known as
argument matching, which can be termed as a process of
disambiguation. Argument matching involves
comparing the actual arguments of the call with the
formal arguments of each declared instance of the
function.
Contd.
Resolution rules (when substitution is used as conversion in
overloaded methods)

• If there is an exact match, execute that method.


• If there are more than one matching methods, execute the
method that has the most specific formal parameters.
• If there are two or more methods that are equally applicable,
the method invocation is ambiguous, so generate compiler error.
• If there is no matching method, generate compiler error.
•Sum(long b)
•Sum(int a)
•Sum(5)
Search for an Exact Match
If the type of the actual argument exactly matches
the type of one defined instance, the compiler
invokes that particular instance. For example,

void afunc(int); //overloaded function


void afunc(double);
afunc(0); //exactly match. Matches afunc(int)

0 (zero) is of type int , thus the call exactly matches


afunc(int).
A Match Through Promotion
If no exact match is found, an attempt is made to
achieve a match through promotion of the actual
argument.
Recall that the conversion of integer types (char,
short, int) into int (if all values of the type can
be represented by int) or into unsigned int (if all
values can’t be represented by int) is called
integral promotion.
Contd.
For example, consider the following code
fragment:
void afunc (int);
void afunc (float);
afunc (‘c’); //match through the promotion
matches void afunc (int)
A match through application of standard
C++ conversion rules

If no exact match or match through a promotion is


found, an attempt is made to achieve a match
through a standard conversion of the actual
argument. Consider the following example,
void afunc (char);
void afunc (double);
afunc (471); //match through standard conversion
matches afunc (double)
The int argument 471 can be converted to a double value 471
using C++ standard conversion rules and thus the function
call matches (through standard conversion) func(double).
But if the actual argument may be converted to multiple
formal argument types, the compiler wil generate an error
message as it will be ambiguous match. For example,
void afuns(int);
void afunc (long);
void afunc (double);
afunc(15); // error – ambiguous match

Here the int argument 15 can be converted either long or


double, thereby creating an ambiguous situation as to
which afunc() should be used.
Ambiguity..contd
void f(int x);
void f(int &x); // error
int I=5;
f(i);
two functions cannot be overloaded when
the only difference is that one takes a
reference parameter and the other takes a
normal, call-by-value parameter.
A match through application of a user-
defined conversion.
If all the above mentioned steps fail, then the
compiler will try the user-defined conversion in
the combinations to find a unique match.
Any function, whether it is a class member or just
an ordinary function can be overloaded in C++,
provided it is required to work for distinct
argument types, numbers and combinations.
Default Arguments and
Function Overloading
• Default arguments and function
overloading can give rise to an ambiguity
• Consider an overloaded function f where
one declaration has default arguments that,
if removed, makes the function look
identical to a second declaration, as in
If we call f(2), is it to f
1. int f(int x, int y=0); returns xy
in (1) or (2)?
2. int f(int x); // returns 2x
The 1st returns 1.
The 2nd returns 4.
Default Arguments and
Function Overloading (Contd.)
• If overloaded declarations of a function with
default arguments cause ambiguity, the
compiler gives an error message.
• It is the responsibility of the programmer not
to cause such ambiguities.
Exercise-
• Find area of different shapes using function
overloading, with class.

You might also like