C/C++ Arena

Step 3 of 8

Class templates

Classes can be templates too. That's how std::vector<T> works:

template <typename T>
class Box {
public:
    explicit Box(T v) : value_(v) {}
    const T& get() const { return value_; }
private:
    T value_;
};

Box<int> b(5);

Your turn: write a class template Stack<T> with push(const T&), pop() (returns the top value and removes it; assume non-empty), bool empty() const and int size() const. Store items in a std::vector<T>.

Previous: Templates over containers Next: constexpr