SlideShare a Scribd company logo
WEL COME
PRAVEEN M JIGAJINNI
PGT (Computer Science)
MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA,
Dc. Sc. & Engg.
email: praveenkumarjigajinni@yahoo.co.in
CHAPTER 3
Function
Overloading
3
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’.
4
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);
5
Declaration and Definition
The key to function overloading is a function’s
argument list which is also known as the
function signature. It is the signature, not the
function type that enables function overloading.
Note:
A function’s argument list (i.e., number and type of
argument) is known as the function’s signature.
6
If two functions are having same number and types
of arguments in the same order, they are said to
have the same signature. Even if they are using
distinct variable names, it doesn’t matter. For
instance, following two functions have same
signature.
void squar (int a, float b); //function 1
void squar (int x, float y); //same function as
that of function 1
7
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 instance, following code fragment overloads
a function name prnsqr( ).
Void prnsqr (int i);
Void prnsqr (char c);
Void prnsqr (float f);
Void prnsqr (double d);
//overloaded for floats #3
//overloaded for double floats #4
//overloaded for character #2
//overloaded for floats #3
//overloaded for integer #1
8
void prnsqr (int i)
{cout<<“Integer”<<i<<“’s square is”<<i*i<<“n”;
}
void prnsqr (char c);
{cout<<c<<“is a character”<<“Thus No Square for
it”<<“n”;
}
Void prnsqr (float f)
{cout<<“float”<<f <<“’s square is”<<f *f<<“n”;
}
void prnsqr (double d)
{cout <<“Double float”<<d<<“’s square
is”<<d*d<<“n’;
}
After declaring overloading functions, you must
define them separately, as it is shown below for
above given declarations.
9
Thus, we see that is not too much difficulty
in declaring overloaded functions; they are
declared as other functions are. Just one thing is
to be kept in mind that the arguments are
sufficiently different to allow the functions to be
differentiated in use.
The argument types are said to be part of
function’s extended name. For instance, the
name of above specified functions might be
prnsqr() but their extended names are
different. That is they have prnsqr(int),
prnsqr(char), prnsqr(float), and prnsqr(double)
extended names respectively.
10
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.
For example,
float square (float f);
double square (float x); //error
11
Functions with the same signature and same name
but different return types are not allowed in C++.
You can have different return types, but only if
the signatures are also different:
float square (float f);
double square (double d);
//different signatures, hence
//allowed
12
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.
Use function overloading only when a function is
required to work for alternative argument
types and there is a definite way of optimizing
the function for the argument type.
13
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.
14
Typedef declaration do not define new types;
they introduces synonyms for existing types.
They do not affect the overloading mechanism.
Consider the following code:
typedef char* PSTR;
void Print (char * szToPrint);
void Print (PSTR szToPrint);
15
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);
16
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. There are three possible cases, a function call
may result in:
a) A match. A match is found for the function call.
b) No match. No match is found for the function call.
c) Ambiguous Match. More than one defined
instance for the function call.
17
1. 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);
void afunc(double);
afunc(0);
0 (zero) is of type int , thus the call exactly matches
afunc(int).
//overloaded functions
//exactly match. Matches afunc(int)
18
2. 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, enumerator, 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.
19
For example, consider the following code
fragment:
void afunc (int);
void afunc (float);
afunc (‘c’); //match through the promotion;
matches afunc (int)
20
3. 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)
21
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 afunc (long);
void afunc (double);
afunc(15);
Here the int argument 15 can be converted either long or
double, thereby creating an ambiguous situation as to
which afunc() should be used.
//Error !! Ambiguous match
22
4. 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.
23
Default Arguments Versus
Overloading
Using default argument gives the appearance of
overloading, because the function may be called
with an optional number of arguments. For
instance, consider the following function
prototype:
float amount (float principal, int time=2, float
rate=0.08);
24
Now this function may be called by providing just one
or two or all three argument values. A function call
like as follows:
cout<<amount (3000);
will invoke the function amount() with argument
values 3000, 2, and 0.08 respectively. Similarly a
function call like
cout <<amount (3000,4);
Will invoke amount() with argument values 2500, 5,
and 0.12 respectively. That is if argument values are
provided with the function call, then the function is
invoked with the given values. But if any value is
missing and there has been default values specified
for it, then the function is invoked using the default
value.
25
However if you skip the middle argument time
but C++ makes no attempt at this type of
interpretation. C++ will take 0.13 to be the
argument value for time and hence invoke
amount() with values 2000, 0 (0.13 converted to
int, thus 0) and 0.08 (the default rate). That is,
with default arguments C++ expects that only
the arguments on the right side can be defaulted.
If you want to default a middle argument, then
all the arguments on its right must also be
defaulted.
?Any Questions Please
Thank You
Very Much

