computer science ii for majors
play

Computer Science II for Majors Lecture 15 Polymorphism (Continued) - PowerPoint PPT Presentation

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


  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. Today’s 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 Car class • Can implement function • Can implement function void Drive() • Can create Vehicle • Can create Car • Calls Vehicle::Drive • Can implement function • Can implement function virtual void • Can create Vehicle • Can create Car Drive() • Calls Car::Drive • Cannot implement function • Must implement function virtual void • Cannot create Vehicle • Can create Car Drive() = 0 • Calls Car::Drive 7 www.umbc.edu

  8. Overview of Polymorphism • Assume we have Vehicle *vehiclePtr = &myCar; • And this method call: vehiclePtr->Drive(); prototype Vehicle class Car class • Can implement function • Can implement function void Drive() • Can create Vehicle • Can create Car • Calls Vehicle::Drive • Can implement function • Can implement function virtual void • Can create Vehicle • Can create Car Drive() • Calls Car::Drive • Cannot implement function • Must implement function virtual void • Cannot create Vehicle • Can create Car Drive() = 0 • Calls Car::Drive 8 www.umbc.edu

  9. Overview of Polymorphism • Assume we have Vehicle *vehiclePtr = &myCar; • And this method call: vehiclePtr->Drive(); prototype Vehicle class Car class • Can implement function • Can implement function void Drive() • Can create Vehicle • Can create Car If no Car::Drive • Calls Vehicle::Drive implementation, calls This is a pure virtual • Can implement function • Can implement function virtual void Vehicle::Drive • Can create Vehicle • Can create Car function, and Vehicle is Drive() • Calls Car::Drive now an abstract class • Cannot implement function • Must implement function virtual void • Cannot create Vehicle • Can create Car Drive() = 0 • Calls Car::Drive 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? 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

Recommend


More recommend