Polymorphism. Zimmer CSCI 330

Similar documents
C++ Programming: Polymorphism

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

VIRTUAL FUNCTIONS Chapter 10

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Polymorphism Part 1 1

Increases Program Structure which results in greater reliability. Polymorphism

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

OBJECT ORIENTED PROGRAMMING USING C++

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

A <Basic> C++ Course

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Data Abstraction. Hwansoo Han

Object Oriented Programming. Solved MCQs - Part 2

Object Oriented Software Design II

Object Oriented Software Design II

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Polymorphism. Arizona State University 1

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Object Oriented Programming with c++ Question Bank

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Inheritance, Polymorphism and the Object Memory Model

C++ Important Questions with Answers

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Introduction to RTTI. Jonathan Hoyle Eastman Kodak 11/30/00

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Dynamic Binding C++ Douglas C. Schmidt

C++ Inheritance and Encapsulation

15: Polymorphism & Virtual Functions

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Short Notes of CS201

Interview Questions of C++

G Programming Languages - Fall 2012

CS201 - Introduction to Programming Glossary By


Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

OOP THROUGH C++(R16) int *x; float *f; char *c;

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Inheritance. Program construction in C++ for Scientific Computing. School of Engineering Sciences. Introduction. Michael Hanke.

Inclusion Polymorphism

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

04-24/26 Discussion Notes

CS11 Introduction to C++ Fall Lecture 7

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

CS105 C++ Lecture 7. More on Classes, Inheritance

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

CS 162, Lecture 25: Exam II Review. 30 May 2018

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Chapter 5 Object-Oriented Programming

Instantiation of Template class

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Syllabus of C++ Software for Hands-on Learning: This course offers the following modules: Module 1: Getting Started with C++ Programming

Introduction to Programming Using Java (98-388)

Tokens, Expressions and Control Structures

Fast Introduction to Object Oriented Programming and C++

Programming in C++: Assignment Week 6

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Inheritance, and Polymorphism.

Object-Oriented Concept

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

Inheritance and Polymorphism

Chapter 5. Object- Oriented Programming Part I

Object Model. Object Oriented Programming Spring 2015

Inheritance and Polymorphism

Object Oriented Programming

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Extending Classes (contd.) (Chapter 15) Questions:

Absolute C++ Walter Savitch

COMP322 - Introduction to C++

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Concepts of Programming Languages

CS OBJECT ORIENTED PROGRAMMING

Framework Fundamentals

Chapter 14 Abstract Classes and Interfaces

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CSE 303: Concepts and Tools for Software Development

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

More C++ : Vectors, Classes, Inheritance, Templates

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Object Oriented Programming in C++ Basics of OOP

Extending Classes (contd.) (Chapter 15) Questions:

EL2310 Scientific Programming

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Object-Oriented Programming

Transcription:

Polymorphism Polymorphism - is the property of OOP that allows the run-time binding of a function's name to the code that implements the function. (Run-time binding to the starting address of the code.) Compile-time binding of function names: Typically the compiler determines the code to be executed when a function call is made. At compile time the starting address of the code segment of the function called is inserted where the call occurs. ; class DC: public BC ; void BC::f1() void DC::f1() cout << "DC:: f1() called"<<endl; BC* ptr; int sel; do cout << " Which one? 1-BC, 2-DC, other - quit "; cin >> sel; if (sel ==1) ptr = new BC; else if (sel==2) ptr = new DC; else cout << "bye" << endl; if ((sel ==1) (sel == 2)) ptr->f1(); delete ptr; while ((sel==1) (sel ==2)); Output: Which one are you? 1-BC, 2-DC, other - quit 1 Which one are you? 1-BC, 2-DC, other - quit 2 Which one are you? 1-BC, 2-DC, other - quit 3 bye 08/25/18 1

Run-time binding of function names: The starting address of the function is determined during program execution. Some OOP languages only use run-time binding of functions - Smalltalk. Using same example insert virtual in front of BC declaration of f1(): virtual ; Output: Which one are you? 1-BC, 2-DC, other - quit 1 Which one are you? 1-BC, 2-DC, other - quit 2 DC:: f1() called Which one are you? 1-BC, 2-DC, other - quit 3 bye C++ Polymorphism Requirements: There must be an inheritance hierarchy The classes in the hierarchy must have a virtual method with the same signature (virtual is a keyword for method declaration only). If the base class has the method declared virtual then any derived class method of the same signature is automatically virtual. There must be either a pointer or a reference to a base class. The pointer or reference is used to invoke a virtual method. (Note: declaring a pointer to point to the base class data type allows it to also point to any derived class object.) Virtual table (vtable) - The table used for the run-time binding of virtual methods. A table exists for every virtual method. During execution it is determined which object a pointer points. This is used to lookup in the vtable the entry point of the method. The method is then executed. (Performance penalty and storage penalty) Constructors - Cannot be virtual. Proper constructors are always called because they are based on the object created. Destructors - Should be virtual. Compile bound destructors are based on the pointer declaration and will usually only call the base class destructor. Run-time binding leaves the destructor to run-time and then is based on the object. 08/25/18 2

