1
The C++ implements polymorphism through function
overloading and operator overloading. The term
overloading means a name having two or more distinct
meanings. Thus, an overloaded function refers to the
function having same 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. In C++ an overloaded functions are
selected for invoking(calling) by matching arguments,
both type(of arguments) and number (of arguments).
This information is known to the Compiler at the
compile-time and, thus, the compiler is able to select the
2
appropriate function for a particular call at the time of
compilation itself. “This is called early binding or
static binding. Alternatively it is known as compile-
time polymorphism as the required function gets
determined only at compile time”.
When several function declarations are specified for a
single function name in the same scope, the function
said to be overloaded. C++ allows functions to have same
name if it can distinguish by their number and types of
arguments.
3
for e.g. (overloaded functions)
float Area(float r) //Calculates Area of Circle
{
return(3.14*r*r);
}
float Area(float b, float h) //Calculates Area of Triangle
{
return(0.5*b*h);
}
4
Function overloading not only implements
polymorphism but also reduces number of comparisons
in a program and thereby makes the program run faster.
5
The name of the functions should be same.
float Area(float r) //Calculates Area of Circle
{
return(3.14*r*r);
}
float Area(float b, float h) //Calculates Area of Triangle
{
return(0.5*b*h);
}
6
It must have different number of arguments.
float Area(float r) //One argument
{
return(3.14*r*r);
}
float Area(float b, float h) //Two arguments
{
return(0.5*b*h);
}
7
If the number or arguments are same then type of any
one argument should be different.
float Area(float r, double m) //Two arguments
{
return(r*k);
}
float Area(float b, float h) //Two arguments
{
return(0.5*b*h);
}
8
If the number and types of arguments are same then
one function should have return type as void and the
other function should have another return type.
void Area(float r, float k) //Two arguments
{
return(r*k);
}
float Area(float b, float h) //Two arguments
{
return(0.5*b*h);
}
9
If the number ,types of arguments and return type is
also same then that’s’ not a function overloading. The
C++ considers it as an AMBIGUITY(two functions
can be called at the time of invoking one).
float Area(float r, float k) //Two arguments
{
return(r*k);
}
float Area(float b, float h) //Two arguments
{
return(0.5*b*h);
} 10
The key to function overloading is a function’s argument
list which is also known as function signature. It is the
function signature not the type that enables function
overloading. If two functions are having same number
and types of arguments in the same order, they are said
to be same signature. Even if they have distinct variable
names. for e.g.
This both functions are having same
void square(int a, float b); signature though the variables names
void square(int x, float y); are different
11
To overload a function name, all you need to do is,
declare and define all the functions with the same name
but different signatures, separately, for e.g. following is
the overloaded functions for a function name prnSqr.
The following four function
void prnSqr(int i); // Version 1 declarations for prnSqr() is valid
as all functions having different
void prnSqr(char i); // Version 2 signature. The first function
void prnSqr(float i); // Version 3 accept int argument, second
function accept char argument,
void prnSqr(double i); // Version 4 third function accept float
argument and the last function
accepts double argument.
12
After declaring the overloaded functions, these
functions should be defined separately. i.e.
void prnSqr(int i) // Version 1
{
cout<<i*i;
}
void prnSqr(char i) // Version 2
{
cout<<“Square is not possible”;
}
13
After declaring the overloaded functions, these
functions should be defined separately. i.e.
void prnSqr(float i) // Version 3
{
cout<<i*i;
}
void prnSqr(double i) // Version 4
{
cout<<i*i;
}
14
Overloaded functions can be called just like other
functions. The number and the type of arguments
determine which should be invoked. For e.g. the
following overloaded functions to be called,
void prnSqr(int i); // Version 1
void prnSqr(char i); // Version 2
void prnSqr(float i); // Version 3
void prnSqr(double i); // Version 4
Now, to invoke the version 1 we should pass an integer
constant or variable, to invoke the version 2 we should
pass a char constant or a character argument, to invoke 15
version 3 we should pass a float type constant or variable
and to invoke version 4 we should pass double type
constant or variable. Thus, to invoke the above
mentioned versions we should use,
prnSqr(34); Or int L=12; // Version 1
prnSqr(L);
prnSqr(‘#’); Or char L=‘K’; // Version 2
prnSqr(L);
prnSqr(5.12); Or float L=12; // Version 3
prnSqr(L);
prnSqr(3.14); Or double L=12; // Version 4
prnSqr(L);
16
A function call first matches the prototype available with
the number and types of arguments provided with the
function call and then calls the appropriate function for
execution. But sometimes there might be ambiguity
between float and double values or int or long values.
For e.g. if we want to invoke version 3 and 4, with the
value 3.14, this value can cause an ambiguity as value 3.14
can be considered as float or double type. Now, if we
want to avoid such ambiguity then we should use the
constant suffixes i.e. F, L, U & UL, to distinguish
between such values as these greatly help in indicating
which overloaded function should be called.
17
The function calls suffixes F,L,U and UL are referred to
as F(for float), L(for long double), U(for unsigned) and
UL or LU (for unsigned long). When calling functions
we should use constant suffixes to avoid ambiguity
Now, To call version 3 and 4 we should use the following
function calls.
prnSqr(3.14F); for version 3
prnSqr(3.14); for version 4
Note that when we pass a real type value, it is by default
considered as double type, therefore, the suffix F
converts it to float type. 18
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 disambiguates. Argument matching
involves comparing the actual arguments of the call with
the formal arguments of each declared instance of the
function. There are three possible cases, a function call
may result in.
Matching
A Match No Match Ambiguous Match
(Match found for the (No match is found (More than one calls
function call) for the function call) function can calls) 19
A Match:
At the time of invoking or calling a function, if exactly
only one function can be invoked, then it is referred to as
A Match to a function call.
for e.g.
void Area (float r) //Function 1
{
cout<<3.14*r*r;
}
void Area(float b, float h) //Function 2
{
cout<<0.5*b*h;
20
}
A Match:
Now, at the time of calling the above function, if exactly
one function is called by a function call statement it is
referred to as A Match to the function call. Therefore, to
invoke(call) the above mentioned functions we will use
the following function call statements.
for e.g.
void main()
{
Area(3.14); //invokes Function 1 (A Match)
Area(3.14, 2.36) //Invokes Function 2 (A Match)
}
21
No Match:
At the time of invoking or calling a function, if no
function is invoked, then it is referred to as No Match to
a function call.
for e.g.
void Area (float r) //Function 1
{
cout<<3.14*r*r;
}
void Area(float b, float h) //Function 2
{
cout<<0.5*b*h;
22
}
No Match:
Now, at the time of calling the above functions, if no
function is called by a function call statement it is
referred to as No Match to the function call.
No Match
for e.g. because, 3
arguments are
void main() passed.
{
Area(3.14); //invokes Function 1 (A Match)
Area(3.14, 2.36, 1.2) // (No Match)
}
23
Ambiguous Match:
At the time of invoking or calling a function, if more
than one function can be invoked, then it is referred to
as Ambiguous Match to a function call.
for e.g.
void Area (float r, float k) //Function 1
{
cout<<3.14*r*r;
}
void Area(float b, float h) //Function 2
{
cout<<0.5*b*h;
24
}
Ambiguous Match:
Now, at the time of calling the above functions, if more
than one function can be called by a function call
statement it is referred to as Ambiguous Match to the
function call.
Ambiguous
Match because,
it can invoke
for e.g. both functions.
void main()
{
Area(3.14); //(No Match)
Area(3.14, 2.36) //(Ambiguous Match)
}
25
Using default arguments gives the appearance of
overloading, because the function may be called with
optional number of arguments, for e.g. consider a
function amount() with the following prototype.
The function amount() is
declared with three arguments
in which last two arguments
namely time and rate are
DEFAULT ARGUMENTS.
float amount(float principal, int time=2,float rate=0.08);
26
Now, to invoke (call) the above mentioned function we
can write the following statements.
For this function call the principal will be
5000, time will be 2 years & rate will be
0.08 (time and rate will use its default
value as it is not passed to the function)
float Ans;
Ans=amount(5000);
27
For this function call the principal will be
5000, time will be 4 years & rate will be
0.08 (rate will use its default value as it is
Or not passed to the function)
float Ans;
Ans=amount(5000,4);
28
For this function call the principal will be
5000, time will be 2 years & rate will be
0.12 (no default argument will be used as
all arguments values are passed to it)
Or
float Ans;
Ans=amount(5000, 4, 0.12);
29
With default arguments exactly one function definition
can be executed. But using function overloading we can
make a function work differently (by providing different
function definitions) depending upon the different
argument combinations, for e.g. consider the following
overloaded function.
30
This function can be invoked when we
pass three arguments i.e. principal of float
type, time of int type & rate of float type.
void amount(float principal, int time, float rate) // Case 1
{
cout<<principal*time*rate;
}
31
This function can be invoked when we
pass two arguments i.e. principal of float
type, time of int type.
void amount(float principal, int time) // Case 2
{
float rate=0.08;
cout<<principal*time*rate;
}
32
This function can be invoked when we
pass two arguments i.e. principal of float
type, rate of float type.
void amount(float principal, float rate) // Case 3
{
int time=2;
cout<<principal*time*rate;
}
33
This function can be invoked when we
pass two arguments i.e. time of int type,
rate of float type.
void amount(int time, float rate) // Case 4
{
float principal=5000;
cout<<principal*time*rate;
}
34
This function can be invoked when we
pass one argument i.e. principal of float
type.
void amount(float principal) // Case 5
{
float rate=0.08; int time=2;
cout<<principal*time*rate;
}
35
Thus, the advantages of function overloading over
default arguments are:
1. Default arguments might not work for all possible
combinations of arguments where as a function may
be overloaded for all possible combinations of
arguments.
2. With function overloading, multiple function
definitions can be executed but with default
arguments exactly one function definition is
executed.
36
3. By declaring an overloaded function, you save the
compiler from the trouble of pushing the default
argument value on to the function call stack, and you
save the function from the trouble of testing the
default value.
37
Problem-1.
A function printChar is defined as
void printChar(char ch=‘*’,int len=40)
{
for(int x=0; x<len; x++)
{
cout<<ch;
}
cout<<endl;
}
38
Problem-1.
How will you invoke the function printChar for the
following output?
1. To print * , 40 times.
2. To print * , 20 times.
3. To print = , 40 times.
4. To print = , 30 times.
39
Solution-1.
To invoke the function printChar for the above
mentioned outputs, we shall write the following
function call statements.
1. printChar(); //To print * , 40 times.
2. printChar(‘*’, 20); //To print * , 20 times.
3. printChar(‘=’); //To print = , 40 times.
4. printChar(‘=’, 30) //To print =,30 times.
40
Problem-2.
C++ does not allow arguments other than the last to be
given default values. Therefore , the following code
void test(int a, float b=0.0, char c)
{
cout<<“a=”<<a<<“,b=”<<b<<“ & c=”<<c<<”\n”;
}
is wrong. Is there some way we can achieve the result we
want in this example? 41
Solution-2.
It is possible to achieve the the result we want in this
example using function overloading and the above
mentioned can be written using function overloading as
void test(int a, float b, char c)
{
cout<<“a=”<<a<<“,b=”<<b<<“ & c=”<<c<<”\n”;
}
42
Solution-2.
void test(int a, char c)
{
cout<<“a=”<<a<< “ & c=”<<c<<”\n”;
}
43
Problem-3.
Which of the following overloaded function are called in
each of the called in main()? If any of the calls are
ambiguous?
void func(int a) {…..} //function 1
void func(int a, float b) {…..} //function 2
void func(int a, double y) {…..} //function 3
int main()
{
func(1); //Statement 1
func(‘1’); //Statement 2 44
Problem-3.
func(2,3); //Statement 3
return 0;
}
45
Solution-3.
In the above example
Statement 1 will invoke Function 1 as an integer
argument is passed to it.
Statement 2 will invoke Function 1 as character
argument is passed to it, and it will be convetred to
integer.
46
Solution-3.
Statement 3 is ambiguous as it can invoke Function 2 or
Function 3 as the 2nd argument can be converted to float
or double.
47
Problem-3.
Write a C++ program that uses an area() function for the
calculation of area of triangle or a rectangle or a square.
Number of sides (3 for triangle, 2 for rectangle and 1 for
square).
48
Problem-4.
Write a C++ program that uses a function to check
whether a given number is divisible by another or not.
However, if the second number is missing, the function
checks whether the given number is prime or not.
49
Problem-5.
Write overloaded prototypes of inQuote(), a function
that displays it’s arguments enclosed in double
quotation marks. Write three versions: one for single int
argument, one for double argument and one for
character argument.
50
Problem-6.
Write overloaded prototypes of theMax(), a function
that displays maximum of it’s arguments. Write three
versions: one for single int argument, one for two int
arguments and one for an array of integers.
51
Problem-7.
Write overloaded prototypes of Volume(), a function
that returns volumes of different structures. Write three
versions: one for cube’s volume that takes one float side
of the cube, one for cylinder’s volume that takes float
radius and float height of cylinder, and one rectangular
box’s volume that takes float length, float breadth and
float height of the box.
52
Problem-8.
Write overloaded prototypes of Handle(), a function that
returns the reversed case of the character or prints it
twice depending upon whether you assign the return
value to it or not.
53
Problem-9.
Write definitions of two versions of overloaded function.
This function 1st version sum() takes an argument, an int
array, and returns the sum of all elements passed to it.
The 2nd version of sum() takes two arguments, an int
array and a character (either ‘E’ or ‘O’). If the passed
character is ‘E’, it returns sum of even elements of the
passed array and if the passed character is ‘O’, it returns
the sum of odd elements. In case of any other character
it should return ZERO.
54
1. Why do you think the function overloading must be
a part of Object Oriented Programming.?
2. With the multiple definitions of single function
name, what makes them significantly different?
3. Illustrate the concept of function overloading with
the help of an example?(Delhi 2000)
4. How is matching done in case of overloaded
function?
5. How would you compare default argument and
function overloading?
55
6. What do you understand by the term static binding?
56