C++ Templates3 min read

Templates in C++ are the powerful feature that allows us to write generic programs (generic classes and generic functions).

A single class or function is created to work with different data types using templates. We pass the data types in a function as a parameter so that repetition of the same code can be avoided for different data types.

The passed parameter is called the template parameter. It is the same as the regular parameter but here instead of values we pass different data types.

The templates can be represented in two different ways.

  • Function Templates
  • Class Templates
templates in C++

Function Templates

We can create the function template in order to use it for different data types such as float, int, double, etc. For example, suppose we create a function called mul() for the multiplication, in normal function this function is used for specific data-types values but with templates we can pass any data types into the function.

It makes a better approach because we can write less code and save time from the repetition of writing code that is used to do the same work.

Syntax of Function Template:

  • T is a template argument or placeholder name for a data type (int, float, etc). The compiler automatically replaces it with the actual data type.
  • class is a keyword used for generic type and it can replace with typename.

Example of function templates in C++

Output:


Class Templates

Just like a function template, we can create a template for the class for generic operations that is independent of any data type.

It is required when we need the same implementation for many classes but for different data types and also avoided repetition of the same code.

Syntax of class Template:

Creating class template object.

The data type is mentioned inside <> while creating an object for the class template. For example:

let us go through an example of a class template in C++ to understand more.

Example of class templates in C++

Output:

As you can see that the result is shown according to the data type passed while creating class objects (intOb, floatOb).


MORE

Implement Insertion sort Program in C

In this tutorial, we will learn and write a C program to implement insertion sort. Before that you may go through the following topics in …
Read More

C Program for implementation of Selection sort

In this tutorial, we will learn and write a C program to implement selection sort using function. Before that you may go through the following …
Read More

C Program to Delete a Particular element from an Array

In this tutorial, we will write a C program to delete an element in an array by specifying the value. Before that, you should have …
Read More

C Program to sort a String in Ascending order

In this tutorial, we will write a c program to sort a string array in ascending order. Before that, you may go through the following …
Read More

C Program to find Maximum occurring Character in a String

In this tutorial, we will write a program in C to find the maximum occurring character in a string. Before that, you may go through …
Read More

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More