C++ Class Templates Tutorial
Introduction to Class Templates
Class templates are a powerful feature in C++ that allow you to create a class that can handle any data type. This is particularly useful for creating data structures like arrays, linked lists, and stacks that can operate on any type of data.
A class template is a blueprint for creating classes. It allows you to define a generic class with placeholder types that can be specified when the class is instantiated.
Syntax of Class Templates
The basic syntax for declaring a class template is as follows:
template <typename T> class ClassName { // class definition };
Here, <typename T>
tells the compiler that T
is a placeholder for a data type. You can use T
within the class definition to define members that can operate on any data type.
Example of a Simple Class Template
Let's create a simple class template for a class called Box
that can store an item of any data type:
#include <iostream> using namespace std; template <typename T> class Box { private: T item; public: void setItem(T item) { this->item = item; } T getItem() { return item; } }; int main() { Box<int> intBox; intBox.setItem(123); cout << "Integer value: " << intBox.getItem() << endl; Box<string> stringBox; stringBox.setItem("Hello, Templates!"); cout << "String value: " << stringBox.getItem() << endl; return 0; }
In this example, we define a class template Box
that has a private member item
of type T
. The class has two member functions:
setItem(T item)
: Sets the value of the item.getItem()
: Returns the value of the item.
We then create instances of Box
for int
and string
types in the main
function.
Compiling and Running the Example
To compile and run the above example, you can use the following commands:
g++ -o box_example box_example.cpp ./box_example
The output of the program will be:
String value: Hello, Templates!
Advanced Example: Class Template with Multiple Types
Class templates can also accept multiple types. Here is an example of a class template Pair
that can store a pair of values of any two types:
#include <iostream> using namespace std; template <typename T1, typename T2> class Pair { private: T1 first; T2 second; public: Pair(T1 first, T2 second) : first(first), second(second) {} T1 getFirst() { return first; } T2 getSecond() { return second; } }; int main() { Pair<int, string> pair(1, "one"); cout << "First: " << pair.getFirst() << ", Second: " << pair.getSecond() << endl; Pair<double, char> anotherPair(3.14, 'A'); cout << "First: " << anotherPair.getFirst() << ", Second: " << anotherPair.getSecond() << endl; return 0; }
In this example, we define a class template Pair
that has two private members first
and second
of types T1
and T2
, respectively. The class has a constructor to initialize these members and two member functions to return their values.
Conclusion
Class templates are a versatile feature in C++ that allow you to create generic classes that can operate on any data type. This makes it easier to create reusable and type-safe code. By understanding the syntax and usage of class templates, you can enhance your C++ programming skills and create more efficient and maintainable code.