Home C++ Introduction Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess Recursion STL Modern C++ Misc Books ----

Templates


Contents

Introduction

Templates allow us to specify a class parameter to a function or a class. We are used to passing parameters that are primitive types such int, float or class types such as string. With templates the type itself is a parameter and we can pass different types. This allows us a great deal of flexibility in writing our functions and classes.

Function Templates



File: template1.cpp
#include <iostream>

using namespace std;


template <class T> T GetMax (T a1, T b1)
{
	T result;
	result = (a1>b1)? a1 : b1 ;
	return (result);
}

int main ()
{
	int i1=5, j1=6, k1;
	long l1=10, m1=5, n1;

	k1 = GetMax<int>(i1, j1 );
	n1 = GetMax<long>(l1, m1 );
	cout << k1 << endl;
	cout << n1 << endl;
	return 0;
}
Output:

ajay.mittal@USEUG-57MJ2J3 ~/cplus
$ ./a.exe
6
10
template T GetMax (T a1, T b1)

The above is a declaration for the function. We are stating that we can pass "T" which is a type parameter to the function "GetMax" . It is used as:

k1 = GetMax(i1, j1 );
n1 = GetMax(l1, m1 );

We use the syntax "" to specify the type that we are passing a type. We do not have to create 2 separate functions for the different types "int" and "long" . In the above code we are explicitly stating the type for the function.
k1 = GetMax(i1, j1 );
We do not need to give the type "" if the compiler can figure out the type.

File: template2.cpp
#include <iostream>

using namespace std;


template <class T> T GetMax (T a1, T b1)
{
	T result;
	result = (a1>b1)? a1 : b1 ;
	return (result);
}

int main ()
{
	int i1=5, j1=6, k1;
	long l1=10, m1=5, n1;

	k1 = GetMax(i1, j1 );
	n1 = GetMax(l1, m1 );
	cout << k1 << endl;
	cout << n1 << endl;
	return 0;
}

File: template3.cpp
#include <iostream>
#include <string.h>

using namespace std;


template <class T, class Y> void console (T a1, Y b1)
{
  cout << a1 << " " << b1 << endl ;
}

template <class T, class Y, int i1> void console (T a1, Y b1)
{
  cout << a1 << " " << b1 << " " << i1 << endl ;
}


int main ()
{
	int i1=5  ;
	string str1 = "demo" ;
	console( 5, str1 ) ;
	console<int, string, 10>( 5, str1 ) ;
	return 0;
}
ajay.mittal@USEUG-57MJ2J3 ~/cplus
$ g++ template3.cpp  ; ./a.exe
5 demo
5 demo 10
The above program shows that we can pass more than 1 class parameter into the function. We can also choose to define the type in the template parameters and then give a constant value for it that we can access later on. We can overload the template functions with normal functions.
File: template4.cpp
#include <iostream>

using namespace std;


template <class T> T GetMax (T a1, T b1)
{
	T result;
	result = (a1>b1)? a1 : b1 ;
	cout << "Inside the template function." << endl ;
	return (result);
}

int GetMax (int a1, int b1)
{
	int result;
	result = (a1>b1)? a1 : b1 ;
	cout << "Inside the normal function." << endl ;
	return (result);
}


int main ()
{
	int i1=5, j1=6, k1;
	long l1=10, m1=5, n1;

	k1 = GetMax(i1, j1 );
	n1 = GetMax(l1, m1 );
	cout << k1 << endl;
	cout << n1 << endl;
	return 0;
}

File: template5.cpp
#include <iostream>

using namespace std;


template <class T , class Y=int> T GetMax (T a1, Y b1)
{
	T result;
	result = (a1>b1)? a1 : b1 ;
	return (result);
}



int main ()
{
	int i1=5, j1=6, k1;
	long l1=10, m1=5, n1;

	k1 = GetMax<int>(i1, j1 );
	n1 = GetMax<long,long>(l1, m1 );
	cout << k1 << endl;
	cout << n1 << endl;
	return 0;
}
Normal functions can have default argument values and similarly template functions parameters can have default values as the above example shows.

Class Templates

We can also pass a class parameter to a class.

File: stack1.cpp
#include <iostream>

using namespace std ;

template <class T>
class Stack
{
private:
	int size ;  // number of elements on Stack.
	int top ;
	T* stackPtr ;

public:
	Stack( int initSize = 10 )
           {
              stackPtr = new T[ initSize ]  ;
              top = -1 ;
           }
	~Stack()
           {
               delete[] stackPtr ;
           }
	int push(const T& value)
           {
               if ( isFull() )
                 {
                   cout << "Stack is full." << endl ;
                    exit(1) ;
                 }
               stackPtr[ ++top ] = value ;
           }
	T pop()
           {
               if ( isEmpty() )
                 {
                    cout << "Stack is Empty." << endl ;
                    exit(1) ;
                  }
                T result ;
                result =  stackPtr[ top ]  ;
                top-- ;
                return result ;
           }
	int isEmpty()const { return top == -1 ; }
	int isFull() const { return top == size - 1 ; }
} ;


int main()
{
   Stack<int> s1 ;
   s1.push( 4 ) ;
   cout << s1.pop() << endl  ;

   Stack<string> s2    ;
   s2.push( "Alan" )    ;
   cout << s2.pop() <<   endl  ;



File: container.cpp
}

Class Template Inheritance

We can have a derived class pass it's template parameter to the base class.
File: der1.cpp