CIS 190: C/C++ Programming. Lecture 11 Polymorphism

Similar documents
CMSC202 Computer Science II for Majors

Inheritance, and Polymorphism.

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

Inheritance and Polymorphism

Data Structures and Other Objects Using C++

Object-Oriented Concept

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

Project. C++: Inheritance III. Plan. Project. Before we begin. The final exam. Advanced Topics. Project. This week in the home stretch

Java Object Oriented Design. CSC207 Fall 2014

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

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

Data Structures (list, dictionary, tuples, sets, strings)

C++ Inheritance and Encapsulation

Polymorphism. Agenda

15: Polymorphism & Virtual Functions

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

Object-Oriented Programming

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

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

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Polymorphism Part 1 1

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

CGS 2405 Advanced Programming with C++ Course Justification

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

VIRTUAL FUNCTIONS Chapter 10

Object Orientated Programming Details COMP360

Inheritance, Polymorphism and the Object Memory Model

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Programming Exercise 14: Inheritance and Polymorphism

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

OVERRIDING. 7/11/2015 Budditha Hettige 82

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

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

What are the characteristics of Object Oriented programming language?

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Polymorphism. Arizona State University 1

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

Object Oriented Software Design II

CS-202 Introduction to Object Oriented Programming

And Even More and More C++ Fundamentals of Computer Science

Data Abstraction. Hwansoo Han

Chapter 14 Abstract Classes and Interfaces

Object Oriented Software Design II

This wouldn t work without the previous declaration of X. This wouldn t work without the previous declaration of y

ECE 3574: Dynamic Polymorphism using Inheritance

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

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

Object-Oriented Concepts and Design Principles

POLYMORPHISM Polymorphism: the type Polymorphism taking many shapes type of object

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

G52CPP C++ Programming Lecture 13

What is Inheritance?

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

INHERITANCE: EXTENDING CLASSES

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

CSE 12 Week Eight, Lecture One

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

step is to see how C++ implements type polymorphism, and this Exploration starts you on that journey.

Lecture 18 Tao Wang 1

Inheritance, Polymorphism, and Interfaces

Lecture Notes on Programming Languages

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

More Relationships Between Classes

G Programming Languages - Fall 2012

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

Lecture 15: Even more pointer stuff Virtual function table

CS 251 Intermediate Programming Inheritance

PROGRAMMING LANGUAGE 2

C++ Important Questions with Answers

OOP Fundamental Concepts

CS250 Final Review Questions

Reusable and Extendable Code. Composition Example (1) Composition Example (2) CS183-Su'02-Lecture 8 17 June by Eric A. Durant, Ph.D.

Binghamton University. CS-140 Fall Dynamic Types

Chapter 10 Introduction to Classes

Framework Fundamentals

COMP 110/L Lecture 20. Kyle Dewey

1. Write two major differences between Object-oriented programming and procedural programming?

Basics of Object Oriented Programming. Visit for more.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Inheritance and Polymorphism

Module 10 Inheritance, Virtual Functions, and Polymorphism

Object Oriented Programming with c++ Question Bank

CSC207 Week 3. Larry Zhang

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Inheritance and Polymorphism

Inheritance and object compatibility

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

Instance Members and Static Members

Chapter 5. Inheritance

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Transcription:

CIS 190: C/C++ Programming Lecture 11 Polymorphism 1

Outline Review of Inheritance Polymorphism Limitations Virtual Functions Abstract Classes & Function Types Virtual Function Tables Virtual Destructors/Constructors Application of Polymorphism Project Alphas 2

Review of Inheritance specialization through sub classes child class has direct access to parent member functions and variables that are??? 3

Review of Inheritance specialization through sub classes child class has direct access to parent member functions and variables that are public protected 4

Review of Inheritance specialization through sub classes child class has direct access to parent member functions and variables that are: public protected parent class has direct access to:??? in the child class 5

Review of Inheritance specialization through sub classes child class has direct access to parent member functions and variables that are: public protected parent class has direct access to: nothing in the child class 6

What is Inherited Parent Class public members protected members private variables private functions copy constructor assignment operator constructor destructor 7

What is Inherited Child Class Parent Class child class members (functions & variables) public members protected members private variables private functions copy constructor assignment operator constructor destructor 8

Outline Review of Inheritance Polymorphism Limitations Virtual Functions Abstract Classes & Function Types Virtual Function Tables Virtual Destructors/Constructors Application of Polymorphism Project Alphas 9

Car Example Car SUV Sedan Van Jeep class SUV: public Car {/*etc*/}; class Sedan: public Car {/*etc*/}; class Van: public Car {/*etc*/}; class Jeep: public Car {/*etc*/}; 10

