C++ language is a very powerful programming language and is suitable for almost any software development task. One of the great features of modern C++ is templates. A template is a simple and a very powerful statement in C++ which defines the operations of a class or function and lets the user apply the same template on different types in those operations. In this post we will explain class templates in C++ which can be used by a professional C++ Compiler and IDE with C++ examples from this article .
Table of Contents
What is a template in C++?
A template is a very powerful statement in C++ which simply defines the operations of a class, a function, an alias or a variable and lets the user apply the same template on different data types in those template operations. Templates are similar to macros in C++ except the compiler checks the types used before the template is expanded.
How does a template in C++ work?
Here is a function template that adds two a
and b
parameters in T type
,
1 2 3 4 5 6 7 |
template <class T> T add (T a, T b) { return a+b; } |
for example, if a and b are int
variables, this template can be used as below
1 2 3 |
int i = add <int> (a, b); |
and in the next lines same template can be used for the x and y float variables as below too,
1 2 3 |
float f = add <float> (x, y); |
Templates can be parameterized by one or more template parameters. These parameters can be type template parameters, non-type template parameters, and template template parameters – templates as parameters are allowed, in templates!
What is a class template in C++?
A class template (also called a generic class or class generator) lets you define a pattern for class definitions. Consider the following example of a vector class, a one-dimensional array. Whether you have a vector of integers or any other type, the basic operations performed on the type are the same – insert, delete, index, and so on. With the element type treated as a type parameter to the class, the system will generate type-safe class definitions on the fly.
Here is the syntax for a class template,
1 2 3 4 5 6 |
template <class_type> return_type function_name( <parameter_list>) { // body of function } |
As with function templates, an explicit template class specialization can be provided to override the automatic definition for a given type:
1 2 3 |
class Vector<char *> { ... }; |
The symbol Vector must always be accompanied by a data type in angle brackets. It cannot appear alone, except in some cases in the original template definition.
How to declare a class template in C++?
When you create a class template you should start with template
keyword then the parameters inside < >
, for example
1 2 3 4 5 6 7 8 9 10 |
template <class T> class my_class { private: T val; public: T my_function(T parameter); }; |
How to create objects from a class template in C++?
If you declared a class template, you could create objects from this template. You just need to type the class name with a data type and then the object name.
Here is the syntax to create a class template:
1 2 3 |
class_name<data_type> object_name; |
here is an example to create objects:
1 2 3 4 5 6 |
my_class<char> my_object1; my_class<int> my_object2; my_class<float> my_object3; my_class<std::string> my_object4; |
Is there a full example of a class template in C++?
Here is a full class template example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> // Class Template Example template <class T> class Weight { private: // A variable for a value of type T T val; public: Weight(T n) : val(n) { } // constructor T value() // method to obtain value { return val; } }; int main() { Weight<double> car_weight(1650); // create weight object for a car Weight<int> student_weight(82.5); // create weight object for a student std::cout << "Weight of the Car " << car_weight.value() << " kg" << std::endl; std::cout << "Weight of the Student " << student_weight.value() << " kg" << std::endl; system("pause"); return 0; } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here