CMSC 202 - Lec17 - Polymorphism (Cont)
CMSC 202 - Lec17 - Polymorphism (Cont)
Computer Science II
Lecture 17 -
Polymorphism (cont'd)
• Understanding polymorphism
• Limitations of inheritance
• Virtual functions
• Abstract classes & function types
Any Questions from Last Time?
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
Review of
Inheritance vs Polymorphism
Inheritance
• Using non-virtual functions a derived classes can:
1. Use a base class’s public and protected functions
• The function will not exist in the child class. Child uses parent function.
2. Replace or Override a base class’s public and protected functions
• The function has the same signature as the parent class.
3. Extend a base class’s public and protected functions
• The function has a different signature as the parent class
Problem: If I replace the function, how can I still use the parent version
of the function?
Scope Resolution
Polymorphism
• Polymorphism refers to the ability to associate many meanings with
one function name by means of a special mechanism known as
virtual functions or late binding.
Polymorphism
• Using virtual functions a derived classes can:
1. Override a base class’s public and protected functions
• The function has the same signature as the parent class.
2. Overload a base class’s public and protected functions
• The function has a different signature as the parent class
Abstract Classes &
Function Types
Function Types – Virtual
virtual void Drive();
SUV virtual table Jeep virtual table Van virtual table Sedan virtual table
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
SUV virtual table Jeep virtual table Van virtual table Sedan virtual table
* to SUV::Drive(); * to Jeep::Drive(); * to Van::Drive(); * to Sedan::Drive();
Virtual Table Pointer
• The hidden variable points to the
appropriate virtual table of functions
SUV SUV Jeep Van Jeep Sedan Sedan Van
SUV virtual table Jeep virtual table Van virtual table Sedan virtual table
* to SUV::Drive(); * to Jeep::Drive(); * to Van::Drive(); * to Sedan::Drive();
Virtual Destructors/Constructors
Virtual Destructors
Vehicle *vehicPtr = new Car;
delete vehicPtr;
• Why?
• Non-virtual destructors will only
invoke the base class’s destructor
Virtual Constructors
• Not a thing... why?
LIVECODING!!!
Live Coding
Lec17–> pet.cpp
Announcements
• Prelab Quizzes (4 pts)
• Released every Friday by 10am on Blackboard
• Due every Monday by 10am on Blackboard
• Lab (6 pts)
• In Engineering building during scheduled time!
• Project 3
• Due on Thursday, October 31st at 8:59pm on GL
• Exam 2 Review
• On Friday, November 1st from 2-4pm in ENGR 027
• Exam 2
• In person during lecture on Wednesday, November 6th and Thursday, November 7th
29
Next Time: Templates