CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
C++ Operator Overloading Gordon College CPS212 Gordon College CPS212.
OBJECT ORIENTED PROGRAMMING
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
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.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
ECE122 Feb. 22, Any question on Vehicle sample code?
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
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.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
CS212: Object Oriented Analysis and Design
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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
Introduction to Object-Oriented Programming Lesson 2.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Overview of C++ Polymorphism
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Learners Support Publications Constructors and Destructors.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
CSCI 383 Object-Oriented Programming & Design Lecture 21 Martin van Bommel.
Design issues for Object-Oriented Languages
CS212: Object Oriented Analysis and Design
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
FUNCTIONS In C++.
Function Overloading.
Constructor & Destructor
Polymorphism.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
Compile Time polymorphism.
Constructors and Destructors
Java – Inheritance.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Overview of C++ Polymorphism
Inheriting Multiple Base Classes
Java Programming Language
Chapter 6 Polymorphism.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Presentation transcript:

CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++

Recap of Lecture 8 GRASP principle 9 different attributes Cohesion and coupling Polymorphism

Outline of Lecture 9 Polymorphism in C++ Function overloading Name mangling Steps of name resolution Copy Constructor

Polymorphism Refers to ‘one name having many forms’ ‘Different behaviour of an instance depending upon the situation’. An ‘overloaded function’ refers to a function having (one name and) more than one distinct meanings. Polymorphism in C++ Compile Time Function overloading Operator overloading Runtime Virtual Function Not only does it provide support for compile-time polymorphism, it also adds flexibility and convenience.

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 add (int a, int b); float add (float x, float y);

Function signature/ Extended name The key to function overloading is a function’s argument list This is also known as the function signature/ extended name. It is the signature, not the function type that enables function overloading. Two functions with same number and types of arguments in the same order are said to have the same signature. int myfunc(int i) int myfunc(int i, float j) void myfunc(int x, float y)

Overloading  Different signature To overload a function name Declared and defined with the same name Characterized by different signatures. For instance, following code fragment overloads a function name add( ). int add(int a, int b) int add(int a, int b, int c) double add(double x, double y) double add(double p, int q)

Task of a compiler Signatures of two functions are identical, and the return types are same, then the second is treated as a re- declaration of the first. Signatures of two functions are identical, however the return type differ, the second is an erroneous re-declaration Signature of the two functions differ in either the number or type of their arguments, the two functions are considered to be overloaded

C++ compiler model Pre- processor Compiler Assembly code Assembler Object code Linker Executable code

Name Mangling/ Name Decoration Technique used to resolve unique names for programming entities It provides a way of encoding additional information – function, structure, class, and other data type The need arises where the language allows different entities to be named with the same To correctly link a function it needs its name, the number of arguments and their types

Name mangling in C++ C++ allows overloading, however linker does not Each function name must be unique Generates a new name for each function – mangling/ decoration The process of name mangling is compiler dependent. The name mangled by a compiler may not be same as mangled by other compilers. The mangled name contains all the necessary information that the linker needs, such as linkage type, scope, calling convention, number of arguments, type of arguments.

Mangled name for g++ compiler Mangling pattern _Z N OriginalFunctionName parameter-names-encoded N = Number Of Chars In Original Function Name Original NameMangled Name void myFun();_Z5myFunv void myFun(int a, char c);_Z5myFunic

Restrictions 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 non-static. Typedef declaration do not define new types; they introduces synonyms for existing types.

Steps involved in finding match Argument matching A 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

Function selection flow-chart Start Exact match ? Call that function Stop Match by promotion ? No Yes Match by conversion ? No Error!!

Search for an Exact Match The compiler first tries to find an exact match Types of actual arguments are exactly same 1. int add(int a, int b) 2. int add(int a, int b, int c) 3. double add(double x, double y) 4. double add(double p, int q) 1. cout << add(5,10) 2. cout << add(6,-5,15) 3. cout << add(12.5,7.5) 4. cout << add(0.145, 2)

A match through promotion If exact match is not found Compiler uses integral promotion to the actual arguments int add(int a, int b) cout << add(10, ‘A’);

A match through application of standard C++ conversion rules Compiler attempts built-in conversion Implicit assignment conversion Then finds a unique match for the function If conversion results multiple match – compiler error !! void aFunc(char); void aFunc(double); aFunc(10); C++ standard conversion rules void aFunc(long); void aFunc(double); aFunc(10); Error !! Ambiguous match

A match through application of a user-defined conversion If all the three methods fails Then the compiler will try the user-defined conversion in the combinations to find a unique match. This technique is used to handle class objects

Copy Constructors One of the more important forms of an overloaded constructor is the copy constructor. Defining a copy constructor can help you prevent problems that might occur when one object is used to initialize another. When we copy? Initialize Pass as argument Return as parameter

Thank you Next Lecture: Operator Overloading