#Previous question #Index of C++ Questions #Next question

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: 
};

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

 Vote 0

Similar topics related to this section

object-oriented, C vs C++, C++ vs Java, encapsulation, constructor, overloaded constructor, destructor, destructor overloaded, copy constructor, copy constructor, deep copy, deep copy vs shallow copy, singleton, default access modifier, default access modifier, default access modifier, abstraction, THIS pointer, static function, THIS pointer, a static function, scope resolution operator, inline function, private vs protected, const function, polymorphism, operator overloading, function overloading, overriding, prefix vs postfix, friend class, friend function, sizeof class with virtual, vfptr and vftable, vfptr and vftable using C, early binding and late binding, inheritance, virtual function call from a constructor/destructor, virtual destructor, virtual base class, virtual base class, exception, stack unwinding, exception handling, try-catch block, namespace, mutable variable and const function, mutable keyword, explicit, access a member function, object slicing, ctor sequence of constructor, dtor sequence of destructor, virtual destructor, print the type, dynamic casting, static casting, dynamic and static casting, const casting, reinterpret casting, static and reinterpret casting, template class, template vs macro,

#Object-Oriented Programming in C++ (4th Edition)
#Programming: Principles and Practice Using C++ (2nd Edition)
#The C++ Programming Language, 4th Edition
#Professional C++ Paperback by Nicholas A.Solter, Scott J.Kleper
#Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14