CPP Templates
C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using generic parameters.
There are commonly two ways to represent templates:
- Function Templates
- Class Templates
Let’s discuss these two representations in detail.
Functional Templates
Functional templates usually define a template for a function. If we take into account a function multiply(), we can use it with any data type like int, char, float, or double type values. Consider the below characteristics of functional templates.
- Functional templates are extensively used in generic functions like add() or delete(). These generic functions define some sort of operations that can be easily applied to different data types.
- The operation of the function depends on the type of data passed as a parameter. Consider quick sort algorithm, which uses general function methodology with arrays and strings.
- The template keyword helps in the creation of a generic function. The template defines what function will serve.
Syntax:
template < class Ttype> return_type_function_name(parameter_list) { // body of function. }
Here,
Ttype: It is a placeholder name that is later replaced by the compiler by the actual data type.
class: It is a keyword used to generic template declaration.
Example Code:
#include<bits/stdc++.h> using namespace std; template<class A> A multiply(A &x,A &y) { A result = x*y; return result; } int main() { int i =4; int j =5; float m = 3.3; float n = 4.4; cout<<"Mulplication of x and y is :"<<multiply(i,j); cout<<'\n'; cout<<"Mulplication of x and y is :"<<multiply(m,n); return 0; }
Output:
Explanation:
In the above code, we have a placeholder value A associated with function multiplication which takes two parameters, x, and y. Another variable, 'result' is assigned with placeholder A, which stores the multiplicative value of x and y. We must set some random integer and float values to variables I, j, m, and n, respectively. The output of the code is the multiplicative value of these variables.
Similarly, functional templates can also be used with multiple parameters by separating with commas in the list.
Also, they can be easily overloaded, which is restricted to some extent depending on the situation.
Class Templates
A class template in C++ is used for specification for generating classes based on parameters. Generally, they are used to implement containers. A given set of types is passed as template arguments in a class template.
Syntax:
template<class T1, class T2, ......> class class_name { // Body of the class. }
Let us discuss class templates with the help of a coding example.
#include<bits/stdc++.h> using namespace std; template <class X> class Calculator_Sample { private: X number1, number2; public: Calculator_Sample(X a1, X a2) { number1 = a1; number2 = a2; } void Result() { cout << "Your entered : " << number1 << " and " << number2 << "." << endl; cout << "Addition Result: " << add() << endl; cout << "Subtraction Result: " << subtract() << endl; cout << "Product Result: " << multiply() << endl; cout << "Division Result: " << divide() << endl; } X add() { return number1 + number2; } X subtract() { return number1 - number2; } X multiply() { return number1 * number2; } X divide() { return number1 / number2; } }; int main() { Calculator_Sample<int> intCalc(3, 4); Calculator_Sample<float> floatCalc(12.4, 11.2); cout << "Results in integers :" << endl; intCalc.Result(); cout << endl << "Results in float :" << endl; floatCalc.Result(); return 0; }
Output:
Explanation:
In the above program, a class template Calculator_sample is declared. The class template X contains two private members of X, namely number1 and number2, and a constructor to initialize the members.
To calculate addition, subtraction, multiplication, and division, it also contains public members who return the data type defined by the user. Likewise, to display the final output, we declared a function Result().
In the driver code, we created two objects of the class namely floatCalc and intCalc which are based on integer and float data types, and the values are initialized with the help of the constructor.
We used <int> and <float> to tell the compiler the data type we are using for the creation of the class. By doing this, it creates a class definition that can later be used accordingly.
The Result() function performs the operations of the class Calculator_sample and displays the values on the console as defined in the code.
Key Points:
Let’s consider some key points, which are listed below:
- C++ holds its back by supporting a powerful feature used for generic programming implementation popularly known as template.
- With the help of a template, we can easily create a family of classes or functions that can handle different data types.
- Faster and easier development is achieved by using templates because we can easily handle class and function redundancy.
- Class template and function template can be used for multiple parameters.
- Overloading can be easily done in a function template.
- We can use built-in derived data type or non-type arguments with the help of templates in C++.