CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Road Map Introduction to object oriented programming. Classes
Chapter 14: Overloading and Templates
Overview creating your own functions calling your own functions.
Function Part II: Some ‘advanced’ concepts on functions.
OBJECT ORIENTED PROGRAMMING
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
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.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CSCI-383 Object-Oriented Programming & Design Lecture 23.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
1 CSC241: Object Oriented Programming Lecture No 25.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
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.
Chapter -6 Polymorphism
Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Classes, Interfaces and Packages
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
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.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Learners Support Publications Constructors and Destructors.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Constructors and Destructors
Chapter VII: Arrays.
More About Objects and Methods
Chapter 7: User-Defined Functions II
FUNCTIONS In C++.
Function Overloading.
Chapter 5 Function Basics
Inheritance and Overloading
Constructor & Destructor
Methods and Parameters
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
Compile Time polymorphism.
Constructors and Destructors
More About Objects and Methods
Function Overloading CSCE 121 J. Michael Moore
Constructors and Destructors
Java Programming Language
Presentation transcript:

CHAPTER 3 Function Overloading

2 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’.

3 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);

4 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.

5 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

6 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

7 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.

8 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.

9 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

10 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

11 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.

12 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.

13 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);

14 C ALLING O VERLOADED F UNCTIONS 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 ( ); prnsqr (12.5F);

15 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.

16 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)

17 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.

18 For example, consider the following code fragment: void afunc (int); void afunc (float); afunc (‘ c ’); //match through the promotion; matches afunc (int)

19 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)

20 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

21 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.

22 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);

23 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.

24 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.

The End Thanking You