C++ Language



Admission Enquiry Form

  

Templates in C++:


What are Templates in C++ ?

Templates are powerful features of C++ which allows us to write generic programs e.g. generic classes and generic functions.Generic programming is a technique which is type safe where generic types are used as parameters so that they can work for a variety of data types. There are two ways we can implement templates:

  • Function Templates
  • Class Templates


Function Templates:

We can define a template for a function. For example, if we have an add() function, we can create versions of the add function for adding the int, float or double type values.


Class Template:

We can define a template for a class. For example, a class template can be created for the array class that can accept the array of various types such as int array, float array or double array.




Syntax of Function Template

template <class T>
T functionName(T parameter1, T parameter2, ...)
{
// code
}

Note:

A function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition.
In the above syntax, T is a template argument that accepts different data types (int, float,double etc.), and class is a keyword.



Program of Function Template in C++

#include <iostream>
using namespace std;
// template function
template <class T>
T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}

int main()
{
int i1, i2;
float f1, f2;
char c1, c2;

cout << "Enter two integers:\n";
cin>> i1 >> i2;
cout<< Large<int>(i1, i2) <<" is larger." << endl;

cout<< "\nEnter two floating-point numbers:\n";
cin>> f1 >> f2;
cout<< Large<float>(f1, f2) <<" is larger." << endl;

cout << "\nEnter two characters:\n";
cin>> c1 >> c2;
cout<< Large<char>(c1, c2) << " has larger ASCII value."; }//end of main




Another Program of Function Template in C++

#include <iostream>
using namespace std;
// template function
template <class T>
T sum(T n1, T n2)
{
return (n1+n2);
}

int main()
{
int i1,i2;
float f1, f2;

cout << "Enter two integers:\n";
cin>> i1 >> i2;
cout<<"The sum of two integer is " <<sum<int>(i1, i2)<<endl;

cout<< "\nEnter two floating-point numbers:\n";
cin>> f1 >> f2;
cout<<"The sum of two integer is " <<sum<float>(f1, f2)<<endl;

}//end of main




Syntax of class Template

template<class Ttype> 
class class_name 

private:
T var1;
... .. ...
public:
T functionName(T arg);
... .. ...
}; 

Note:

A class template starts with the keyword template followed by template parameter(s) inside <> which is followed by the class declaration.
In the above declaration, T is the template argument which is a placeholder for the data type used, and class is a keyword.
Inside the class body, a member variable var1 and a member function functionName() are both of type T.



Program of Class Template in C++

#include <iostream>
using namespace std;

template <class T>
class Calculator
{
private:
T num1, num2;

public:
Calculator(T n1, T n2)
{
num1 = n1;
num2 = n2;
}

void displayResult()
{
cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
cout << "Addition is: " << add() << endl;
cout << "Subtraction is: " << subtract() << endl;
cout << "Product is: " << multiply() << endl;
cout << "Division is: " << divide() << endl;
}

T add()
{
return num1 + num2;
}

T subtract()
{
return num1 - num2;
}

T multiply()
{
return num1 * num2;
}

T divide()
{
return num1 / num2;
}
};
int main()
{

Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);

cout << "Int results:" << endl;
intCalc.displayResult();

cout << endl << "Float results:" << endl;
floatCalc.displayResult();

}