Chapter 15 Polymorphism and Virtual Functions Copyright © 2006 Pearson Addison- Wesley. All rights reserved. 15-2 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 Copyright © 2006 Pearson Addison- Wesley. All rights reserved. 15-3 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" Copyright © 2006 Pearson Addison- Wesley. All rights reserved. 15-4 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 Copyright © 2006 Pearson Addison- Wesley. All rights reserved. 15-5 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… Copyright © 2006 Pearson Addison- Wesley. All rights reserved. 15-6 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? Copyright © 2006 Pearson Addison- Wesley. All rights reserved. 15-7 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"! Copyright © 2006 Pearson Addison- Wesley. All rights reserved. 15-8 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 Copyright © 2006 Pearson Addison- Wesley. All rights reserved. 15-9 Virtual Functions: Another Example ♦ Bigger example best to demonstrate ♦ Record-keeping program for automotive parts store ♦ Track sales ♦ Don’t know all sales yet ♦ 1 st only regular retail sales ♦ Later: Discount sales, mail-order, etc. ♦ Depend on other factors besides just price, tax Copyright © 2006 Pearson Addison- Wesley. All rights reserved. 15-10 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! [...]... for standard functions ♦ So: ♦ Virtual functions changed: overridden ♦ Non -virtual functions changed: redefined Copyright © 2006 Pearson Addison- 15- 20 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... 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! Copyright © 2006 Pearson Addison- 15- 19 Overriding ♦ Virtual function definition changed in a derived class ♦ We say it’s been "overidden" ♦ Similar to redefined ♦ Recall: for standard functions ♦ So: ♦ Virtual. .. (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) Copyright © 2006 Pearson Addison- 15- 15 DiscountSale’s Implementation of bill() ♦ Virtual function in base class: ♦ "Automatically" virtual in derived class ♦ Derived class... Pearson Addison- 15- 34 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 Copyright © 2006 Pearson Addison- 15- 35 Summary... const; }; class Dog : public Pet { public: string breed; virtual void print() const; }; Copyright © 2006 Pearson Addison- 15- 25 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! Copyright © 2006 Pearson Addison- 15- 26 Using Classes Pet and Dog ♦ Anything that "is a" dog "is a" pet: ♦ vdog.name... Addison- 15- 21 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 © 2006 Pearson Addison- 15- 22... © 2006 Pearson Addison- 15- 31 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 © 2006 Pearson Addison- 15- 32 Casting ♦ Consider:... Pearson Addison- 15- 17 Virtual: Wow! ♦ Recall class Sale written long before derived class DiscountSale ♦ Members savings and " . 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++. Chapter 15 Polymorphism and Virtual Functions Copyright © 2006 Pearson Addison- Wesley. All rights reserved. 15- 2 Learning Objectives ♦ Virtual Function Basics ♦ Late. with virtual functions Copyright © 2006 Pearson Addison- Wesley. All rights reserved. 15- 3 Virtual Function Basics ♦ Polymorphism ♦ Associating many meanings to one function ♦ Virtual functions