Templates in C++ programming

By  Falak Hasija    51 - 25 August, 20
templates in C++ programming

Share article on social media


In C++, templates are a model of a function or a class that can be used to generate functions or classes. During the compilation, C++ uses the template to generate functions and classes. For example, given a function template, one, two, or more concrete functions can be generated in the program. Similarly, given a class template, one, two, or more concrete classes can be generated from it.

Templates provides a major implementation for code reusaibilty. From one template, different functions and classes can be easily created. 

FUNCTION TEMPLATES:

A function template can create multiple functions, each with potentially different arguments and return types.

 

Demonstrate function template.

#include

#include

#include

using namespace std;

template

TYPE max(TYPE x, TYPE y)

{

return (x>y) ? x : y;

}

int main()

{

int i1 = rand();

int i2 = rand();

cout<<"Given "<

float f1 = rand()/3.3;

float f2 = rand()*6.7;

cout<<"\nGiven "<

return 0;

 

RESULTS

Given 16838 and 5758: 16838 is larger

Given 3064.55 and 117350: 117350 is larger

 

TEMPLATE FORMAT:

A function template begins with template prefix that provides the generic parameters, enclosed in a set of pointed brackets, used in the template.

template

Function templates allow us to write a single function for a whole family of similar functions.

 

OVERLOADING FUNCTION TEMPLATES

There are times when a function template won't work. Perhaps the most common situations occur when one or more of the arguments is a pointer or the relational operators are not defined for the object. 

In these situations, the solution is to write an overloaded function that handles the specific types. 

 

Overloading max for fractions

template

TYPE max (TYPE x, TYPE y)

{

return (x>y) ? x : y;

}

Fraction max(Fraction fr1, Fraction f2)

{

if (fr1.compare(fr2)>0)

return fr1;

else

return fr2;

}

 

CLASS TEMPLATES:

There are several occasions when we need to have not only one class, but several classes in which the objects of each class behave the same while their attributes are of different types. For example, if we were to create a generalised array type using a class structure, we would need to define several different classes; one for array of integers, one for array of floats and so on. This is the purpose of class template.

 

 CLASS TEMPLATE SYNTAX:

The class declaration is immediately preeceded by a template prefix that specifies the generic types to be associated with the class. The class then uses generic types to complete its declaration.

 

CLASS TEMPLATE SYNTAX

template

class array

{

protected:

TYPE* ary;

int capacity;

int next;

public:

.....

};

 

DEFINING THE CLASS:

The generation of a class with the template follows the rules for any class, with five differences:

  1.  The parameterized types can be used in declaring data members.
  2.  The parameterized types can be used in declaring function members.
  3.  All functions must be defined using the template prefix.
  4.  References to the class in scope definition, such as in function headers, must include the parameterized types.
  5.  Class instantiation must include concrete types.

CLASS DEFINITION:

#include

using namespace std;

template

class array

{

protected:

TYPE* ary;

int capacity;

int next;

public:

array (int size);

void append (TYPE data);

};

template

array : : array (int size)

{

.....

}

template

array : : append (TYPE data)

{

....

}

 

SPECIALIZED MEMBER FUNCTIONS:

As with function templates, there will be occasions when the template does not support a specific need. With function templates, we wrote an overloaded function. With classes, we write specialized class templates. 

Specialized classes must follow the following rules:

  1. The class must be rewritten.
  2. The template statement for the specialized class has no parameters between pointed brackets.
  3. The specific type(s) must be declared in the class statement.