Understanding Polymorphism in Computer Science II for Majors
Lecture 15 continued with a review of inheritance, overriding, and the concept of polymorphism in computer science. Dr. Katherine Gibson covered the limitations of inheritance, virtual functions, abstract classes, and function types. The class discussed common errors in project 3 and highlighted today's objectives, focusing on virtual function tables, virtual constructors/destructors, and live coding applications.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
CMSC202 Computer Science II for Majors Lecture 15 Polymorphism (Continued) Dr. Katherine Gibson www.umbc.edu
Last Class We Covered Review of inheritance Overriding (vs overloading) Understanding polymorphism Limitations of inheritance Virtual functions Abstract classes & function types 2 www.umbc.edu
Any Questions from Last Time? www.umbc.edu
Common Project 3 Error Have you seen something like this? g++ -Wall -ansi g -c test1.cpp In file included from test1.cpp:4: TrainCar.h:49: error: expected constructor, destructor, or type conversion before & token The error is not in TrainCar.h! Where is it? The error is in test1.cpp Do not change the TrainCar.h file! 4 www.umbc.edu
Todays Objectives Review of polymorphism Limitations of inheritance Virtual functions Abstract classes & function types Finishing polymorphism Virtual function Tables Virtual destructors/constructors Livecoding application 5 www.umbc.edu
Review of Virtual and Polymorphism www.umbc.edu
Overview of Polymorphism Assume we have Vehicle *vehiclePtr = &myCar; And this method call: vehiclePtr->Drive(); prototype Vehicle class Can implement function Can create Vehicle Car class Can implement function Can create Car Calls Vehicle::Drive Can implement function Can create Car Calls Car::Drive Must implement function Can create Car Calls Car::Drive void Drive() Can implement function Can create Vehicle virtual void Drive() Cannot implement function Cannot create Vehicle virtual void Drive() = 0 7 www.umbc.edu
Overview of Polymorphism Assume we have Vehicle *vehiclePtr = &myCar; And this method call: vehiclePtr->Drive(); prototype Vehicle class Can implement function Can create Vehicle Car class Can implement function Can create Car Calls Vehicle::Drive Can implement function Can create Car Calls Car::Drive Must implement function Can create Car Calls Car::Drive void Drive() Can implement function Can create Vehicle virtual void Drive() Cannot implement function Cannot create Vehicle virtual void Drive() = 0 8 www.umbc.edu
Overview of Polymorphism Assume we have Vehicle *vehiclePtr = &myCar; And this method call: vehiclePtr->Drive(); prototype Vehicle class Can implement function Can create Vehicle Car class Can implement function Can create Car Calls Vehicle::Drive Can implement function Can create Car Calls Car::Drive Must implement function Can create Car Calls Car::Drive void Drive() If no Car::Drive implementation, calls Vehicle::Drive This is a pure virtual Can implement function Can create Vehicle virtual void Drive() function, and Vehicle is now an abstract class Cannot implement function Cannot create Vehicle virtual void Drive() = 0 9 www.umbc.edu
Virtual Function Tables www.umbc.edu
Behind the Scenes If our Drive()function is virtual, how does the compiler know which child class s version of the function to call? vector of Car* objects SUV SUV Jeep Van Jeep Sedan Sedan SUV 11 www.umbc.edu
Virtual Function Tables The compiler uses virtual function tables whenever we use polymorphism Virtual function tables are created for: Classes with virtual functions Child classes of those classes 12 www.umbc.edu
Virtual Table Pointer SUV SUV Jeep Van Jeep Sedan Sedan Van 13 www.umbc.edu
Virtual Table Pointer The compiler adds a hidden variable SUV SUV Jeep Van Jeep Sedan Sedan Van *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr 14 www.umbc.edu
Virtual Table Pointer The compiler also adds a virtual table of functions for each class SUV SUV Jeep Van Jeep Sedan Sedan Van *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr SUV virtual table Jeep virtual table Van virtual table Sedan virtual table 15 www.umbc.edu
Virtual Table Pointer Each virtual table has pointers to each of the virtual functions of that class SUV SUV Jeep Van Jeep Sedan Sedan Van *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr SUV virtual table Jeep virtual table Van virtual table Sedan virtual table * to SUV::Drive(); * to Jeep::Drive(); * to Van::Drive(); * to Sedan::Drive(); 16 www.umbc.edu
Virtual Table Pointer The hidden variable points to the appropriate virtual table of functions SUV SUV Jeep Van Jeep Sedan Sedan Van *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr SUV virtual table Jeep virtual table Van virtual table Sedan virtual table * to SUV::Drive(); * to Jeep::Drive(); * to Van::Drive(); * to Sedan::Drive(); 17 www.umbc.edu
Virtual Destructors/Constructors www.umbc.edu
Virtual Destructors Vehicle *vehicPtr = new Car; delete vehicPtr; For any class with virtual functions, you must declare a virtual destructor as well Why? Non-virtual destructors will only invoke the base class s destructor 19 www.umbc.edu
Virtual Constructors Not a thing... why? We use polymorphism and virtual functions to manipulate objects without knowing type or having complete information about the object When we construct an object, we have complete information There s no reason to have a virtual constructor 20 www.umbc.edu
Livecoding Animals (Bird, Cat, and Dog) All Animals can: Eat(), Speak(), and Perform() Vector of Animal pointers what happens? LIVECODING!!! 21 www.umbc.edu
Announcements Project 3 is due tonight! Exam 2 is in 1 week Will focus heavily on: Classes Inheritance Linked Lists Dynamic Memory Some Polymorphism 22 www.umbc.edu