0% found this document useful (0 votes)
13 views7 pages

Templates

Templates in C++ allow developers to write generic functions and classes that can operate on different data types without duplicating code. They utilize the keywords 'template' and 'typename' (or 'class') and are expanded at compile time with type checking. The document also distinguishes between function overloading and templates, noting that the former is for similar operations while the latter is for identical operations across data types.

Uploaded by

jgr102005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Templates

Templates in C++ allow developers to write generic functions and classes that can operate on different data types without duplicating code. They utilize the keywords 'template' and 'typename' (or 'class') and are expanded at compile time with type checking. The document also distinguishes between function overloading and templates, noting that the former is for similar operations while the latter is for identical operations across data types.

Uploaded by

jgr102005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Templates

Templates in C++

 A template is a simple yet very powerful tool in C++. The simple idea
is to pass 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 data
type 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’.
 Templates are expanded at compiler time. This is like macros. The
difference is, that the compiler does type checking before template
expansion. The idea is simple, source code contains only
function/class, but compiled code may contain multiple copies of the
same function/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().
Function Template Example
#include <iostream>
using namespace std;
// One function works for all data types.
template <typename T> T myMax(T x, T y)
{
return (x > y) ? x : y;
}
int main()
{
cout << myMax<int>(3, 7) << endl; // Call myMax for int
cout << myMax<double>(3.0, 7.0)
<< endl; // call myMax for double
cout << myMax<char>('g', 'e')
<< endl; // call myMax for char
return 0;
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.
 Following is a simple example of a template Array class.
Class Template Example
#include <iostream>
using namespace std;
template <typename T> class Array { template <typename T> void
Array<T>::print()
private:
{
T* ptr;
for (int i = 0; i < size; i++)
int size;
cout << " " << *(ptr + i);
public:
cout << endl;
Array(T arr[], int s);
}
void print();
int main()
};
{
template <typename T> Array<T>::Array(T
arr[], int s) int arr[5] = { 1, 2, 3, 4, 5 };

{ Array<int> a(arr, 5);

ptr = new T[s]; a.print();

size = s; return 0;

for (int i = 0; i < size; i++) }

ptr[i] = arr[i];
}
What is the difference between
function overloading and
templates?
Both function overloading and templates are examples of polymorphism
features of OOP. Function overloading is used when multiple functions do
quite similar (not identical) operations, templates are used when
multiple functions do identical operations.

You might also like