C++ Class Templates
A class template is a blueprint for making classes that work with different data types. It lets you write one class definition, then choose the actual type when you create an object.
Class templates are useful when the behavior is the same but the stored data type changes. For example, a simple box can hold an int, a double, or a std::string without needing three separate classes.
Basic Syntax
A class template begins with template <typename T>. The name T is a type parameter. Inside the class, T is used anywhere a real type would normally appear.
For example, a template named Box can store a member variable of type T, accept a T in its constructor, and return a T from a getter function. This is a template definition, not a finished class by itself. C++ creates a real class when you use a type such as Box<int> or Box<std::string>.
Example
This program defines a Box class template that stores one value. It also defines a Pair class template with two type parameters.
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class Box {
private:
T value;
public:
Box(T start) : value(start) {}
void set(T newValue) {
value = newValue;
}
T get() const {
return value;
}
void show() const {
cout << "Box contains: " << value << endl;
}
};
template <typename First, typename Second>
class Pair {
private:
First first;
Second second;
public:
Pair(First firstValue, Second secondValue)
: first(firstValue), second(secondValue) {}
void show() const {
cout << first << ": " << second << endl;
}
};
int main() {
Box<int> score(95);
score.show();
score.set(100);
cout << "Updated score: " << score.get() << endl;
Box<string> label("Templates");
label.show();
Pair<string, int> level("Level", 3);
level.show();
return 0;
}
Output:
Box contains: 95
Updated score: 100
Box contains: Templates
Level: 3
Creating Template Objects
When you create an object from a class template, write the type in angle brackets after the class name. For example, Box<int> creates a box whose value member is an int. Box<string> creates a different version whose value member is a std::string.
These are separate types. A Box<int> object is not the same type as a Box<string> object, even though both come from the same template.
Member Functions
Member functions can use the template type too. In Box, the constructor accepts a T, set accepts a T, and get returns a T. The compiler replaces T with the chosen type for each object.
The const after get and show means those functions do not change the object. This is especially common for getter and display functions.
Multiple Type Parameters
A class template can have more than one type parameter. The Pair class uses First for one member and Second for another. That allows Pair<string, int> to store text and a number together.
When To Use Class Templates
Use a class template when the same class design should work for several types. Containers, simple wrappers, pairs of values, and reusable data structures are common examples.
A template still depends on the operations used inside it. The show functions in this lesson require the stored types to work with cout <<. If a type cannot be printed that way, this template would need a different design.
Takeaway
Class templates let you write one reusable class definition, then create strongly typed classes such as Box<int> and Box<string> when you need them.