Car Rental Example we want to implement a catalog of different types of cars available for rental how could we do this? 11

Car Rental Example we want to implement a catalog of different types of cars available for rental how could we do this? can accomplish this with a single vector using polymorphism 12

What is Polymorphism? ability to manipulate objects in a type-independent way 13

What is Polymorphism? ability to manipulate objects in a type-independent way already done to an extent via overriding child class overrides a parent class function 14

What is Polymorphism? ability to manipulate objects in a type-independent way already done to an extent via overriding child class overrides a parent class function can take it further using subtyping, AKA inclusion polymorphism 15

Using Polymorphism a pointer of a parent class type can point to an object of a child class type Vehicle *vehicleptr = &mycar; why is this valid? 16

Using Polymorphism a pointer of a parent class type can point to an object of a child class type Vehicle *vehicleptr = &mycar; why is this valid? because mycar is-a Vehicle 17

Polymorphism: Car Rental vector <Car*> rentallist; vector of Car* objects 18

Polymorphism: Car Rental vector <Car*> rentallist; vector of Car* objects SUV SUV Jeep Van Jeep Sedan Sedan SUV can populate the vector with any of Car s child classes 19

Outline Review of Inheritance Polymorphism Limitations Virtual Functions Abstract Classes & Function Types Virtual Function Tables Virtual Destructors/Constructors Application of Polymorphism Project Alphas 20

Limitations of Polymorphism parent classes do not inherit from child classes what about public member variables and functions? 21

Limitations of Polymorphism parent classes do not inherit from child classes not even public member variables and functions 22

Limitations of Polymorphism parent classes do not inherit from child classes not even public member variables and functions Vehicle *vehicleptr = &mycar; 23

Limitations of Polymorphism parent classes do not inherit from child classes not even public member variables and functions Vehicle *vehicleptr = &mycar; which version of PrintSpecs() does this call? vehicleptr->printspecs(); 24

Limitations of Polymorphism parent classes do not inherit from child classes not even public member variables and functions Vehicle *vehicleptr = &mycar; which version of PrintSpecs() does this call? vehicleptr->printspecs(); Vehicle::PrintSpecs() 25

Limitations of Polymorphism parent classes do not inherit from child classes not even public member variables and functions Vehicle *vehicleptr = &mycar; will this work? vehicleptr->repaintcar(); 26

Limitations of Polymorphism parent classes do not inherit from child classes not even public member variables and functions Vehicle *vehicleptr = &mycar; will this work? vehicleptr->repaintcar(); NO! RepaintCar() is a function of the Car child class, not the Vehicle class 27

Review of Inheritance Polymorphism Limitations Virtual Functions Abstract Classes & Function Types Virtual Function Tables Virtual Destructors/Constructors Application of Polymorphism Project Alphas 28

Virtual Functions can grant access to child methods by using virtual functions virtual functions are how C++ implements late binding used when the child class implementation is unknown or variable at parent class creation time 29

Late Binding simply put, binding is determined at run time as opposed to at compile time in the context of polymorphism, you re saying I don t know for sure how this function is going to be implemented, so wait until it s used and then get the implementation from the object instance. 30

Using Virtual Functions declare the function in the parent class with the keyword virtual in front virtual void Drive(); 31

Using Virtual Functions declare the function in the parent class with the keyword virtual in front virtual void Drive(); only use virtual with the prototype // don t do this virtual void Vehicle::Drive(); 32

Using Virtual Functions the corresponding child class function does not require the virtual keyword but 33

Using Virtual Functions the corresponding child class function does not require the virtual keyword should still include it, for clarity s sake makes it obvious the function is virtual, even without looking at the parent class // inside the Car class virtual void Drive(); 34

Outline Review of Inheritance Polymorphism Limitations Virtual Functions Abstract Classes & Function Types Virtual Function Tables Virtual Destructors/Constructors Application of Polymorphism Project Alphas 35

Function Types Virtual virtual void Drive(); parent class must have an implementation even if it s trivial or empty child classes may override if they choose to if not overridden, parent class definition used 36

Function Types Pure Virtual virtual void Drive() = 0; denote pure virtual by the = 0 at the end 37

Function Types Pure Virtual virtual void Drive() = 0; denote pure virtual by the = 0 at the end the parent class has no implementation of this function child classes must have an implementation 38

Function Types Pure Virtual virtual void Drive() = 0; denote pure virtual by the = 0 at the end the parent class has no implementation of this function child classes must have an implementation parent class is now an abstract class 39

