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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …