Polymorphism and Virtual Functions

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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Polymorphism, Virtual Methods and Abstract Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Inheritance.
Polymorphism and Virtual Functions. class Ball : public Sphere.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism 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 6: Polymorphism - The fourth pillar of OOP - 1.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Virtual Functions Fall 2008 Dr. David A. Gaitros
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 15 Inheritance. Slide Overview 15.1 Inheritance Basics 15.2 Inheritance Details 15.3 Polymorphism.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Problem Solving with C++ The Object of Programming
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Chapter 15 Polymorphism and Virtual Functions. Learning Objectives Virtual Function Basics – Late binding – Implementing virtual functions – When to use.
Slide 1 Polymorphism and Virtual Functions. Slide 2 Learning Objectives  Virtual Function Basics  Late binding  Implementing virtual functions  When.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Programming With Java ICS Chapter 8 Polymorphism.
Polymorphism and Virtual Functions. Motivation Polymorphism is one of the fundamental mechanisms offered by OOP, and it is directly related to inheritance.
Copyright 2006 Pearson Addison-Wesley, 2008, 2009, 2013 Joey Paquet 6-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
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.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
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.
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
Chapter 15 Inheritance. Chapter 15 Inheritance.
Polymorphism, Virtual Methods and Abstract Classes
Polymorphism Templates
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Advanced Program Design with C++
7. Inheritance and Polymorphism
Polymorphism.
Polymorphism & Virtual Functions
Polymorphism and Virtual Functions
Polymorphism, Virtual Methods and Abstract Classes
Chapter 15 & additional topics
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Learning Objectives Inheritance Virtual Function.
Inheritance Chapter 15 & additional topics.
Chapter 15 & additional topics
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:

Polymorphism and Virtual Functions 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 Pointers and Virtual Functions Extended type compatibility Downcasting and upcasting C++ "under the hood" with virtual functions

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"

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

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…

Figures Example: center() Parent class Figure contains functions that apply to "all" figures; consider: center(): moves a figure to center of screen Erases 1st, then re-draws So Figure::center() would use function draw() to re-draw Complications! Which draw() function? From which class?

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"!

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

Virtual Functions: Another Example Bigger example best to demonstrate Record-keeping program for automotive parts store Track sales Don’t know all sales yet 1st only regular retail sales Later: Discount sales, mail-order, etc. Depend on other factors besides just price, tax

Virtual Functions: Auto Parts Program must: Compute daily gross sales Calculate largest/smallest sales of day Perhaps average sale for day All come from individual bills But many functions for computing bills will be added "later"! When different types of sales added! So function for "computing a bill" will be virtual!

Class Sale Definition class Sale { public: Sale(); Sale(double thePrice); double getPrice() const; virtual double bill() const; double savings(const Sale& other) const; private: double price; };

Member Functions savings and operator < double Sale::savings(const Sale& other) const { return (bill() – other.bill()); } bool operator < ( const Sale& first, const Sale& second) { return (first.bill() < second.bill()); } Notice BOTH use member function bill()!

Class Sale Represents sales of single item with no added discounts or charges. Notice reserved word "virtual" in declaration of member function bill Impact: Later, derived classes of Sale can define THEIR versions of function bill Other member functions of Sale will use version based on object of derived class! They won’t automatically use Sale’s version!

Derived Class DiscountSale Defined class DiscountSale : public Sale { public: DiscountSale(); DiscountSale(double thePrice,double theDiscount); double getDiscount() const; void setDiscount(double newDiscount); double bill() const; private: double discount; };

DiscountSale’s Implementation of bill() double DiscountSale::bill() const { double fraction = discount/100; return (1 – fraction)*getPrice(); } Qualifier "virtual" does not go in actual function definition "Automatically" virtual in derived class Declaration (in interface) not required to have "virtual" keyword either (but usually does)

DiscountSale’s Implementation of bill() Virtual function in base class: "Automatically" virtual in derived class Derived class declaration (in interface) Not required to have "virtual" keyword But typically included anyway, for readability

Derived Class DiscountSale DiscountSale’s member function bill() implemented differently than Sale’s Particular to "discounts" Member functions savings and "<" Will use this definition of bill() for all objects of DiscountSale class! Instead of "defaulting" to version defined in Sales class!

Virtual: Wow! Recall class Sale written long before derived class DiscountSale Members savings and "<" compiled before even had ideas of a DiscountSale class Yet in a call like: DiscountSale d1, d2; d1.savings(d2); Call in savings() to function bill() knows to use definition of bill() from DiscountSale class Powerful!

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!

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

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

Self-Test Exercise 1 Explain the difference among the terms virtual function, late binding, and polymorphism. In essence there is no difference among the three terms. They all refer to the same topic. There is only a slight difference in their usage. (Virtual function is a kind of member function, late binding refers to the mechanism used to decide which function definition to use when a function is virtual, and polymorphism is another for late binding.)

Self-Test Exercise 2 Suppose you modify the definition of the class Sale (Display 15.1) by deleting the reserved word virtual. How would that change the output of the program in Display 15.5? The output would change to the following: Discounted item is not cheaper.

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;

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

Self-Test Exercise 3 Is it legal to have an abstract class in which all member functions are pure virtual functions? Yes, it is legal to have an abstract class in which all member functions are pure virtual functions.

