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

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 …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More