Abstract Classes an abstract class is one that contains a function that is pure virtual 40

Abstract Classes an abstract class is one that contains a function that is pure virtual cannot declare abstract class objects why? 41

Abstract Classes an abstract class is one that contains a function that is pure virtual cannot declare abstract class objects why? this means abstract classes can only be used as base classes 42

Applying Virtual to Shape, etc. how should we label the following functions? (virtual, pure virtual, or leave alone) CalculateArea(); CalculatePerimeter(); Print(); SetColor(); 43

Outline Review of Inheritance Polymorphism Limitations Virtual Functions Abstract Classes & Function Types Virtual Function Tables Virtual Destructors/Constructors Application of Polymorphism Project Alphas 44

Behind the Scenes if our Drive() function is virtual, how does the compiler know which child class s version of the function to call? vector of Car* objects SUV SUV Jeep Van Jeep Sedan Sedan SUV 45

Virtual Function Tables the compiler uses virtual function tables whenever we use polymorphism virtual function tables are created for: what types of classes? 46

Virtual Function Tables the compiler uses virtual function tables whenever we use polymorphism virtual function tables are created for: classes with virtual functions child classes of those classes 47

Virtual Table Pointer SUV SUV Jeep Van Jeep Sedan Sedan Van 48

Virtual Table Pointer the compiler adds a hidden variable SUV SUV Jeep Van Jeep Sedan Sedan Van * vptr * vptr * vptr * vptr * vptr * vptr * vptr * vptr 49

Virtual Table Pointer the compiler also adds a virtual table of functions for each class SUV SUV Jeep Van Jeep Sedan Sedan Van * vptr * vptr * vptr * vptr * vptr * vptr * vptr * vptr SUV virtual table Jeep virtual table Van virtual table Sedan virtual table 50

Virtual Table Pointer each virtual table has pointers to each of the virtual functions of that class SUV SUV Jeep Van Jeep Sedan Sedan Van * vptr * vptr * vptr * vptr * vptr * vptr * vptr * vptr SUV virtual table Jeep virtual table Van virtual table Sedan virtual table * to SUV::Drive(); * to Jeep::Drive(); * to Van::Drive(); * to Sedan::Drive(); 51

Virtual Table Pointer the hidden variable points to the appropriate virtual table of functions SUV SUV Jeep Van Jeep Sedan Sedan Van * vptr * vptr * vptr * vptr * vptr * vptr * vptr * vptr SUV virtual table Jeep virtual table Van virtual table Sedan virtual table * to SUV::Drive(); * to Jeep::Drive(); * to Van::Drive(); * to Sedan::Drive(); 52

Virtual Everything! in Java, all functions are virtual by default everything seems to work fine for Java why don t we make all our functions virtual in C++ classes???? 53

Virtual Everything! in Java, all functions are virtual by default everything seems to work fine for Java why don t we make all our functions virtual in C++ classes? non-virtual functions can t be overridden (in the context of parent class pointers) creates unnecessary overhead 54

Outline Review of Inheritance Polymorphism Limitations Virtual Functions Abstract Classes & Function Types Virtual Function Tables Virtual Destructors/Constructors Application of Polymorphism Project Alphas 55

Virtual Destructors Vehicle *vehicptr = new Car; delete vehicptr; for any class with virtual functions, you must declare a virtual destructor as well why? 56

Virtual Destructors Vehicle *vehicptr = new Car; delete vehicptr; for any class with virtual functions, you must declare a virtual destructor as well non-virtual destructors will only invoke the base class s destructor 57

Virtual Constructors not a thing... why? 58

Virtual Constructors not a thing... why? we use polymorphism and virtual functions to manipulate objects without knowing type or having complete information about the object when we construct an object, we have complete information there s no reason to have a virtual constructor 59

Outline Review of Inheritance Polymorphism Limitations Virtual Functions Abstract Classes & Function Types Virtual Function Tables Virtual Destructors/Constructors Application of Polymorphism Project Alphas 60

Application of Polymorphism examine polymorphism and virtual functions using these classes: Animal Bird Cat Dog LIVECODING 61

Outline Review of Inheritance Polymorphism Limitations Virtual Functions Abstract Classes & Function Types Virtual Function Tables Virtual Destructors/Constructors Application of Polymorphism Project Alphas 62

Project Alphas due next Sunday (November 23rd) doesn t: have to be working a complete project in a folder named <your_team_name> 63

Next Time take an (anonymous) in-class survey for 1% overall extra credit receive feedback on your project proposal have some say in what we cover during our last (gasp! sob!) class together 64