***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Saturday, July 24, 2021

Function Templates

A function template is a “generic” function that can work with any data type.

Function templates allows us to write a single function definition that works with many different data types, instead of having to write a separate function for each data type used. The programmer writes the specifications of the function, but substitutes parameters for data types. When the compiler encounters a call to the function, it generates code to handle the specific data type(s) used in the call.

Here is a function template for the square function:

template <class T>

T square(T number)

{

return number * number;

}

The beginning of a function template is marked by a template prefix , which begins with the key word template .

Set of angled brackets that contain one or more generic data types are used in the template. A generic data type starts with the key word class followed by a parameter name that stands for the data type. After this, the function definition is written as usual, except the type parameters are substituted for the actual data type names.

 

Example:

// This program uses a function template.

#include <iostream.h>

// Template definition for square function.

template <class T>

T square(T number)

{

return number * number;

}

void main()

{

int userInt; // To hold integer input

double userDouble; // To hold double input

cout << "Enter an integer and a floating-point value: ";

cin >> userInt >> userDouble;

cout << "Here are their squares: ";

cout << square(userInt) << " and " << square(userDouble) << endl;

}

 

A function template is merely the specification of a function and by itself does not cause memory to be used. An actual instance of the function is created in memory when the compiler encounters a call to the template function.

 

Function Templates with Multiple Types

While creating templates, it is possible to specify more than one type. More than one generic data type can be used in a class template. They are declared as a comma-separated list within the template as below

Syntax:

template<class T1, class T2, ...>

class classname

{

      ...

      ...

};

 

Example: CPP program to illustrate Class template with multiple parameters

#include<iostream>

using namespace std;

 

// Class template with two parameters

template<class T1, class T2>

class Test

{

                                              T1 a;

                                              T2 b;

                          public:

                                              Test(T1 x, T2 y)

                                              {

                                                   a = x;

                                                   b = y;

                                              }

                                              void show()

                                              {

                                                   cout << a << " and " << b << endl;

                                              }

};

void main()

{

                          // instantiation with float and int type

                          Test <float, int> test1 (1.23, 123);

                         

                          // instantiation with float and char type

                          Test <int, char> test2 (100, 'W');

                         

                          test1.show();

                          test2.show();

}

 

Because the function parameters are specified with different types, the function generated from this template can accept two arguments of different types.

No comments:

Post a Comment