SlideShare a Scribd company logo
1 of 36
Function Overloading
Finding Gross Pay
• Three are three types of employees in Indian railways.
They are regular, daily wages and consolidated
employees. Gross Pay for the employees are
calculated as follows:
– regular employees - basic + hra + % of DA * basic
– Daily wages – wages per hour * number of hours
– Consolidated – fixed amount
PAC - Finding Gross pay
Input Output Logic Involved
Components for
calculating gross
pay
Gross pay Based on type of
employees –
Calculate gross pay
Writing Functions
• Same function name for all three type of
employees
• More meaningful and elegant way of doing
things
• I prefer the name - calculate_Gross_Pay for all
types of employees
Polymorphism
• Refers to ‘one name having many forms’,
‘one interface doing multiple actions’.
• In C++, polymorphism can be either
– static polymorphism or
– dynamic polymorphism.
• C++ implements static polymorphism
through
– overloaded functions
– overloaded operators
Polymorphism
• Derived from the Greek many forms
• Single name can be used for different purposes
• Different ways of achieving the polymorphism:
1. Function overloading
2. Operator overloading
3. Dynamic binding
Overloading
• Overloading – A name having two or
more distinct meanings
• Overloaded function - a function having
more than one distinct meanings
• Overloaded operator - When two or more
distinct meanings are defined for an
operator
Overloading
• Operator overloading is inbuilt in C and
C++.
• ‘-’ can be unary as well as binary
• ‘*’ is used for multiplication as well as
pointers
• ‘<<‘, ‘>>’ used as bitwise shift as well as
insertion and extraction operators
• All arithmetic operators can work with
any type of data
Function Overloading
 C++ enables several functions of the same name to be
defined, as long as they have different signatures.
 This is called function overloading.
 The C++ compiler selects the proper function to call by
examining the number, types and order of the
arguments in the call.
 Overloaded functions are distinguished by their signatures
 Signature - Combination of a function’s name and its
parameter types (in order)
 C++ compilers encodes each function identifier with the
number and types of its parameters (sometimes referred to
as name mangling or name decoration) to enable type-safe
linkage.
12
Signature of a Function
• A function’s argument list (i.e., number and type of
argument) is known as the function’s signature.
• Functions with Same signature - Two functions with same
number and types of arguments in same order
• variable names 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);
13
13
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
14
14
void prnsqr (int i)
{
cout<<“Integer”<<i<<“’s square is”<<i*i<<“n”;
}
void prnsqr (char c);
{
cout <<“No Square for characters”<<“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’;
}
15
15
1) Signature of subsequent functions match previous function’s,
then the second is treated as a re-declaration of the first - Error
2) Signatures of two functions match exactly but the return type
differ, then the second declaration is treated as an erroneous
re-declaration of the first
For example,
float square (float f);
double square (float x);
// Differ only by return type so erroneous re-
declaration
//error
Resolution by Compiler when it sees second
function with same name
16
16
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.
17
17
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);
18
18
Steps Involved in Finding the Best
Match for a function call
A call to an overloaded function is resolved to a particular
instance of the function, there are three possible cases,
a function call may result in:
a) One 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.
19
19
1. Exact Match
For example, there are two functions with same name
afunc:
void afunc(int);
void afunc(double);
The function call
afunc(0);
is matched to void afunc(int); and compiler invokes
corresponding function definition
as 0 (zero) is of type int
//overloaded functions
//exactly match. Matches afunc(int)
20
20
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 - integral
promotion.
21
21
For example, consider the following code fragment:
void afunc (int);
void afunc (float);
afunc (‘c’);
Will invoke afunc(int)
//match through the promotion;
matches afunc (int)
22
Compiler resolves
square (‘a’) to
square(int)
Compiler resolves
square (‘a’) to
square(char)
24
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);
afunc (471);
//match through standard
conversion matches afunc (char)
25
25
• ‘int’ argument is converted to char
26
26
• Error ambiguous call
27
27
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.
• Member functions can also be overloaded
28
28
Default Arguments Versus Overloading
• Using default argument is also 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);
29
29
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 3000, 4
and 0.08
cout <<amount (2500,5,0.12);
Will invoke amount() with argument values 2500, 5,
and 0.12 respectively
30
Case Study Implementation
• Member function search is overloaded
• Railways class has to be friend of train class to
access members of the class
2022/4/6
To calculate the area of circle, rectangle and
triangle using function overloading.
• S1: Start the program.
• S2: Declare the class name as fn with data members and member functions.
• S3: Read the choice from the user.
• S4: Choice=1 then go to the S5.
• S5: The function area() to find area of circle with one integer argument.
• S6: Choice=2 then go to the S7.
• S7: The function area() to find area of rectangle with two integer argument.
• S8: Choice=3 then go to the S9.
• S9: The function area() to find area of triangle with three arguments, two as Integer
and one as float.
• S10: Choice=4 then stop the program.

More Related Content

What's hot

What's hot (19)

Functions in C++
Functions in C++Functions in C++
Functions in C++
 
functions of C++
functions of C++functions of C++
functions of C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
C++ functions
C++ functionsC++ functions
C++ functions
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
C++ Function
C++ FunctionC++ Function
C++ Function
 

Similar to OODP UNIT 2 Function overloading

Similar to OODP UNIT 2 Function overloading (20)

Presentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxPresentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptx
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c language
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Functions
FunctionsFunctions
Functions
 
oops.pptx
oops.pptxoops.pptx
oops.pptx
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Functions
FunctionsFunctions
Functions
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
Review functions
Review functionsReview functions
Review functions
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 

More from Shanmuganathan C

17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog217-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2Shanmuganathan C
 
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_AlgorithmsShanmuganathan C
 
ClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPairShanmuganathan C
 
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsShanmuganathan C
 
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02Shanmuganathan C
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortShanmuganathan C
 
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...Shanmuganathan C
 
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...Shanmuganathan C
 
OODP UNIT 2 operator overloading
OODP UNIT 2 operator overloadingOODP UNIT 2 operator overloading
OODP UNIT 2 operator overloadingShanmuganathan C
 
OODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objectsOODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objectsShanmuganathan C
 

More from Shanmuganathan C (10)

17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog217-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
 
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
 
ClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPair
 
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
 
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
 
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
 
OODP UNIT 2 operator overloading
OODP UNIT 2 operator overloadingOODP UNIT 2 operator overloading
OODP UNIT 2 operator overloading
 
OODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objectsOODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objects
 

Recently uploaded

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 

Recently uploaded (20)

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 

OODP UNIT 2 Function overloading

  • 2. Finding Gross Pay • Three are three types of employees in Indian railways. They are regular, daily wages and consolidated employees. Gross Pay for the employees are calculated as follows: – regular employees - basic + hra + % of DA * basic – Daily wages – wages per hour * number of hours – Consolidated – fixed amount
  • 3. PAC - Finding Gross pay Input Output Logic Involved Components for calculating gross pay Gross pay Based on type of employees – Calculate gross pay
  • 4. Writing Functions • Same function name for all three type of employees • More meaningful and elegant way of doing things • I prefer the name - calculate_Gross_Pay for all types of employees
  • 5. Polymorphism • Refers to ‘one name having many forms’, ‘one interface doing multiple actions’. • In C++, polymorphism can be either – static polymorphism or – dynamic polymorphism. • C++ implements static polymorphism through – overloaded functions – overloaded operators
  • 6. Polymorphism • Derived from the Greek many forms • Single name can be used for different purposes • Different ways of achieving the polymorphism: 1. Function overloading 2. Operator overloading 3. Dynamic binding
  • 7. Overloading • Overloading – A name having two or more distinct meanings • Overloaded function - a function having more than one distinct meanings • Overloaded operator - When two or more distinct meanings are defined for an operator
  • 8. Overloading • Operator overloading is inbuilt in C and C++. • ‘-’ can be unary as well as binary • ‘*’ is used for multiplication as well as pointers • ‘<<‘, ‘>>’ used as bitwise shift as well as insertion and extraction operators • All arithmetic operators can work with any type of data
  • 9. Function Overloading  C++ enables several functions of the same name to be defined, as long as they have different signatures.  This is called function overloading.  The C++ compiler selects the proper function to call by examining the number, types and order of the arguments in the call.
  • 10.  Overloaded functions are distinguished by their signatures  Signature - Combination of a function’s name and its parameter types (in order)  C++ compilers encodes each function identifier with the number and types of its parameters (sometimes referred to as name mangling or name decoration) to enable type-safe linkage.
  • 11.
  • 12. 12 Signature of a Function • A function’s argument list (i.e., number and type of argument) is known as the function’s signature. • Functions with Same signature - Two functions with same number and types of arguments in same order • variable names 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);
  • 13. 13 13 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
  • 14. 14 14 void prnsqr (int i) { cout<<“Integer”<<i<<“’s square is”<<i*i<<“n”; } void prnsqr (char c); { cout <<“No Square for characters”<<“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’; }
  • 15. 15 15 1) Signature of subsequent functions match previous function’s, then the second is treated as a re-declaration of the first - Error 2) Signatures of two functions match exactly but the return type differ, then the second declaration is treated as an erroneous re-declaration of the first For example, float square (float f); double square (float x); // Differ only by return type so erroneous re- declaration //error Resolution by Compiler when it sees second function with same name
  • 16. 16 16 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.
  • 17. 17 17 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);
  • 18. 18 18 Steps Involved in Finding the Best Match for a function call A call to an overloaded function is resolved to a particular instance of the function, there are three possible cases, a function call may result in: a) One 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.
  • 19. 19 19 1. Exact Match For example, there are two functions with same name afunc: void afunc(int); void afunc(double); The function call afunc(0); is matched to void afunc(int); and compiler invokes corresponding function definition as 0 (zero) is of type int //overloaded functions //exactly match. Matches afunc(int)
  • 20. 20 20 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 - integral promotion.
  • 21. 21 21 For example, consider the following code fragment: void afunc (int); void afunc (float); afunc (‘c’); Will invoke afunc(int) //match through the promotion; matches afunc (int)
  • 24. 24 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); afunc (471); //match through standard conversion matches afunc (char)
  • 25. 25 25 • ‘int’ argument is converted to char
  • 27. 27 27 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. • Member functions can also be overloaded
  • 28. 28 28 Default Arguments Versus Overloading • Using default argument is also 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);
  • 29. 29 29 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 3000, 4 and 0.08 cout <<amount (2500,5,0.12); Will invoke amount() with argument values 2500, 5, and 0.12 respectively
  • 30. 30
  • 31.
  • 32.
  • 33. Case Study Implementation • Member function search is overloaded • Railways class has to be friend of train class to access members of the class
  • 34.
  • 35.
  • 36. 2022/4/6 To calculate the area of circle, rectangle and triangle using function overloading. • S1: Start the program. • S2: Declare the class name as fn with data members and member functions. • S3: Read the choice from the user. • S4: Choice=1 then go to the S5. • S5: The function area() to find area of circle with one integer argument. • S6: Choice=2 then go to the S7. • S7: The function area() to find area of rectangle with two integer argument. • S8: Choice=3 then go to the S9. • S9: The function area() to find area of triangle with three arguments, two as Integer and one as float. • S10: Choice=4 then stop the program.