Templates in C++

Templates in C++

1. The keyword 'template' is used to define function templates and class templates in C++.

2. It is a way to make your function or class generalize as far as data type is concern.

3. It is used in generic programming. Templates in C++ is so powerful that you can write generic programs using it.

4. It is helpful in code reusability and flexibility in programs.


Types of Templates in C++

1. Function templates (or generic function).
2. Class template.


Function Templates in C++

1. Working of function templates in C++ is same as that of normal function but only one difference is there.

2. Normal function can only work with one set of datatypes but function templates can work with different datatypes at once.

3. Function template is also known as generic function.

4. You can do same task with different datatypes with the same code using generic function or function template.


Function Templates Declaration in C++

template <class type>
type Function_Name(type arg1) {
 // ... .. ...
}


template <class TYPE1, class TYPE2>
TYPE1 Function_Name(TYPE1 arg1, TYPE2 arg2) {
 // ... .. ...
}

In the above example, TYPE1 and TYPE2 are used for different data types. If you want to give two different data types then you may use this template function. 


template <class AnyName>
AnyName FunctionName(AnyName arguments) {
 // ... .. ...
}


In the above code, AnyName is a template argument that takes different datatypes (int, float, char, etc), and class is a keyword. 

You may use typename keyword in the place of class in the above example. 

When an argument of a data type is passed to FunctionName( ), compiler generates a new version of FunctionName( ) for the given data type.


For Example:- Function Template example in c++

#include<iostream>
using namespace std;

//making Template Function
template <class TEMPLATE>
TEMPLATE bigNum(TEMPLATE num1, TEMPLATE num2) {
 if (num1>num2)
  return num1;
  
 else
  return num2;
 }
 
int main() {
 cout << bigNum(4,9);
 cout << bugNum(5.6,3.9);
}


Points to Remember:-

1. C++ Compiler, while doing early binding, puts the data type according to the arguments given while calling template function

2. If the arguments are of integer type numbers, compiler puts int datatype, if arguments are floating type numbers, the compiler puts float data type.


Class Templates in C++

1. Class templates are also called generic class.

2. Class templates are used when you want to use class implementation which is same for all classes but with different data types.

3. Class templates make it easier for us to reuse the same code for all data types.


Class Templates Declaration in C++

template <class T>
class className
{
//... .. ...

public:
T variableName;
T someOperations(T arguments);

//... .. ...
};


Creating Class Template Object in C++

class_Name <dataType> class_Object_Name;

For Example:- class template objects

class_Name <int> class_Object_Name;
class_Name <float> class_Object_Name;
class_Name <string> class_Object_Name;


For Example:- Class Template example in c++

#include<iostream>
using namespace std;

template <class CLS> class ArrayLIST
{
private:
 struct ControlBlock
 {
  int capacity;
  CLS *arr_ptr;
 };
 
 ControlBlock *S;
  
public:
 ArrayLIST(int capacity)
 {
  S = new ControlBlock;
  S -> capacity = capacity;
  S -> arr_ptr - new CLS[S -> capacity];
 }
 
 void addElement(int index, CLS data)
 {
  if(index >=0 && index <= S->capacity-1)
   S -> arr_ptr[index] = data;
   
  else
   cout << "\nArray index is not valid";
 }
 
 void ViewElement(int index, CLS &data)
 {
  if(index >=0 && index <= S->capacity-1)
   data = S->arr_ptr[index];
   
  else
   cout << "\nArray index is not valid";
 }
 
 void ViewList()
 {
  int i;
  for(i=0; i<S->capacity; i++)
  {
   cout << " " << S->arr_ptr[i];
  }
 }
};

int main()
{
 int data;
 ArrayLIST <int> List1(4);
 
 List1.addElement(0, 6);
 List1.addElement(1, 5);
 List1.addElement(2, 12);
 
 List1.ViewList();
}

Class-template-example-RAM-Representation-learning-mania

Previous
Next Post »