More Related Content

PPTX
Function overloading
PPTX
Function overloading and overriding
PPTX
Function overloading in c++
PPTX
Function overloading
PPTX
Classes function overloading
PDF
Operator overloading
PPTX
INLINE FUNCTION IN C++
PPTX
Inline function
Function overloading
Function overloading and overriding
Function overloading in c++
Function overloading
Classes function overloading
Operator overloading
INLINE FUNCTION IN C++
Inline function

What's hot (19)

PPTX
Presentation on overloading
PDF
03 function overloading
PPTX
Function overloading
PPT
Functions in C++
PPT
FUNCTIONS IN c++ PPT
PPT
C++ overloading
PPTX
Pointers,virtual functions and polymorphism cpp
PDF
Functions in C++
PPTX
Function C++
PPTX
Inline Functions and Default arguments
PPT
Function overloading(C++)
PPT
16 virtual function
PPT
Functions in C++
PPT
C++ Function
PDF
Functional Programming in JavaScript
PPT
CPP Language Basics - Reference
PPTX
Learning C++ - Functions in C++ 3
PPTX
Functions in C++ (OOP)
PPTX
Functions in C++
Presentation on overloading
03 function overloading
Function overloading
Functions in C++
FUNCTIONS IN c++ PPT
C++ overloading
Pointers,virtual functions and polymorphism cpp
Functions in C++
Function C++
Inline Functions and Default arguments
Function overloading(C++)
16 virtual function
Functions in C++
C++ Function
Functional Programming in JavaScript
CPP Language Basics - Reference
Learning C++ - Functions in C++ 3
Functions in C++ (OOP)
Functions in C++
Ad

Viewers also liked (20)

PPT
6 Inheritance
PPT
PPT
7 Data File Handling
PPT
13 Boolean Algebra
PPT
PDF
Introduction to cpp
PPTX
Operator overloadng
PPTX
2CPP08 - Overloading and Overriding
PPTX
1 unit (oops)
PPTX
Data types
PPT
16717 functions in C++
 
PPT
08 c++ Operator Overloading.ppt
PDF
Intro to C++ - language
PPTX
functions of C++
PPTX
Data types
PPT
C++ programming
PPT
Object-Oriented Programming Concepts
PPT
Basic concepts of object oriented programming
PPT
Oops ppt
6 Inheritance
7 Data File Handling
13 Boolean Algebra
Introduction to cpp
Operator overloadng
2CPP08 - Overloading and Overriding
1 unit (oops)
Data types
16717 functions in C++
 
08 c++ Operator Overloading.ppt
Intro to C++ - language
functions of C++
Data types
C++ programming
Object-Oriented Programming Concepts
Basic concepts of object oriented programming
Oops ppt
Ad

Similar to 3 Function Overloading (20)

