Types of Templates in C++


              The types of templates are listed as follows.

  • Class Templates
  • Function Templates
  • Variable Templates (Since C++ 14)

Class Templates:

              It is a type of template which handles class to define the template. This template can be used for any data types.

Program:

This program creates a template “Sample” with a constructor and ‘get_Data()’ function to get the values from the user.

In the main() function, various data are send to the get_Data() to print the data.

Code:

#include <iostream>

using namespace std;

template <typename T> class Sample

              {

                public:

                  T a;

                  T b;

                  // Constructor

                  Sample(T data1, T data2) : a(data1), b(data2)

                  {

                  }       

                  // Method to get values

                  void get_Data()

                  {

                      cout <<"The values are:"<<endl;

                      cout << a << " " << b;

                  }

              };

              int main()

              {

                  // Creating objects of Sample with different data types

                  Sample<int> intSample(510, 620);

                  Sample<double> doubleSample(11.19, 76.28);      

                  // Access the templates values

                  intSample.get_Data();

                  cout << endl;

                  doubleSample.get_Data();

                  return 0;

}

Output:

The values are:

510 620

The values are:

11.19 76.28

Note: Class templates are useful for data structures like linked list,stack,queue and so on.

Function Templates:

              This is the second type of template. A function is written once and it can be used for all data types.

Program:

              Here, a template “myadd()” is created as function. it can be used for integer, float and double data types.

Code:

#include <iostream>

using namespace std;

template <typename T> T myadd(T x, T y, T z)

              {

                  return (x+y+z);

              }

int main()

{            

        cout<< "The addition of three integers:"<<endl;

                  cout << myadd<int>(9,6,3) << endl;

                  cout<< "The addition of three float variables:"<<endl;

                  cout << myadd<float>(3.2, 6.01,5.6) << endl;

                  cout<< "The addition of three double variables:"<<endl;

                  cout << myadd<double>(23.4,45.7,78.6) << endl;

                  return 0;

              }

Output:

The addition of three integers:

18

The addition of three float variables:

14.81

The addition of three double variables:

147.7

Variable Templates (Since C++ 14):

              This is the third type. Actually, it is a variable which can be used to many data types.

Eg: a constant variable e(exponent) which has constant value(2.7182818284). The same variable can be used to other data types.

Program:

#include <iostream>

using namespace std;

// Template variable with constexpr

template <typename T> constexpr T e = T(2.7182818284);

int main()

              {

                  // Using e with different types

                 cout << "e(exponent) as float: " << e<float> << endl;

                 cout << "e(exponent) as double: " << e<double>;

                  return 0;

              }

Output:

e(exponent) as float: 2.71828

e(exponent) as double: 2.71828

These are the three types of templates explained briefly with suitable code. Keep Coding!!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.