CMSC 202 Computer Science II, Spring 2016, Section 01
Lecture 18: Polymorphism
Tue 04/05, 2016
Slides As Shown:
- Reviewed for Midterm Exam II.
- Dynamic binding and virtual member functions.
- Used virtual member functions to design an abstract
Array base class with a sorting function. Any class derived
from array can be sorted using sort() without recompiling
the base class functions.
- A hetergeneous linked list can hold different types of
objects in the list at the same time.
The implementation of HList brings up many issues
that come up when you work with virtual functions,
- the need for a virtual destructor
- clone(): a virtual "copy constructor"
- run-time type information
- dynamic type casting
Code for HList and the HNode base class:
IntNode class derived from HNode:
Test program #1 and output:
DoubleNode class derived from HNode:
StringNode class derived from HNode:
Test program #2 and output:
Test program #3 and output:
HStack is a private derivation of HList.
FIFO queue implemented. Check out source code for HQueue.
5 Apr 2016 19:57:21 EDT