November 2002
Intermediate to advanced
552 pages
10h 52m
English
Similar to functions, classes can also be parameterized with one or more types. Container classes, which are used to manage elements of a certain type, are a typical example of this feature. By using class templates, you can implement such container classes while the element type is still open. In this chapter we use a stack as an example of a class template.
StackAs we did with function templates, we declare and define class Stack<> in a header file as follows (we discuss the separation of declaration and definition in different files in Section 7.3 on page 89):
// basics/stack1.hpp #include <vector> #include <stdexcept> template <typename T> class Stack { private: std::vector<T> ...
Read now
Unlock full access