Template in C++

In  C++ programming language, Template is used to declare a group of function or class in a generic manner.

Generic  means that that can process all types of data.

By using template a single function can defined for a group of functions, it is called as a “function template”.

Similarly, when template is associated with classes are known as class template.

Advantage of function template:  It avoid unnecessary repetition of source code.

Using template we can create a single function that can process any type of data .

It can accept data of any type such as int, float, long etc.

    Syntax for declare class template:                       

“template” is a keywords.

“template class <t>”  statement tells the compiler the following class declaration can use the template data type.

Here T is a variable of template type. It is used to define variable of template (generic) type.

Within the angle bracket <> we declare a variable of template.

We can declare one or more variable separated by comma.

“template” always be global and should not be local means template cannot be declared inside of function or class.                       

T  k;     Where , k is variable of type template. 

  Normal Template Function Declaration

Syntax for Template Function

template class < T > type function_name() { // code
}

Example:   Write a program to perform constructor overloading and pass different data type argument.

Output

In the above program we have created three overloaded constructor, first accept integer data as argument and second accepts float data as argument and third constructor accepts character data as argument.

This approach has following disadvantages

  • Re-defining the same function separately for each data type. It increases the requires more time.  
  • Size of program is increased. Hence, occupies more memory.  
  • If constructor/function contains bug, it should be correct in every constructor/function. 

Example:   Write a program to create constructor and pass different data type as argument using  template.

Output

In the above program we have created a class “xyz” and one constructor it can accept all data type because, constructor contains the variable of template T.

Normal Template  Function Declaration

Syntax for Template Function

template class < T > type function_name() { // code
}

Example:   Write a program to define normal template function.

Output

Example:   Write a program to find the square of different data type using template.

Output

Class Template With More Parameter

In C++ programming, class template can contain one or more parameter of generic data type.

The argument is separated by comma with template declaration.

Syntax for class template with more than one parameter:

template < class T1, class T2 > class name_of_class {
}

Example:   Write a program to define a constructor with multiple template variables.

Output

Example:   Write a program to exchange the value of two variable. Use Template variable as function argument.

Output

Overloading of Template Function

In a C++ programming language, like a function & constructor we can also override the template.

If we create more than one function with same name and one function take template type data as a argument and another function takes primitive data type as a argument then it overrides the function which takes template data as an argument.

Output

In the above program we have created two function (show ()) with same name.

One function take template type data as a argument and another function takes integer data type as a argument then it overrides the function which takes template data as an argument.

Categories C++