1 Virtual Functions and Polymorphism Chapter 10. 2 What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
Inheritance, Polymorphism, and Virtual Functions
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,
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 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Cpt S 122 – Data Structures 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.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Object Oriented Programming
Chapter -6 Polymorphism
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Object-Oriented Programming: Inheritance and Polymorphism.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
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.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
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,
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
Inheritance and Run time Polymorphism
Polymorphism & Virtual Functions
Inheritance, Polymorphism, and Virtual Functions
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 20- Virtual Functions and Polymorphism
“Under the Hood” of Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Chapter 20 - C++ Virtual Functions and Polymorphism
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
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:

1 Virtual Functions and Polymorphism Chapter 10

2 What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes

3 Problem with Derived Classes Given the class hierarchy below Consider the existence of a draw function for each derived class Consider also an array of pointers to the base class (which can also point to various objects of the derived classes) How do you specify which print statement to be called when referencing the pointers

4 Solution 1: Type Casting For objects of other than base type Shape *shapePtr = (Shape*) item[i]; Triangle *trianglePtr = (Triangle*) item[i+1]; Poor solution –clumsy –must keep track of correct object type for each item[i]

5 Solution 2: Variant Records Rewrite Shape class –private member is a variant record –consists of unions of structs for all possible derived types –Display function would use switch stmt to determine which struct should be used But -- contradicts OOP philosophy –object would not be autonomous -- knowing how to manipulate its own data –when new derived class created, base class must be adapted! (See Definition)

6 Solution 3 : Type Fields and switch Statements Could use switch statements to specify which print function (according to which class) will be used But … Must remember to –make a type test –test all possible cases in a switch –modify switch statements when new classes are derived

7 Real Solution Virtual functions Polymorphic programming

8 Virtual functions Declare the draw function in the base class as a virtual function then override draw in each of the derived classes to draw the appropriate shape virtual void draw ( ) const; shapePtr->draw( ); /* correct draw( ) function will be used, no matter what shape is pointed to */

9 Virtual functions When virtual function called via pointer ->, the reference is resolved at run time –called dynamic binding When virtual function called by referencing a specific object (using dot operator) reference is resolved at compile time –called static binding

10 Abstract Base Classes When we declare a class (as a type) we usually intend to declare objects of that type –but … not always An abstract class is declared, not to be instantiated –usually are used in inheritance situation –thus called abstract base class –provides base class for intended derived classes

11 Concrete Classes concreteClass from which objects will actually be instantiated called a concrete class Example: AbstractAbstract Classes Shape TwoD_Shape ThreeD_Shape Square Circle Triangle Cube Sphere Cylinder Shape TwoD_Shape ThreeD_Shape Square Circle Triangle Cube Sphere Cylinder

12 Polymorphism Ability for objects –of different classes –related by inheritance Implemented with virtual functions When request made thru base-class pointer (or reference) to use a virtual function –C++ knows enough to choose the correct overriden function To respond differently to same member function call

13 Polymorphism A class is made abstract by declaring one or more of its virtual functions to be "pure" A pure virtual function is one with an initializer of =0 in its declaration If a class is derived from a class with a pure virtual function & no defn for that pure funct is provided –that class is also an abstract class –not possible to instantiate any objects

14 Case Study: Payroll System Refer to figure 10.1 and audio of Text CD Demonstration of –pure virtual functions –polymorphism –static binding –dynamic binding

15 New Classes & Dynamic Binding New classes accommodated by dynamic binding –also called late binding Type of an object not needed at compile time for a virtual function Type of virtual function determined at run time This makes adding new capabilities to systems easier to do

16 Dynamic Binding Enables a programmer to distribute software without revealing how something is done –distribute only header and.obj files of base classes Another programmer who purchases the routines writes derived classes –dynamic (late or runtime) binding of virtual functions are easily accommodated

17 Virtual Destructors Problems can occur when –using polymorphism –processing dynamically allocated objects –in a class hierarchy Consider the delete applied to base- class pointer to an object –base class destructor called –what if the pointer referenced a derived class object -- other class members deleted??

18 Virtual Destructors Solution is to make all derived-class destructors to be virtual functions This causes the appropriate destructor to be called –the system knows which derived class destructor applies –the successive destructors on up the hierarchy are also called

19 Virtual Destructors If a class contains virtual functions –declare the base class destructor to be virtual Then if an object in the hierarchy is destroyed explicitly with delete on a base-class pointer to a derived class object –destructor for appropriate class is called

20 Case Study Inheriting Interface & Implementation Figure access the audio for the text CD Note features of program –abstract base class Shape –virtual functions –some derived classes exclude area, volume functions(if not needed) –calls to appropriate functions higher up in the hierarchy –use of pointers, reference parameters

21 Polymorphism, virtual Functions, Dynamic Binding "Under the Hood" Dynamic binding requires calls to virtual member functions be given to appropriate version of the function This is done "under the hood" with a function table called the vtable –contains pointers to each version of the virtual functions System chooses virtual function defined for that class (or inherited from higher base class)

22 Polymorphism, virtual Functions, Dynamic Binding "Under the Hood" Derived class is not required to override the virtual function in higher base class –it can use base class version of the function –vtable would reflect this Appropriate function pointer in vtable dereferenced at run time –does require some execution time overhead See Fig 10.3, pg 591, hear CD audio

23 Unions defn => a struct that holds only one of its members at a time during program execution union Qty_type { int count; float linear_ft; long cu_in; }; Qty_type how_many; At run time, how_many contains either an int a float a long... But … never all three Back