Templates in C++ are the powerful feature that allows us to write generic programs (generic classes and generic functions).
A single class or function is created to work with different data types using templates. We pass the data types in a function as a parameter so that repetition of the same code can be avoided for different data types.
The passed parameter is called the template parameter. It is the same as the regular parameter but here instead of values we pass different data types.
The templates can be represented in two different ways.
- Function Templates
- Class Templates
Function Templates
We can create the function template in order to use it for different data types such as float, int, double, etc. For example, suppose we create a function called mul()
for the multiplication, in normal function this function is used for specific data-types values but with templates we can pass any data types into the function.
It makes a better approach because we can write less code and save time from the repetition of writing code that is used to do the same work.
Syntax of Function Template:
1 2 3 4 | template <class T> T func_name(T parameter_list) { // body of function } |
T
is a template argument or placeholder name for a data type (int, float, etc). The compiler automatically replaces it with the actual data type.class
is a keyword used for generic type and it can replace withtypename
.
Example of function templates in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; template<class T> T mul(T &num1,T &num2) { T product = num1 + num2; return product; } int main() { int a = 4, b = 2; float x = 3.3, y = 2.3; cout<<"Multiplication of a and b: "<< mul(a,b) << endl; cout<<"Multiplication of x and y: "<< mul(x,y); return 0; } |
Output:
1 2 | Multiplication of a and b: 6 Multiplication of x and y: 5.6 |
Class Templates
Just like a function template, we can create a template for the class for generic operations that is independent of any data type.
It is required when we need the same implementation for many classes but for different data types and also avoided repetition of the same code.
Syntax of class Template:
1 2 3 4 5 6 | template<class T> class class_name { .... .... ...... } |
Creating class template object.
The data type is mentioned inside <>
while creating an object for the class template. For example:
1 2 3 4 5 6 | //Syntax class_name<dataType> classObject; //Example class_name<int> classObject; class_name<float> classObject; |
let us go through an example of a class template in C++ to understand more.
Example of class templates in C++
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <iostream> using namespace std; template <class T> class Operation { public: T num1 = 10.5, num2 = 5.2; void print() { cout << "Addition: " << add() << endl; cout << "Subtraction: " << sub() << endl; cout << "Product: " << mul() << endl; cout << "Division: " << divide() << endl; } T add() { return num1 + num2; } T sub() { return num1 - num2; } T mul() { return num1 * num2; } T divide() { return num1 / num2; } }; int main() { Operation<int> intOb; Operation<float> floatOb; cout << "INTEGER:" << endl; intOb.print(); cout << endl << "FLOAT:" << endl; floatOb.print(); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 | INTEGER: Addition: 15 Subtraction: 5 Product: 50 Division: 2 FLOAT: Addition: 15.7 Subtraction: 5.3 Product: 54.6 Division: 2.01923 |
As you can see that the result is shown according to the data type passed while creating class objects (intOb, floatOb
).