PPT
class12 3 Function Overloadingin ooooop .ppt
PPT
FunctionOverloadingwithpolymorphismconcept.ppt
PPTX
Presentation on polymorphism in c++.pptx
PPT
OODP UNIT 2 Function overloading
PPTX
oprators in cpp,types with example and details.pptx
PPTX
9 functions.pptxFunction that are used in
DOCX
Functions in c++
PPT
Lecture 5
PPT
Lecture 4
PDF
overloading in C++
PPTX
C Programming Unit-2
PPTX
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
PDF
Function in C++
PPTX
Amit user defined functions xi (2)
PDF
12 computer science_notes_ch01_overview_of_cpp
PDF
[ITP - Lecture 12] Functions in C/C++
DOCX
Notes on c++
PPTX
Introduction& Overview-to-C++_programming.pptx
PPTX
Introduction to C++ programming language
PDF
polymorphism for b.tech iii year students
class12 3 Function Overloadingin ooooop .ppt
FunctionOverloadingwithpolymorphismconcept.ppt
Presentation on polymorphism in c++.pptx
OODP UNIT 2 Function overloading
oprators in cpp,types with example and details.pptx
9 functions.pptxFunction that are used in
Functions in c++
Lecture 5
Lecture 4
overloading in C++
C Programming Unit-2
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Function in C++
Amit user defined functions xi (2)
12 computer science_notes_ch01_overview_of_cpp
[ITP - Lecture 12] Functions in C/C++
Notes on c++
Introduction& Overview-to-C++_programming.pptx
Introduction to C++ programming language
polymorphism for b.tech iii year students

More from Praveen M Jigajinni (20)

PPTX
Chapter 09 design and analysis of algorithms
PPTX
Chapter 08 data file handling
PPTX
Chapter 07 inheritance
PPTX
Chapter 06 constructors and destructors
PPTX
Chapter 05 classes and objects
PPTX
Chapter 04 object oriented programming
PPTX
Chapter 03 python libraries
PPTX
Chapter 02 functions -class xii
PPTX
Unit 3 MongDB
PPTX
Chapter 17 Tuples
PPTX
Chapter 15 Lists
PPTX
Chapter 14 strings
PPTX
Chapter 13 exceptional handling
PPTX
Chapter 10 data handling
PPTX
Chapter 9 python fundamentals
PPTX
Chapter 8 getting started with python
PPTX
Chapter 7 basics of computational thinking
PPTX
Chapter 6 algorithms and flow charts
PPTX
Chapter 5 boolean algebra
PPTX
Chapter 4 number system
Chapter 09 design and analysis of algorithms
Chapter 08 data file handling
Chapter 07 inheritance
Chapter 06 constructors and destructors
Chapter 05 classes and objects
Chapter 04 object oriented programming
Chapter 03 python libraries
Chapter 02 functions -class xii
Unit 3 MongDB
Chapter 17 Tuples
Chapter 15 Lists
Chapter 14 strings
Chapter 13 exceptional handling
Chapter 10 data handling
Chapter 9 python fundamentals
Chapter 8 getting started with python
Chapter 7 basics of computational thinking
Chapter 6 algorithms and flow charts
Chapter 5 boolean algebra
Chapter 4 number system

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
RMMM.pdf make it easy to upload and study
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Basic Mud Logging Guide for educational purpose
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Structure & Organelles in detailed.
PDF
Complications of Minimal Access Surgery at WLH
PDF
Anesthesia in Laparoscopic Surgery in India
Computing-Curriculum for Schools in Ghana
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
RMMM.pdf make it easy to upload and study
2.FourierTransform-ShortQuestionswithAnswers.pdf
01-Introduction-to-Information-Management.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
GDM (1) (1).pptx small presentation for students
Renaissance Architecture: A Journey from Faith to Humanism
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial disease of the cardiovascular and lymphatic systems
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Basic Mud Logging Guide for educational purpose
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Institutional Correction lecture only . . .
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Structure & Organelles in detailed.
Complications of Minimal Access Surgery at WLH
Anesthesia in Laparoscopic Surgery in India

