Understanding Polymorphism in Computer Science II for Majors

Slide Note
Embed
Share

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.


Uploaded on Sep 22, 2024 | 0 Views


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


  1. CMSC202 Computer Science II for Majors Lecture 15 Polymorphism (Continued) Dr. Katherine Gibson www.umbc.edu

  2. 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

  3. Any Questions from Last Time? www.umbc.edu

  4. 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

  5. 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

  6. Review of Virtual and Polymorphism www.umbc.edu

  7. 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

  8. 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

  9. 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

  10. Virtual Function Tables www.umbc.edu

  11. 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

  12. 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

  13. Virtual Table Pointer SUV SUV Jeep Van Jeep Sedan Sedan Van 13 www.umbc.edu

  14. 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

  15. 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

  16. 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

  17. 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

  18. Virtual Destructors/Constructors www.umbc.edu

  19. 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

  20. 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

  21. Livecoding Animals (Bird, Cat, and Dog) All Animals can: Eat(), Speak(), and Perform() Vector of Animal pointers what happens? LIVECODING!!! 21 www.umbc.edu

  22. 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

Related