Class templates allow you to define a class that works with any data type. This is particularly useful for creating data structures like linked lists, stacks, and queues that need to handle different types of data.
Here’s a basic example of a class template:
template <typename T>
class Box {
T value;
public:
Box(T v) : value(v) {}
T getValue() { return value; }
};
int main() {
Box<int> intBox(123);
Box<string> strBox("Hello, Templates!");
cout << intBox.getValue(); // Output: 123
cout << strBox.getValue(); // Output: Hello, Templates!
}
In this example:
template <typename T> declares the template.class Box is the template class that holds a value of type T.Member functions of a class template can be defined inside or outside the class definition. When defined outside, the template parameters must be specified again.
Here’s how to define member functions both inside and outside the class definition:
template <typename T>
class Box {
T value;
public:
Box(T v);
T getValue();
};
// Outside the class definition
template <typename T>
Box<T>::Box(T v) : value(v) {}
template <typename T>
T Box<T>::getValue() {
return value;
}
In this example:
Box(T v) and the member function getValue are defined outside the class.Just like function templates, class templates can be specialized for specific types. This is useful when certain types need special handling.
template <typename T>
class Box {
T value;
public:
Box(T v) : value(v) {}
T getValue() { return value; }
};
// Specialization for char*
template <>
class Box<char*> {
char* value;
public:
Box(char* v) : value(v) {}
const char* getValue() { return value; }
};
int main() {
Box<int> intBox(123);
Box<char*> strBox("Hello, Specialization!");
cout << intBox.getValue(); // Output: 123
cout << strBox.getValue(); // Output: Hello, Specialization!
}
In this example:
Box template handles any type.Box template handles char* specifically, managing C-style strings.This is a simple example of a class template in C++. The class template Calculation takes a type parameter T that specifies the data type of the class variables and member functions.
The template keyword is used to declare the class as a template class. The class T or typename T is the placeholder for the type parameter T. The class template is defined with a private member variables n1 and n2 of type T, and public member functions add() and sub() that perform addition and subtraction on the class variables, respectively.
In the main function, two instances of the Calculation class are created, one with the type parameter int and the other with the type parameter double. The constructor of the Calculation class is called to initialize the private member variables with the input arguments.
The add() and sub() member functions of each instance are called and the results are printed to the console. The template class Calculation can work with different data types, allowing for code reusability and flexibility.
When an instance of the Calculation class is created with a specific type parameter, the class is instantiated with that specific data type. In this case, the intNumbers instance is instantiated with int data type, and the doubleNumbers instance is instantiated with double data type. The member functions add() and sub() of each instance work with the instantiated data type.
#include <iostream>;
using namespace std;
template <class T>;
class Calculation {
private:
T n1, n2;
public:
Calculation(T a, T b) {
n1 = a;
n2 = b;
}
T add() {
return n1 + n2;
}
T sub() {
return n1 - n2;
}
};
int main() {
Calculation<int>; intNumbers(10, 6);
Calculation<double>; doubleNumbers(5.6, 4.34);
cout << "integer add: " << intNumbers.add() << endl;
cout << "double add: " << doubleNumbers.add() << endl;
cout << "integer sub: " << intNumbers.sub() << endl;
cout << "double sub: " << doubleNumbers.sub() << endl;
return 0;
}
integer add: 16
double add: 9.94
integer sub: 4
double sub: 1.26