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.
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
Basic knowledge of C++ programming is required, including a basic understanding of functions and classes.
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.
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.
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.
#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
#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
For further reading, consider the following resources:
- C++ Templates - W3Schools
- Templates in C++ - GeeksforGeeks
Create a function template that takes two arguments and returns the larger one. Test this function with different data types.
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.
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.
template <typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}
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--];
}
}
};
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!