Chapter 1, Unit - IV (Templates)
Chapter 1, Unit - IV (Templates)
A template is a simple yet very powerful tool in C++. The simple idea is to
pass the data type as a parameter so that we don’t need to write the same
code for different data types. For example, a software company may need
to sort() for different data types. Rather than writing and maintaining
multiple codes, we can write one sort() and pass the datatype as a
parameter.
C++ adds two new keywords to support templates: ‘template’ and
‘typename’. The second keyword can always be replaced by the keyword
‘class’.
Function Templates
We write a generic function that can be used for different data types.
Examples of function templates are sort(), max(), min(), printArray().
// C++ Program to demonstrate
// Use of template
#include <iostream>
using namespace std;
// One function works for all data types. This would work
// even for user defined types if operator '>' is overloaded
template <typename T> T myMax(T x, T y)
{
return (x > y) ? x : y;
}
int main()
{
// Call myMax for int
cout << myMax<int>(3, 7) << endl;
// call myMax for double
cout << myMax<double>(3.0, 7.0) << endl;
// call myMax for char
cout << myMax<char>('g', 'e') << endl;
return 0;
}
Class Templates
Class templates like function templates, class templates are useful when
a class defines something that is independent of the data type. Can be
useful for classes like LinkedList, BinaryTree, Stack, Queue, Array, etc.
// C++ Program to implement
// template Array class
#include <iostream>
using namespace std;
public:
Array(T arr[], int s);
void print();
};
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
Array<int> a(arr, 5);
a.print();
return 0;
}
Yes, like normal parameters, we can pass more than one data type as
arguments to templates. The following example demonstrates the same.
We write a generic function that can be used for different data types.
Examples of function templates are sort(), max(), min(), printArray()
#include <iostream>
using namespace std;
T myMax(T x, T y)
{
return (x > y) ? x : y;
}
int main()
{
return 0;
}
Like function templates, class templates are useful when a class defines
something that is independent of data type. Can be useful for classes like
LinkedList, binary tree, Stack, Queue, Array, etc.
Following is a simple example of template Array class.
#include <iostream>
class Array {
private:
T* ptr;
int size;
public:
Array(T arr[], int s);
void print();
};
size = s;
void Array<T>::print()
}
int main()
int arr[5] = { 1, 2, 3, 4, 5 };
a.print();
return 0;
// Driver Code
int main()
{
// Function Call for side as
// 9 i.e., integer
square(9);
parameters.
time.
function template.
T functionName(T arguments)
{
// Function definition
………. …… ….. …….
}
where, T is template argument accepting different arguments and class is
a keyword.
● The name of the function templates are the same but called with
differs.
function, the function name remains the same but the function’s
// Template declaration
template <class T>
// Template overloading of function
void display(T t1)
{
cout << "Displaying Template: "
<< t1 << "\n";
}
// Driver Code
int main()
{
// Function Call with a
// different arguments
display(200);
display(12.40);
display('G');
return 0;
}