Class12 3 Function Overloading
Class12 3 Function Overloading
Function
Overloading
Introduction
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.
2
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.
For example;
float divide (int a, int b);
float divide (float x, float y);
Note:
A functions argument list (i.e., number and
type of argument) is known as the
functions signature.
4
//function 1
//same function
as that of
function 1
5
Void
prnsqr
(int i); prnsqr
Void
(char
c);
Void prnsqr (float
for
double
//error
9
signatures,
10
11
Restrictions on Overloaded
Functions
Several restrictions governs an acceptable set
of overloaded functions:
Any two functions in a set of overloaded
functions must have different argument lists.
Overloading functions with argument lists of
the same types, based on return type alone,
is an error.
Member functions cannot be overloaded
solely on the basis of one being static and
the other nonstatic.
12
13
CALLING OVERLOADED
FUNCTIONS
Overloaded functions are called just like other
functions. The number and type of arguments
determine which function should be invoked.
For instance consider the following code
fragment:
prnsqr (z);
prnsqr (13);
prnsqr (134.520000012);
prnsqr (12.5F);
14
void afunc(int);
void afunc(double);
//exactly
afunc(0);
match.
Matches
afunc(int)
18
Thanking You
The End