Understanding C++ Templates

Tutorial 1 of 5

Understanding C++ Templates

1. Introduction

1.1 Goal

This tutorial aims to breakdown the concept of templates in C++, explaining why they are useful and how to use them effectively in your programs.

1.2 Learning outcomes

By the end of this tutorial, you will understand:
- What templates are and why they are useful
- The syntax and structure of templates
- How to use function templates and class templates

1.3 Prerequisites

Basic knowledge of C++ programming is required, including a basic understanding of functions and classes.

2. Step-by-Step Guide

2.1 What is a Template?

Templates in C++ are a powerful feature that allow programmers to write more general, reusable code. They are a way to make functions and classes operate with generic types, allowing a function or class to work on many different data types without being rewritten for each one.

2.2 Function Templates

Function templates allow functions to operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

Example:

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

In this example, T is a placeholder for a data type that the function add will use. This means that add can be used with any data type that supports the + operator.

2.3 Class Templates

Like function templates, class templates allow classes to operate with generic types. This can allow a single class to easily work with multiple data types.

Example:

template <typename T>
class MyArray {
    T arr[10];
    int size;
public:
    MyArray() : size(0) {}
    void add(T element) {
        if(size < 10) {
            arr[size] = element;
            size++;
        }
    }
    T get(int index) {
        return arr[index];
    }
};

In this example, MyArray is a class template that can store an array of any type. The add and get methods allow you to add elements to the array and retrieve them, respectively.

3. Code Examples

3.1 Function Template

#include <iostream>

// Function template
template <typename T>
T add(T a, T b) {
    return a + b;
}

int main() {
    std::cout << "Sum of integers: " << add(1, 2) << "\n";
    std::cout << "Sum of doubles: " << add(1.5, 2.5) << "\n";

    return 0;
}

In the above code, we first define a function template add that takes two parameters of any type T and returns their sum. In the main function, we use add with two different types: int and double. The output will be:

Sum of integers: 3
Sum of doubles: 4

3.2 Class Template

#include <iostream>

// Class template
template <typename T>
class MyArray {
    T arr[10];
    int size;
public:
    MyArray() : size(0) {}
    void add(T element) {
        if(size < 10) {
            arr[size] = element;
            size++;
        }
    }
    T get(int index) {
        return arr[index];
    }
};

int main() {
    MyArray<int> intArr;
    intArr.add(1);
    intArr.add(2);
    std::cout << "First element in integer array: " << intArr.get(0) << "\n";

    MyArray<double> doubleArr;
    doubleArr.add(1.5);
    doubleArr.add(2.5);
    std::cout << "First element in double array: " << doubleArr.get(0) << "\n";

    return 0;
}

In this example, we first define a class template MyArray that can store an array of any type T. In the main function, we create two instances of MyArray: one that stores int and another that stores double.

The output will be:

First element in integer array: 1
First element in double array: 1.5

4. Summary

  • Templates in C++ allow for more abstract and reusable code.
  • Function templates can be used to create functions that work with any data type.
  • Class templates can be used to create classes that work with any data type.
  • Templates are a powerful tool in C++, but they can make code difficult to read if used excessively.

For further reading, consider the following resources:
- C++ Templates - W3Schools
- Templates in C++ - GeeksforGeeks

5. Practice Exercises

  1. Create a function template that takes two arguments and returns the larger one. Test this function with different data types.

  2. Create a class template for a Stack data structure that works with any data type. The Stack should have methods for pushing (adding) and popping (removing) elements.

  3. Modify the MyArray class template from the class template example above to include a method that calculates and returns the average of all elements in the array.

Solutions

  1. Function Template:
template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}
  1. Class Template:
template <typename T>
class Stack {
    T arr[10];
    int top;
public:
    Stack() : top(-1) {}
    void push(T element) {
        if(top < 9) {
            arr[++top] = element;
        }
    }
    T pop() {
        if(top >= 0) {
            return arr[top--];
        }
    }
};
  1. Modified Class Template:
template <typename T>
class MyArray {
    T arr[10];
    int size;
public:
    MyArray() : size(0) {}
    void add(T element) {
        if(size < 10) {
            arr[size] = element;
            size++;
        }
    }
    T get(int index) {
        return arr[index];
    }
    T average() {
        T sum = 0;
        for(int i = 0; i < size; i++) {
            sum += arr[i];
        }
        return sum / size;
    }
};

Remember, practice is key in becoming proficient with C++ templates!