New version page

FSU COP 3330 - Polymorphism and Virtual Functions

Upgrade to remove ads

This preview shows page 1-2-17-18-19-36-37 out of 37 pages.

Save
View Full Document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience

Upgrade to remove ads
Unformatted text preview:

Chapter 15Learning ObjectivesVirtual Function BasicsFigures ExampleFigures Example 2Figures Example: center()Figures Example: New FigureFigures Example: Virtual!Virtual Functions: Another ExampleVirtual Functions: Auto PartsClass Sale DefinitionMember Functions savings and operator <Class SaleDerived Class DiscountSale DefinedDiscountSale’s Implementation of bill()Slide 16Derived Class DiscountSaleVirtual: Wow!Virtual: How?OverridingVirtual Functions: Why Not All?Pure Virtual FunctionsAbstract Base ClassesExtended Type CompatibilityExtended Type Compatibility ExampleClasses Pet and DogUsing Classes Pet and DogSlicing ProblemSlicing Problem FixSlicing Problem ExampleSlide 31Virtual DestructorsCastingDowncastingInner Workings of Virtual FunctionsSummary 1Summary 2Chapter 15Polymorphism and Virtual FunctionsCopyright © 2008 Pearson Addison-Wesley. All rights reservedLearning 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 functions15-2Copyright © 2008 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-orientedprogramming!•Virtual–Existing in "essence" though not in fact•Virtual Function–Can be "used" before it’s "defined"15-3Copyright © 2008 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 figure15-4Copyright © 2008 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 drawc.draw(); //Calls Circle class’s draw•Nothing new here yet…15-5Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Figures Example: center()•Parent class Figure contains functionsthat 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?15-6Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Figures Example: New Figure•Consider new kind of figure comes along:Triangle classderived 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 functionTriangle::draw() NOT function Figure::draw()–But class Triangle wasn’t even WRITTEN whenFigure::center() was! Doesn’t know "triangles"!15-7Copyright © 2008 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 objectinstance"•Called late binding or dynamic binding–Virtual functions implement late binding15-8Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Virtual Functions: Another Example•Bigger example best to demonstrate•Record-keeping program for automotiveparts 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, tax15-9Copyright © 2008 Pearson Addison-Wesley. All rights reserved.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 willbe added "later"!•When different types of sales added!•So function for "computing a bill" will be virtual!15-10Copyright © 2008 Pearson Addison-Wesley. All rights reserved.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;};15-11Copyright © 2008 Pearson Addison-Wesley. All rights reserved.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()!15-12Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Class Sale•Represents sales of single item with noadded discounts or charges.•Notice reserved word "virtual" indeclaration of member function bill–Impact: Later, derived classes of Sale candefine THEIR versions of function bill–Other member functions of Sale will useversion based on object of derived class!–They won’t automatically use Sale’s version!15-13Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Derived Class DiscountSale Defined•class DiscountSale : public Sale{public:DiscountSale();DiscountSale( double thePrice,double the Discount);double getDiscount() const;void setDiscount(double newDiscount);double bill() const;private:double discount;};15-14Copyright © 2008 Pearson Addison-Wesley. All rights reserved.DiscountSale’s Implementation of bill()•double DiscountSale::bill() const{double fraction = discount/100;return (1 – fraction)*getPrice();}•Qualifier "virtual" does not go in actualfunction definition–"Automatically" virtual in derived class–Declaration (in interface) not required to have"virtual" keyword either (but usually does)15-15Copyright © 2008 Pearson Addison-Wesley. All rights reserved.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 readability15-16Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Derived Class DiscountSale•DiscountSale’s member function


View Full Document
Download Polymorphism and Virtual Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Polymorphism and Virtual Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Polymorphism and Virtual Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?