Class Templates

A function template is one function that works for many types. A class template is the same idea for a class. You have used one since the Vectors lesson: vector<int> and vector<string> are the same class, vector, with a different type in the angle brackets.

Here is a small class template of your own. A Box holds one item of any type:

#include <iostream>
#include <string>
using namespace std;

template <typename T>
class Box {
private:
    T item;

public:
    Box(T i) {
        item = i;
    }

    T get() {
        return item;
    }
};

int main() {
    Box<int> number(42);
    Box<string> word("hat");
    cout << number.get() << "\n";
    cout << word.get() << "\n";
    return 0;
}

Output:

42
hat

The template line is the same as for a function, but now it goes above the class. Inside the class, T is used for the member, the constructor's parameter and the return type of get.

When you create an object, write the type in angle brackets, like with a vector. Box<int> is a box where every T is int. Box<string> is a separate box type where every T is string.

A stack for any type

A stack is a list where you add and remove items only at one end, the top. Think of a pile of plates: the last plate you put on is the first one you take off. Stacks are used a lot, for example to remember the pages you visited, so that "back" goes to the last one.

This class template keeps the items in a vector<T>. The top of the stack is the end of the vector. back() gives the last item of a vector, and pop_back() removes it:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

template <typename T>
class Stack {
private:
    vector<T> items;

public:
    void push(T item) {
        items.push_back(item);
    }

    T top() {
        return items.back();
    }

    void pop() {
        items.pop_back();
    }

    bool isEmpty() {
        return items.size() == 0;
    }
};

int main() {
    Stack<string> pages;
    pages.push("home");
    pages.push("news");
    pages.push("sports");

    while (!pages.isEmpty()) {
        cout << pages.top() << "\n";
        pages.pop();
    }
    return 0;
}

Output:

sports
news
home

The pages come out in the opposite order: sports was pushed last, so it comes out first. The same Stack works for numbers too: Stack<int> would keep its items in a vector<int>.

The standard library already has a ready-made stack, and a later lesson, Stacks and Queues, shows it. Writing a small one yourself shows how such classes are built.

Forgetting the type

If you leave out the angle brackets, the compiler has no way to know what T is:

#include <iostream>
#include <vector>
using namespace std;

template <typename T>
class Stack {
private:
    vector<T> items;

public:
    void push(T item) {
        items.push_back(item);
    }
};

int main() {
    Stack s;
    s.push(5);
    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:17:11: error: class template argument deduction failed:

The fix is Stack<int> s;.

More than one type

A template can also take more than one type. You will see this in the next section. A map<string, int> has two: the type of its keys and the type of its values.

In the exercise, finish a class template Box that holds a list of items, and then create a Box of ints and a Box of strings.