The Craft Of Coding

CPP 2-May Templates

Posted by Pete Sun, 01 May 2016 23:43:00 GMT

We have already been using template classes in C++, for example when we created a vector to contain strings. Sample code for Calculator (zipped)

    vector<string> strings ;

Basically what templates do is allow you to write code once and have it support multiple different datatypes or classes with complete compiler support for type checking. The simplest way to use templates is to create template functions, but templated classes have a lot more power and capability (but are also a bit more complex to write and understand).

Function Templates

Rather than write a function that works for just one datatype or class, you can make it generic using a template.

    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }

By convention, in templates the letter T is used to denote the first datatype/class in the template, if you need more, then provide a more descriptive name.

    template <class T>
    T add(T a, T b) {
        return a + b;
    }

Now without adding any more code, the following is now legal:

    string one("One");
    string two("two");
    string result = add(one, two);

But of course the result will not be quite what was expected OneTwo. So if necessary it is possible to provide an implementation for any specific type that needs slightly different behavior.

    string add(string a, string b) {
        return a + string(" ") + b;
    }

Which will then produce the result of One Two.

Note. The complete template implementation must be available in the header file.

Class templates

The vector class is part of the Standard Template Library that is shipped with C++. It allows any datatype or class to be used in the vector container.

To declare a class template, the class declaration must be before the class keyword, and then in the context of the class the template class can be used just like any other datatype or class…

    template <class T>
    class List {
        T value;
        void add(T val);

And then when implementing the method, the declaration syntax is a bit more complex…

    template <class T>
    void List<T>::add(T val) {

But after that the implementation of the method is similar.

To Do - Create a Singly Linked List

Initially create the linked list to just hold int values, and once it is working, convert it to a templated class. This is a key part of creating templated classes, because they are much easier to understand when you first implement them to support a single type. Converting to a templated class is relatively mechanical.

Each element in the list holds an int value and a pointer to the next element in the list (which may be NULL). The List itself has a head that points to the first element in the list or NULL.

List needs to support three methods

    void add(int val); // add item at the end of the list
    void remove(int val); // removes the first item in the list that has same value
    void print(); // 

Note. Destructor for the linked list needs to delete all of the nodes that it contains. This will give you good practice at dealing with pointers.

    LinkedList* pList= new LinkedList();
    pList->add(1);
    pList->add(2);
    pList->add(3);
    pList->add(4);
    pList->remove(2);
    pList->print(); // should print (1,3,4)
    delete pList;

© Pete McBreen 2016 We all have our funny little ways. Except me, obviously. — Terry Pratchett (Wintersmith)