Class Templates
We have only dealt with function templates so far. But templates can also be used to provide blueprints for classes. The general structure of a templated class declaration is as follows:
template<class T>
class MyClass {
// variables and methods that use T.
};
Whereas template functions allow us to produce generic algorithms, template classes allow us to produce generic data types and their associated behaviors.
When we introduced the Standard Template Library, we highlighted that it includes templates for containers – vector, deque, stack, and so on. These templates allow us to store and manage any data type that we want, but still behave as we would expect.
Exercise 4: Writing a Class Template
Two of the most commonly used data structures in computing science are stack and queue. Both currently have implementations in the STL. But to get our feet wet with a templated class, we are going to write a stack template class that can be used for any type...