virtual functions & polymorphism. geometric object example function also available in circle...

17
Virtual Functions & Polymorphism

Upload: quentin-byrd

Post on 20-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

Dispatch Normal functions only use "obvious" version of function:

TRANSCRIPT

Page 1: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Virtual Functions& Polymorphism

Page 2: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Geometric Object Example

Function also

availablein Circle & Rectagle Circle & Rectangle

could use toString() but chose to override

Page 3: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Dispatch

• Normal functions only use "obvious" version of function:

Page 4: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Virtual Functions

• virtual functionIf you are trying to use this behavior, first check for a new version in any subtypes I am

Page 5: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Virtual Functions

Everything else the same…

Page 6: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Polymorphism

• Polymorphism : many forms– Ability of one function call to produce different

results depending on exact type of object

Page 7: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Virtual Lookup

• On toString() call…– We know we have a

GeometricObject• Could use that toString()

GeometricObj&

GeometricObject

Circle

?

Page 8: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Virtual Lookup

• On toString() call…– We know we have a GeometricObject

• Could use that toString()

– But since virtual, go look for other version

GeometricObj&

GeometricObject

Circle

!!

Page 9: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

VTables

• Vtable : Table of function pointers– Objects have pointer to vtable for their class

Page 10: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

VTables

• Virtual Dispatch:– Start with object pointer

Page 11: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

VTables

• Virtual Dispatch:– Look up vtable address for this object

Page 12: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

VTables

• Virtual Dispatch:– Look up function address for desired function

Page 13: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

VTables

• Virtual Dispatch:– Start with object pointer– Look up vtable address for this object– Look up function address for desired function– Call that function

• Normal Dispatch:– Call the function

Page 14: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Virtual Advice

• Virtual tips:– Use anytime child class may need to change

behavior of a function– Slight overhead to vtable lookup

Page 15: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Virtual Advice

• Virtual tips:– Declare function virtual in parent .h

– No changes to .cpp– Do not have to declare as virtual in child classes

Page 16: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Other modifiers

• C++11 adds override and final– Final : Child classes cannot provide new versions– Override : Make sure I am actually overriding a

parent function

Page 17: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…

Last Virtual Tip

• Classes that are inherited from should declare virtual destructor

GeometricObject* gp = //mystery pointerdelete GeometricObject;If virtual – go see if we have

child destructor to callIf NOT virtual – only useGeometricObject's destructor