Function Overloading.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Overview creating your own functions calling your own functions.
Chapter 15: Operator Overloading
Function Overloading Overloading  Using same name for more than one function  Example: ovldmean.cpp.
OBJECT ORIENTED PROGRAMMING
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
ECE122 Feb. 22, Any question on Vehicle sample code?
CSCI-383 Object-Oriented Programming & Design Lecture 23.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Function Overloading and References
Chapter -6 Polymorphism
Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CSCI 383 Object-Oriented Programming & Design Lecture 21 Martin van Bommel.
Operator Overloading.
CS212: Object Oriented Analysis and Design
Basic Data Types & Memory & Representation
More About Objects and Methods
Programming with ANSI C ++
Chapter 13: Overloading and Templates
IS Program Design and Software Tools Introduction to C++ Programming
C# Operator Overloading and Type Conversions
Inheritance Allows extension and reuse of existing code
7. Inheritance and Polymorphism
Introduction to C++ Systems Programming.
FUNCTIONS In C++.
Inheritance and Run time Polymorphism
Review: Two Programming Paradigms
Objectives Define polymorphism Overload functions
Constructor & Destructor
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
Lecture 6 C++ Programming
Zhen Jiang West Chester University
Chapter 6 Methods: A Deeper Look
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
Operator Overloading.
Compile Time polymorphism.
CMSC 202 Lesson 22 Templates I.
Overview of C++ Overloading
More About Objects and Methods
Constructors and Destructors
Templates I CMSC 202.
Java Programming Language
Functions Imran Rashid CTO at ManiWeber Technologies.
C++ Object Oriented 1.
Presentation transcript:

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

Look alike but exhibit different characters

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.

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

Following code fragment overloads a function name prnsqr( ). void prnsqr (int i); //overloaded for integer #1 void prnsqr (char c); //overloaded for character #2 void prnsqr (float f); //overloaded for floats #3 //overloaded for floats #3 void prnsqr (double d); //overloaded for double floats #4 14

cout <<“No Square for characters”<<“\n”; 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

Resolution by Compiler when it sees second function with same name Signature of subsequent functions match previous function’s, then the second is treated as a re-declaration of the first - Error 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 16

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

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

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: One match - A match is found for the function call. No match - No match is found for the function call. Ambiguous Match - More than one defined instance for the function call. 19

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

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

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)

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

‘int’ argument is converted to char 26

Error ambiguous call 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

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

cout<<amount (3000); 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

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. 2018/7/5