Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Structures Spring 2013Programming and Data Structure1.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
OBJECT ORIENTED PROGRAMMING
1 CSC241: Object Oriented Programming Lecture No 07.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
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.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
SNU OOPSLA Lab. 7. Functions © copyright 2001 SNU OOPSLA Lab.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
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
Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Classes, Interfaces and Packages
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.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
Programming Fundamentals Enumerations and Functions.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
MAITRAYEE MUKERJI Object Oriented Programming in C++
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
More About Objects and Methods
Chapter 7: User-Defined Functions II
FUNCTIONS In C++.
Function Overloading.
Class A { public : Int x; A()
Inheritance and Run time Polymorphism
Objectives Define polymorphism Overload functions
Chapter 5 Function Basics
Constructor & Destructor
Generic programming – Function template
CS212: Object Oriented Analysis and Design
Operator Overloading
Polymorphism.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
Compile Time polymorphism.
CONSTRUCTORS AND DESRUCTORS
CS410 – Software Engineering Lecture #5: C++ Basics III
C++ Object Oriented 1.
Creating and Using Classes
Presentation transcript:

Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand

Function Overloading =>In C++, two or more functions can share the same name as long as their parameter declarations are different. =>In this situation, the functions that share the same name are said to be overloaded, and the process is referred to as function overloading. => The overloading are selected for invoking 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 appropriate function for a particular call at the compile time itself. This is called EARLY-BINDING or STATIC BINDING or STATIC-LINKING. It is also known as compile-time Polymorphism.

=>Function overloading : A function name having several definitions that are differentiable by the number or types of their arguments is known as overloading of function. =>The secret to overloading is that each redefinition of the function must use either- different types of parameters different number of parameters. float divide( int a,int b); float divide(float x,float y);

Function Overloading and Ambiguity void f(int x); void f(int &x); // error two functions cannot be overloaded when the only difference is that one takes a reference parameter and the other takes a normal, call-by-value parameter. => Note: Function overloading not only implements polymorphism but also reduces number of comparisons in a program and thereby makes the program run faster.

Declaration and definition The key to function overloading is a function’s argument list(i.e., number and type of arguments). A function’s argument list is known as its signature. Example : Different types of arguments Different number of parameters void print(int a); void print (double b); void print(char c); void area(float r); void area(float l, float b); void area( float a, float b, float c );

After declaring overloading function,you must redefine them separately. void area(float r) { cout<<“radius”<<r<<“area of circle”<<3.14*r*r; }

If two function have same number and types of arguments in the same order, they are said to have the same signature. There is no importance to their variable names. void print(int x, float y); void print(int p, float q); are said to have same signature.

When a function name is declared more than once in a program, the compiler will interpret the second(and subsequent) declarations as follows: 1. If the signature of subsequent function s match the previous function’s,then the second is treated as a re-declaration of the first. 2. If the signature of subsequent function s match exactly but the return type differ, the second declaration treated as an erroneous re- declaration of the first and is flagged at compile time as an error. float square(float f); double square(float f); //error

3.If the signature of two functions differ in either the number or type of their arguments, the two functions are considered to be overloaded. float square(float f); double square(double f); //allowed

Restriction on overloaded function 1. Any two functions in set of overload functions must have different arguments lists. 2. Overloading functions with argument lists of same types, based on return type alone, is an error. 3. Member function can’t be overloaded solely on the basis of one being static and other non-static. 4. Typedef declarations do not define new type, they introduces synonyms for existing types. They do not affect the overloading mechanism. typedef char *pstr; void print(char * pr); void print(pstr pr);

5. Enumerated type are distinct types and can be used to distinguish between overloaded functions. 6. The types “array of” and “ pointer to ” are consider identical for the purpose of distinguishing between overloaded functions. this is true only for single dimensioned array. void print( char *pr); void print(char pr[]);

Calling Overloaded Function Overloaded functions are called just like ordinary functions. The signature determines which function among the overloaded functions should be executed. print(3); print(‘B ’);

STEPS FOR FINDING BEST MATCH The actual arguments are compared with formal arguments of the overloaded functions and the best match is found through a number of steps. In order to find the best match, the compiler follows the steps given below:

STEPS- 1 : Search for an exact match If the type of the actual and formal argument is exactly the same, the compiler invokes that function. void print(int x); // function #1 void print(float p); // function #2 void print(char c); // function # print(5.3); //invokes the function #2

STEPS- 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. void print(int x); // function #1 void print(float p); // function #2 void print(double f); // function # print(‘c’); //matches the function #1 through promotion

STEPS - 3 : A match through standard C++ conversion rules If the first and second steps fail, then an attempt is made to find a best match through conversion rule. void print(char x); // function #1 void print(float p); // function # print(471); //matches the function #2 int 471 is converted to float

STEPS - 4 : A match through user defined conversion If all the above three steps fail to find a match, then an attempt is made to find a match by applying user defined conversion rules.

Functions with Default Arguments Vs Overloading Functions with default arguments can be called with optional number of arguments and hence it gives an appearance of function overloading. float calc(float p, int q=8, float r=18.3); float f = calc(3.5); float f = calc(3.5, 10); float f = calc(3.5, 10,30.5);

1. Read the following function definition : void print(int x) { cout << x; } void print (float x) { cout << x; } void print(char x) { cout << x; }

a.All these functions can be used in the same program. Explain this feature of C++ b.Write sample statements to invoke each of these functions. a)About FUNCTION OVERLOADING b)print(35); print(32.7); print(‘M’);

2.Why is function overloading connected to compile-time polymorphism or early binding? 3.Early binding refers to the ability of the compiler to relate or bind a function call with the function definition during compilation itself. FUNCTION OVERLOADING comes under this category because it match the actual arguments with formal arguments during compile-time.

ADVANTAGES OF FUNCTION OVERLOADING 1.Default arguments might not work for all possible combinations of arguments whereas 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. 3.BY declaring an overloaded function,you save the compile time from the trouble of pushing the default argument value on the function call stack and save the function from the trouble of testing the default value.

END

Constructor Overloading Just like any other function, constructors can also be overloaded. We can use constructor overloading for initializing the objects based on different conditions.

In Constructor Overloading, we need not call the constructor separately because they are invoked automatically. But In Function Overloading, we have to invoke them separately.

It is one type of Static Polymorphism. C++ has about 45 operators. These operators are defined for the fundamental data types (int, float etc). While defining a class, we are actually creating a new datatype. Most of the C++ operators can be overloaded to apply to our new class datatype.

Operator overloading is the concept of giving new meaning to an existing C++ operator. When overloaded, the operators are implemented as functions using the operator keyword. For example, the syntax of overloading the addition operator “+” would be operator+().

One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading.

You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention. For example, to overload the + operator for your class, you would provide a member-function named operator+ on your class.