What Are Templates?
In Hour 19, you learned how to make a linked list. Your linked list was nicely encapsulated: The list knew only about its head pointer; the head pointer delegated its work to internal pointers, and so forth.
The one glaring problem with the linked list was that it only knew how to handle the particular data objects it was created to work with. If you wanted to put anything else into your linked list, you couldn't do it. You couldn't for example, make a linked list of Car objects, or of Cats, or of any other object that wasn't of the same type as those in the original list.
To solve this problem, you can create a List base class and derive from it the CarList and CatsList classes. You can then cut and paste much of the LinkedList ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access