How to Create a Template Class in C++?
Last Updated :
07 Feb, 2024
In C++, template classes are used to create generic classes that can work for different data types. In this article, we will learn how to create a template class in C++.
Create a Template Class in C++
To create a template class in C++, we can follow the below syntax:
Syntax of Template Class
template <class T>
class MyTemplateClass {
// function declaration
T member_function (T args) {
// body
}
// data member
T var_name;
};
So here "MyTemplateClass" is a template class with a single template parameter T. This template parameter can be any data type (e.g., int, double, string). Each occurrence of the T will be replaced by the type provided as the template parameter.
We can provide more template parameters if needed.
C++ Program to Create a Template Class in C++
Let's see the example of a template class how it can be created in a program and how it works.
C++
// C++ Program to demonstrate template classes
#include <iostream>
#include <string>
using namespace std;
// Declaration of a template class
template <typename A> class MyTemplateClass {
private:
A data;
public:
// Constructor
MyTemplateClass(A initialData)
: data(initialData)
{
}
// Member function using the template type
void setData(A newData) { data = newData; }
// Member function using the template type
A getData() const { return data; }
};
int main()
{
// Creating instances of the template class with
// different types
MyTemplateClass<int> intInstance(42);
MyTemplateClass<double> doubleInstance(3.14);
MyTemplateClass<string> stringInstance("Hello, World!");
// Using the template class methods
cout << "Integer data: " << intInstance.getData()
<< endl;
cout << "Double data: " << doubleInstance.getData()
<< endl;
cout << "String data: " << stringInstance.getData()
<< endl;
return 0;
}
OutputInteger data: 42
Double data: 3.14
String data: Hello, World!
In C++, the compiler is used to compile the type-specific versions of the template class for each data type used in the programs. This allows the programmers to write generic code while benefiting from type safety.
To know more, refer to the article - Templates in C++
Similar Reads
How to Create a Function Template in C++? In C++, templates enable us to write generic programs that handle any data type. We can create a template class, function, and variable. A template function is a function that can work with any data type. In this article, we will learn how to create a function template in C++. Create a Function Temp
2 min read
How to Create a Thread in C++? A thread is a basic element of multithreading which represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to create a thread in C++. How to Create a Thread in C++?In C++, the std::thread is a class template that is used t
2 min read
How to Create a shared_ptr in C++? A std::shared_pointer is a smart pointer introduced in C++11 that manages the lifetime of a dynamically allocated object through reference counting. In this article, we will learn how to create a shared_pointer. shared_ptr in C++A std::shared_pointer can be created by using the std::make_shared meth
2 min read
How to Create a Class with Private and Public Members in C++? In C++, the classes are blueprints for creating objects with specific properties and methods that provide a feature of access specifiers to the user through which they can control the access of the data members present in a class. In this article, we will learn how to create a class with private and
3 min read
How to Create a Static Library in C++? C++ allows users to add their own libraries to reduce the amount of code we have to write in our program. A static library in C++ is a library that is linked to the program at compile-time. In this article, we will learn how to create a static library in C++ from scratch. Create Static Library in C+
3 min read