Template function is a type of function which only describes the procedure or statements of the function, i.e. what the function will do, but do not specify the data type of arguments. Thus a template class is a basic skeleton.

#include
template 
T swap(T &a, T &b)
{
  T temp;
  temp = a;
  a = b;
  b = temp;
}
int main (int argc, char *argv[])
{
  int i = 10, j = 20;
  swap(i, j);
  cout << "Values i, j " << i << ", " << j;
  float a = 3.14, b = -6.28;
  swap(a, b);
  cout << "Values a, b " << a << ", " << b;
}

From the above code, it is clear that if template was not used then individual functions have to be used to carry out swap of every data type used.

Template class: Considering a class queue which adds elements to the rear and removes element from the front of an array.

#include
const int MAX = 10;
template 
class queue
{
	private: T que[MAX];
		  int top;
	public: 
};

About our authors: Team EQA

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

#