Function Templates and Class Templates in C++ Programming | C++ Programming

Templates in C++ Programming 

Function templates in C++ Programming 

Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type. The format for declaring function templates with type parameters is:

template function_declaration;
template function_declaration;

The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way. For example, to create a template function that returns the greater one of two objects we could use:

template<class type>

myType GetMax (Type a, Type b)
{
return (a>b?a:b);
}

To use this function template we use the following format for the function call:
function_name <type>  (parameters);
For example, to call GetMax to compare two integer values of type int we can write:
int x,y;
GetMax <int>(x,y);

When the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appearance of myType by the type passed as the actual template parameter (int in this case) and then calls it. This process is automatically performed by the compiler and is invisible to the programmer.

Complete Example:

// function template
#include <iostream.h>
template <class T>
T GetMax (T a, T b)
{
T result; result = (a>b)? a : b; return (result);
}
int main ()
{
int i=5, j=6, k;
long l=10, m=5, n;
k=GetMax(i,j);
n=GetMax(l,m);
cout << k << endl;
cout << n << endl;
return 0;
}

In the example above we used the function template GetMax() twice. The first time with arguments of type int and the second one with arguments of type long. The compiler has instantiated and then called each time the appropriate version of the function. As you can see, the type T is used within the GetMax() template function even to declare new objects of that type:

T result;

Therefore, result will be an object of the same type as the parameters a and b when the function template is instantiated with a specific type.

We can also define function templates that accept more than one type parameter, simply by specifying more template parameters between the angle brackets. For example:

template <classT, class U>

GetMin (T a, U b)
{
return (a<b?a:b);
}

In this case, our function template GetMin() accepts two parameters of different types and returns an object of the same type as the first parameter (T) that is passed. For example, after that declaration we could call GetMin() with:

int i,j;
long l;
i = GetMin <int,long> (j,l);

Class templates in C++ Programming 

We also have the possibility to write class templates, so that a class can have members that use template parameters as types. For example:

template class mypair  { T values [2];
public:
 mypair (T first, T second)
{
values[0]=first;
values[1]=second;
}};

The class that we have just defined serves to store two elements of any valid type. For example, if we wanted to declare an object of this class to store two integer values of type int with the values 115 and 36 we would write:
mypair <int> myobject (115, 36);

This same class would also be used to create an object to store any other type:
mypair myfloats (3.0, 2.18);


// class templates
#include <iostream>
template <class T>
class mypair
{
T a, b; public:

mypair (T first, T second) { a=first; b=second;
}
T getmax ()
{
T retval;
retval = a>b? a : b; return retval;
}
};

int main ( )
{
mypair <int>myobject (100, 75);
cout << myobject.getmax();
return 0;
}


Comments

Popular posts from this blog

C Program for SCAN Disk Scheduling Algorithm | C Programming

C program to Find Cartesian Product of Two Sets | C programming

C Program To Check The String Is Valid Identifier Or Not | C Programming