CPP Language / Template Function And Template Class

Template Class
It is used to create a single function / a class to work with different data types. Templates used for the purpose of code reusability and flexibility of the programs.

Templates used are of two different ways:
1. Function Templates.
2. Class Templates.

Function Templates
Template class will have Template functions these can be used to work with different data types at once. I.e In place of function overloading we use Function Templates.

Templates Syntax Explanation

template
T SampleFunction(T arg)
{
... .. ...
}

T is a template argument that accepts different data types (int, float) by a SampleFunction, and class is a keyword.

Program Output

#include
using namespace std;

// template function
template
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(i1, i2) <<" is larger." << endl;

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

cout << "\nEnter two characters:\n";
cin >> c1 >> c2;
cout << Large(c1, c2) << " has larger ASCII value.";

return 0;
}

Enter two integers:
10 20
20 is larger.

Enter two floating-point numbers:
2.4 3.4
3.4 is larger.

Enter two characters:
a b

b has larger ASCII value.

Class Templates
It consists of member variables and member function that can take different data types at different times.

Class Templates Syntax Explanation

template
class className
{
... .. ...
public:
T variable;
T SampleOperation(T arg);
... .. ...
};

className classObject;

For example:
className classObject;
className classObject;
className classObject;


T is the template argument
member variable = variable
member function = someOperation()
both member variable and member function are of type T.

Program for calculator(+,/,*,-) using class template output

#include
using namespace std;

template
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 intCalc(2, 1);
Calculator floatCalc(2.4, 1.2);

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

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

return 0;
}

Int results:
Numbers are: 2 and 1.
Addition is: 3
Subtraction is: 1
Product is: 2
Division is: 2

Float results:
Numbers are: 2.4 and 1.2.
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2


Template Functions Overloading
Overloading of template functions is called as Template Functions Overloading.

Functions Overloading Template Functions Overloading

int f( int );
long f( double );
int i; double d;
f( i ); // calls
f(int)
f( d );
f(double)


template
void f( T1, T2 );
template void fun( T );
template void fun( T, T );
template void fun( T* );
template void fun( T*, T );
template void fun( T, T* );
template void fun( int, T* );
template<> void fun( int );
void fun( int, double );
void fun( int );


Program for Template Functions Overloading Output

#include
using namespace std;
template void fun(T x, T y) { cout << "Template" << endl; }
void fun(int w, int z) { cout << "Non-template" << endl; }

int main()
{
fun( 1 , 2 );
fun('a', 'b');
fun('1', '2');
return 0;
}


Non-template
Template
Template



Home     Back