virtual functions & polymorphism. geometric object example function also available in circle...
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…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/1.jpg)
Virtual Functions& Polymorphism
![Page 2: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/2.jpg)
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…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/3.jpg)
Dispatch
• Normal functions only use "obvious" version of function:
![Page 4: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/4.jpg)
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…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/5.jpg)
Virtual Functions
Everything else the same…
![Page 6: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/6.jpg)
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…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/7.jpg)
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…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/8.jpg)
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…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/9.jpg)
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…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/10.jpg)
VTables
• Virtual Dispatch:– Start with object pointer
![Page 11: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/11.jpg)
VTables
• Virtual Dispatch:– Look up vtable address for this object
![Page 12: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/12.jpg)
VTables
• Virtual Dispatch:– Look up function address for desired function
![Page 13: Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/13.jpg)
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…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/14.jpg)
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…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/15.jpg)
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…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/16.jpg)
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…](https://reader035.vdocuments.mx/reader035/viewer/2022081517/5a4d1c137f8b9ab0599f7f13/html5/thumbnails/17.jpg)
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