3 Function Overloading

  • 1. WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg. email: praveenkumarjigajinni@yahoo.co.in
  • 3. 3 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’.
  • 4. 4 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);
  • 5. 5 Declaration and Definition The key to function overloading is a function’s argument list which is also known as the function signature. It is the signature, not the function type that enables function overloading. Note: A function’s argument list (i.e., number and type of argument) is known as the function’s signature.
  • 6. 6 If two functions are having same number and types of arguments in the same order, they are said to have the same signature. Even if they are using distinct variable names, it doesn’t matter. For instance, following two functions have same signature. void squar (int a, float b); //function 1 void squar (int x, float y); //same function as that of function 1
  • 7. 7 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 instance, following code fragment overloads a function name prnsqr( ). Void prnsqr (int i); Void prnsqr (char c); Void prnsqr (float f); Void prnsqr (double d); //overloaded for floats #3 //overloaded for double floats #4 //overloaded for character #2 //overloaded for floats #3 //overloaded for integer #1
  • 8. 8 void prnsqr (int i) {cout<<“Integer”<<i<<“’s square is”<<i*i<<“n”; } void prnsqr (char c); {cout<<c<<“is a character”<<“Thus No Square for it”<<“n”; } Void prnsqr (float f) {cout<<“float”<<f <<“’s square is”<<f *f<<“n”; } void prnsqr (double d) {cout <<“Double float”<<d<<“’s square is”<<d*d<<“n’; } After declaring overloading functions, you must define them separately, as it is shown below for above given declarations.
  • 9. 9 Thus, we see that is not too much difficulty in declaring overloaded functions; they are declared as other functions are. Just one thing is to be kept in mind that the arguments are sufficiently different to allow the functions to be differentiated in use. The argument types are said to be part of function’s extended name. For instance, the name of above specified functions might be prnsqr() but their extended names are different. That is they have prnsqr(int), prnsqr(char), prnsqr(float), and prnsqr(double) extended names respectively.
  • 10. 10 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. For example, float square (float f); double square (float x); //error
  • 11. 11 Functions with the same signature and same name but different return types are not allowed in C++. You can have different return types, but only if the signatures are also different: float square (float f); double square (double d); //different signatures, hence //allowed
  • 12. 12 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. Use function overloading only when a function is required to work for alternative argument types and there is a definite way of optimizing the function for the argument type.
  • 13. 13 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.
  • 14. 14 Typedef declaration do not define new types; they introduces synonyms for existing types. They do not affect the overloading mechanism. Consider the following code: typedef char* PSTR; void Print (char * szToPrint); void Print (PSTR szToPrint);
  • 15. 15 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);
  • 16. 16 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. There are three possible cases, a function call may result in: a) A match. A match is found for the function call. b) No match. No match is found for the function call. c) Ambiguous Match. More than one defined instance for the function call.
  • 17. 17 1. 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); void afunc(double); afunc(0); 0 (zero) is of type int , thus the call exactly matches afunc(int). //overloaded functions //exactly match. Matches afunc(int)
  • 18. 18 2. 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, enumerator, 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.
  • 19. 19 For example, consider the following code fragment: void afunc (int); void afunc (float); afunc (‘c’); //match through the promotion; matches afunc (int)
  • 20. 20 3. 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)
  • 21. 21 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 afunc (long); void afunc (double); afunc(15); Here the int argument 15 can be converted either long or double, thereby creating an ambiguous situation as to which afunc() should be used. //Error !! Ambiguous match
  • 22. 22 4. 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.
  • 23. 23 Default Arguments Versus Overloading Using default argument gives the appearance of overloading, because the function may be called with an optional number of arguments. For instance, consider the following function prototype: float amount (float principal, int time=2, float rate=0.08);
  • 24. 24 Now this function may be called by providing just one or two or all three argument values. A function call like as follows: cout<<amount (3000); will invoke the function amount() with argument values 3000, 2, and 0.08 respectively. Similarly a function call like cout <<amount (3000,4); Will invoke amount() with argument values 2500, 5, and 0.12 respectively. That is if argument values are provided with the function call, then the function is invoked with the given values. But if any value is missing and there has been default values specified for it, then the function is invoked using the default value.
  • 25. 25 However if you skip the middle argument time but C++ makes no attempt at this type of interpretation. C++ will take 0.13 to be the argument value for time and hence invoke amount() with values 2000, 0 (0.13 converted to int, thus 0) and 0.08 (the default rate). That is, with default arguments C++ expects that only the arguments on the right side can be defaulted. If you want to default a middle argument, then all the arguments on its right must also be defaulted.