Chapter 15 Polymorphism and Virtual Functions. Learning Objectives Virtual Function Basics – Late binding – Implementing virtual functions – When to use.

Slides:



Advertisements
Similar presentations
Chapter 15 Polymorphism and Virtual Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Virtual Function.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Inheritance.
Polymorphism and Virtual Functions. class Ball : public Sphere.
Chapter 1 The Facts to Be Explained. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 1-2.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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 6: Polymorphism - The fourth pillar of OOP - 1.
Virtual Functions Fall 2008 Dr. David A. Gaitros
Chapter 15 Inheritance. Slide Overview 15.1 Inheritance Basics 15.2 Inheritance Details 15.3 Polymorphism.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Polymorphism and Virtual Functions
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Slide 1 Polymorphism and Virtual Functions. Slide 2 Learning Objectives  Virtual Function Basics  Late binding  Implementing virtual functions  When.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Polymorphism and Virtual Functions. Motivation Polymorphism is one of the fundamental mechanisms offered by OOP, and it is directly related to inheritance.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Copyright 2006 Pearson Addison-Wesley, 2008, 2009, 2013 Joey Paquet 6-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
ISBN Object-Oriented Programming Chapter Chapter
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Learning Objectives Relationships Among Objects in an Inheritance Hierarchy. Invoking Base-class Functions from Derived Class Objects. Aiming Derived-Class.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Virtual Function and Polymorphism
CMSC 202 Polymorphism.
Polymorphism and Virtual Functions
Polymorphism Templates
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Advanced Program Design with C++
Polymorphism.
Polymorphism & Virtual Functions
Polymorphism and Virtual Functions
Polymorphism Lec
Learning Objectives Inheritance Virtual Function.
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
CISC/CMPE320 - Prof. McLeod
VIRTUAL FUNCTIONS RITIKA SHARMA.
Polymorphism 2019/4/29.
Ninth step for Learning C++ Programming
C++ Polymorphism Reference and pointer implicit type casting
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Chapter 15 Polymorphism and Virtual Functions

Learning Objectives Virtual Function Basics – Late binding – Implementing virtual functions – When to use a virtual function – Abstract classes and pure virtual functions 15-2 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Virtual Function Basics Polymorphism – Associating many meanings to one function – Virtual functions provide this capability – Fundamental principle of object-oriented programming! Virtual – Existing in "essence" though not in fact Virtual Function – Can be "used" before it’s "defined" 15-3 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Figures Example Best explained by example: Classes for several kinds of figures – Rectangles, circles, ovals, etc. – Each figure an object of different class Rectangle data: height, width, center point Circle data: center point, radius All derive from one parent-class: Figure Require function: draw() – Different instructions for each figure 15-4 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Figures Example 2 Each class needs different draw function Can be called "draw" in each class, so: Rectangle r; Circle c; r.draw(); //Calls Rectangle class’s draw c.draw(); //Calls Circle class’s draw Nothing new here yet… 15-5 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Figures Example: center() Parent class Figure contains functions that apply to "all" figures; consider: center(): moves a figure to center of screen – Erases 1 st, then re-draws – So Figure::center() would use function draw() to re-draw – Complications! Which draw() function? From which class? 15-6 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Figures Example: New Figure Consider new kind of figure comes along: Triangle class derived from Figure class Function center() inherited from Figure – Will it work for triangles? – It uses draw(), which is different for each figure! – It will use Figure::draw()  won’t work for triangles Want inherited function center() to use function Triangle::draw() NOT function Figure::draw() – But class Triangle wasn’t even WRITTEN when Figure::center() was! Doesn’t know "triangles"! 15-7 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Figures Example: Virtual! Virtual functions are the answer Tells compiler: – "Don’t know how function is implemented" – "Wait until used in program" – "Then get implementation from object instance" Called late binding or dynamic binding – Virtual functions implement late binding 15-8 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Virtual: How? To write C++ programs: – Assume it happens by "magic"! But explanation involves late binding – Virtual functions implement late binding – Tells compiler to "wait" until function is used in program – Decide which definition to use based on calling object Very important OOP principle! 15-9 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Overriding Virtual function definition changed in a derived class – We say it’s been "overidden" Similar to redefined – Recall: for standard functions So: – Virtual functions changed: overridden – Non-virtual functions changed: redefined Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Virtual Functions: Why Not All? Clear advantages to virtual functions as we’ve seen One major disadvantage: overhead! – Uses more storage – Late binding is "on the fly", so programs run slower So if virtual functions not needed, should not be used Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Pure Virtual Functions Base class might not have "meaningful" definition for some of it’s members! – It’s purpose solely for others to derive from Recall class Figure – All figures are objects of derived classes Rectangles, circles, triangles, etc. – Class Figure has no idea how to draw! Make it a pure virtual function: virtual void draw() = 0; Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Abstract Base Classes Pure virtual functions require no definition – Forces all derived classes to define "their own" version Class with one or more pure virtual functions is: abstract base class – Can only be used as base class – No objects can ever be created from it Since it doesn’t have complete "definitions" of all it’s members! If derived class fails to define all pure’s: – It’s an abstract base class too Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Virtual Destructors Recall: destructors needed to de-allocate dynamically allocated data Consider: Base *pBase = new Derived; … delete pBase; – Would call base class destructor even though pointing to Derived class object! – Making destructor virtual fixes this! Good policy for all destructors to be virtual Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Summary 1 Late binding delays decision of which member function is called until runtime – In C++, virtual functions use late binding Pure virtual functions have no definition – Classes with at least one are abstract – No objects can be created from abstract class – Used strictly as base for others to derive Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Summary 2 Make all destructors virtual – Good programming practice – Ensures memory correctly de-allocated Copyright © 2012 Pearson Addison-Wesley. All rights reserved.