Self-Test Exercise 4 Given the definition of the class Employee in Display 15.6, which of the following are legal? a. Employee joe; joe = Employee(); b. class HourlyEmployee: public Employee { public: HourlyEmployee(); <Some more legal member function definition, none of which are pure virtual functions.> private: double wageRate; double hours; } int main() joe = HourlyEmployee(); c. bool isBossOf(const Employee& e1, const Employee& e2); Illegal, because Employee is an abstract class. Legal Legal, because an abstract class is a type.

Extended Type Compatibility Given: Derived is derived class of Base Derived objects can be assigned to objects of type Base But NOT the other way! Consider previous example: A DiscountSale "is a" Sale, but reverse not true

Extended Type Compatibility Example class Pet { public: string name; virtual void print() const; }; class Dog : public Pet { public: string breed; virtual void print() const; };

Classes Pet and Dog Now given declarations: Dog vdog; Pet vpet; Notice member variables name and breed are public! For example purposes only! Not typical!

Using Classes Pet and Dog Anything that "is a" dog "is a" pet: vdog.name = "Tiny"; vdog.breed = "Great Dane"; vpet = vdog; These are allowable Can assign values to parent-types, but not reverse A pet "is not a" dog (not necessarily)

Slicing Problem Notice value assigned to vpet "loses" it’s breed field! cout << vpet.breed; Produces ERROR msg! Called slicing problem Might seem appropriate Dog was moved to Pet variable, so it should be treated like a Pet And therefore not have "dog" properties Makes for interesting philosphical debate

Slicing Problem Fix In C++, slicing problem is nuisance It still "is a" Great Dane named Tiny We’d like to refer to it’s breed even if it’s been treated as a Pet Can do so with pointers to dynamic variables

Slicing Problem Example Pet *ppet; Dog *pdog; pdog = new Dog; pdog->name = "Tiny"; pdog->breed = "Great Dane"; ppet = pdog; Cannot access breed field of object pointed to by ppet: cout << ppet->breed; //ILLEGAL!

Slicing Problem Example Must use virtual member function: ppet->print(); Calls print member function in Dog class! Because it’s virtual C++ "waits" to see what object pointer ppet is actually pointing to before "binding" call void Dog::print() const { cout << “name: ” << name << endl; cout << “breed: ” << breed << endl; }

Self-Test Exercise 5 Why can’t you assign a base class object to a derived class variable? There would be no members to assign to the derived class’s added members.

Self-Test Exercise 6 What is the problem with the (legal) assignment of a derived class object to a base class variable? Although it is legal to assign a derived class object to a base class variable, this discards the parts of the derived class object that are not members of the base class. This situation is known as the slicing problem.

Self-Test Exercise 7 Suppose the base class and the derived class each has a member function with the same signature. When you have a base class pointer to a derived class object and call a function member through the pointer, discuss what determines which function is actually called, the base class member function or the derived class member function. If the base class function carries the virtual modifier, then the derived class member function is called. If the base member function does not have the virtual modifier, then the base class member function is called.

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

Casting Consider: Pet vpet; Dog vdog; … vdog = static_cast<Dog>(vpet); //ILLEGAL! Can’t cast a pet to be a dog, but: vpet = vdog; // Legal! vpet = static_cast<Pet>(vdog); //Also legal! Upcasting is OK From descendant type to ancestor type

Downcasting Downcasting dangerous! Casting from ancestor type to descended type Assumes information is "added" Can be done with dynamic_cast: Pet *ppet; ppet = new Dog; Dog *pdog = dynamic_cast<Dog*>(ppet); Legal, but dangerous! Downcasting rarely done due to pitfalls Must track all information to be added All member functions must be virtual

Inner Workings of Virtual Functions Don’t need to know how to use it! Principle of information hiding Virtual function table Compiler creates it Has pointers for each virtual member function Points to location of correct code for that function Objects of such classes also have pointer Points to virtual function table

Self-Test Exercise 8 Why is the following illegal? Pet vpet; Dog vdog; //Dog is a derived class with base class Pet. … vdog = static_cast<Dog>(vpet); //ILLEGAL! Since Dog can have more member variables than pet, the object vpet may not have enough data for all the member variables of type Dog.

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

Summary 2 Derived class objects can be assigned to base class objects Base class members are lost; slicing problem Pointer assignments and dynamic objects Allow "fix" to slicing problem Make all destructors virtual Good programming practice Ensures memory correctly de-allocated

Homework2 Programming Project 1 (page 680) Forms: 2 program versions (2 projects) 1 report (學號.doc) Programming Project 2 (page 681) 1 program versions (1 project) Due date: 5/28 pm 12:00

Programming Project 1 Consider a graphics system that has classes for various figures, say rectangles, squares, triangles, circles, and so on. For example, a rectangle might have data members height, width, and center point, while a square and circle might have only a center point and an edge length or radius, respectively. In a well-designed system these would be derived from a common classes, Figure. You are to implement such a system.

The class Figure is the base class The class Figure is the base class. You should add only Rectangle and Triangle classes derived from Figure. Each class has stubs from member function erase and draw. Each of these member functions outputs a message telling what function has been called and what the class of the calling object is. Since these are just stubs, they do nothing more than output this message. The member function center calls erase and draw to erase and redraw the figure at the center. Because you have only stubs for erase and draw, center will not do any “centering” but will call the member functions erase and draw. Also, add an output message in the member function center that announces that center is being called. The member functions should take no arguments. There are three parts parts to this project:

Do the class definition using no virtual functions. Compile and test. Make the base class member functions virtual. Compile and test. Explain the difference in results.