Using Template Classes and Functions in C++

C++ provides a powerful feature called templates that allow us to write generic code. Template classes and functions can be defined in C++ to work with different data types without rewriting the entire code. This feature significantly improves code reusability and reduces redundancy.

Template Classes

A template class is a blueprint for creating classes where some or all of the data types used in the class are undefined. It acts as a placeholder for any type specified by the user. To define a template class, we use the template keyword followed by the template parameter list enclosed in angle brackets (<>).

Here's an example of a simple template class that represents a generic stack:

template <typename T>
class Stack {
  private:
    static const int MAX_SIZE = 100;
    T elements[MAX_SIZE];
    int top;

  public:
    Stack();
    void push(T item);
    T pop();
};

template <typename T>
Stack<T>::Stack() {
    top = -1;
}

template <typename T>
void Stack<T>::push(T item) {
    if (top == MAX_SIZE - 1) {
        std::cout << "Stack overflow!" << std::endl;
        return;
    }
    elements[++top] = item;
}

template <typename T>
T Stack<T>::pop() {
    if (top < 0) {
        std::cout << "Stack underflow!" << std::endl;
        return T();
    }
    return elements[top--];
}

In the example above, T is a template parameter representing any data type that the user specifies. This template class Stack provides methods to push an item onto the stack (push) and pop an item from the stack and return it (pop).

By using the template class, we can create stack objects that can hold different types of elements. For example, Stack<int> will create an integer stack, while Stack<char> will create a character stack.

Template Functions

Similar to template classes, template functions allow us to define functions that can work with different data types. Template functions are defined using the same template keyword followed by the template parameter list.

Here's an example of a template function to find the maximum of two values:

template <typename T>
T max(T x, T y) {
    return (x > y) ? x : y;
}

The above function max can be used with any data type that supports comparison operators (> in this case). For instance, int, double, float, or even custom data types that implement comparison operators.

We can use the template function as follows:

int main() {
    int a = 5, b = 10;
    std::cout << "Maximum of " << a << " and " << b << " is: " << max(a, b) << std::endl;

    double c = 3.14, d = 2.71;
    std::cout << "Maximum of " << c << " and " << d << " is: " << max(c, d) << std::endl;

    return 0;
}

The output will be:

Maximum of 5 and 10 is: 10
Maximum of 3.14 and 2.71 is: 3.14

In this example, the template function max is invoked with both int and double data types, and it returns the maximum value among them.

Conclusion

Template classes and functions are powerful tools in C++ that enable us to write generic code. They provide a way to create reusable, flexible, and type-independent code. By leveraging templates, we can save time and effort by avoiding redundant code for similar operations on different data types.


noob to master © copyleft