BC(); ~BC(); ; class DC: public BC DC(); ~DC(); ; BC* ptr; int sel; do cout << endl << "========="<< endl; cout << " Which one? 1-BC, 2-DC, other - quit "; cin >> sel; if (sel ==1) ptr = new BC; else if (sel==2) ptr = new DC; else cout << "bye" << endl; BC::BC() cout <<"BC::Constr called" << endl; BC::~BC() cout <<"BC::DEstr called" << endl; if ((sel ==1) (sel == 2)) ptr->f1(); delete ptr; while ((sel==1) (sel ==2)); void BC::f1() DC::DC() cout <<"DC::Constr called" << endl; DC::~DC() cout <<"DC::DEstr called" << endl; void DC::f1() cout << "DC:: f1() called"<<endl; Output: ===================================== Which one are you? 1-BC, 2-DC, other - quit 1 BC::Constructor called BC::DEstructor called ==================================== Which one are you? 1-BC, 2-DC, other - quit 2 BC::Constructor called DC::Constructor called BC::DEstructor called ==================================== Which one are you? 1-BC, 2-DC, other - quit 3 bye 08/25/18 3

Making the destructor virtual solves the problem: BC(); virtual ~BC(); virtual ; Output: Which one are you? 1-BC, 2-DC, other - quit 1 BC::Constructor called BC::DEstructor called Which one are you? 1-BC, 2-DC, other - quit 2 BC::Constructor called DC::Constructor called DC:: f1() called DC::DEstructor called BC::DEstructor called Which one are you? 1-BC, 2-DC, other - quit 3 bye Press any key to continue 08/25/18 4

Name Overloading - functions or methods with the same name but different signatures. Compiler can distinguish which function start address to bind the call to. Example: a class with multiple constructors Example: void f1( int x); void f1(char c); Name Overriding - Methods with the same name and same signature. If the methods are virtual then run-time binding takes place. If they are not virtual, compile-time binding is at work. ; class DC: public BC ; BC* ptr; ptr = new DC; ptr->f1(); return 0; void BC::f1() void DC::f1() cout << "DC:: f1() called"<<endl; OUTPUT: (compile-time binding) BC::f1( ) called OUTPUT: (run-time binding - add virtual to BC::f1() ) DC::f1( ) called 08/25/18 5

Name Hiding - an inherited member has the same name (identifier) as the derived class but a different signature. void f1(int x); ; class DC: public BC ; DC d; d.f1( ); // d.f1(15); this will cause an error d.bc::f1(15); // this is ok return 0; void BC::f1(int x) void DC::f1() cout << "DC:: f1() called"<<endl; OUTPUT: (compile-time binding) DC::f1( ) called 08/25/18 6

Abstract Base Class - Creating an abstraction or basic template for derived classes. A class is considered to be abstract if it has one pure virtual function: virtual datatype function-name( parameters) = 0; virtual void Print( ) = 0; The derived class must override (create a same sign. function) this function to be nonabstract. Abstract classes cannot be instantiated to an object. virtual void f1() = 0; ; class DC: public BC ; // BC b ; ERROR DC d; d.f1( ); return 0; void DC::f1() cout << "DC:: f1() called"<<endl; OUTPUT: (compile-time binding) DC::f1( ) called 08/25/18 7

Run-time type identification - There are several ways to determine the type of an object at run-time. Check type conversion at run-time (dynamic_cast) Determine object's type at run-time (typeid) Extend RTTI (run-time type identification) provided by C++ // polymorphism - dynamic cast #include <typeinfo> virtual ; class DC1: public BC ; class DC2: public BC ; void BC::f1() void DC1::f1() cout << "DC1:: f1() called"<<endl; void DC2::f1() cout << "DC2:: f1() called"<<endl; 08/25/18 8

BC* ptr; DC1* p; int sel; do cout << endl << "==========================================" << endl; cout << " Which one are you? 1-DC1, 2-DC2, 3-BC, other - quit "; cin >> sel; if (sel ==1) ptr = new DC1; else if (sel==2) ptr = new DC2; else if (sel ==3) ptr = new BC; else cout << "bye" << endl; if ((sel ==1) (sel == 2) (sel == 3)) ptr->f1(); p = dynamic_cast<dc1 *> (ptr); if (p) cout << "DC1 CLASS" << endl; delete ptr; while ((sel==1) (sel ==2) (sel == 3)); return 0; 08/25/18 9

// polymorphism - typeid #include <typeinfo> virtual ; class DC1: public BC ; class DC2: public BC ; void BC::f1() void DC1::f1() cout << "DC1:: f1() called"<<endl; void DC2::f1() cout << "DC2:: f1() called"<<endl; 08/25/18 10

BC* ptr; int sel; do cout << endl << "===========================================" << endl; cout << " Which one are you? 1-DC1, 2-DC2, 3-BC, other - quit "; cin >> sel; if (sel ==1) ptr = new DC1; else if (sel==2) ptr = new DC2; else if (sel ==3) ptr = new BC; else cout << "bye" << endl; if ((sel ==1) (sel == 2) (sel == 3)) ptr->f1(); if (typeid(*ptr) == typeid(bc)) cout << "BASE CLASS" << endl; else if (typeid(*ptr) == typeid(dc1)) cout << "DC1 CLASS" << endl; else if (typeid(*ptr) == typeid(dc2)) cout << "DC2 CLASS" << endl; delete ptr; while ((sel==1) (sel ==2) (sel == 3)); return 0; 08/25/18 11