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

C Program to Convert Decimal to Binary

In this tutorial, we will write a C program to convert decimal to binary. Before that, you must have knowledge of the following topics in …
Read More

C Program to Convert Binary to Decimal

In this tutorial, we will write a program to convert binary to decimal in C. Before that, you must have knowledge of the following topics …
Read More

C Program to Print ASCII Value of a Character

In this tutorial, we will write a program to print ASCII value of a character in C programming. Before that, you should have knowledge on …
Read More

Java Program to Convert Binary to Hexadecimal

In this tutorial, we will write a Java Program to Convert a Binary Number to Hexadecimal Number. Before that, you must have knowledge of the …
Read More

Java Program to Convert Hexadecimal to Binary

In this tutorial, we will write a Java Program to Convert a Hexadecimal Number to Binary Number. Before that, you must have knowledge of the …
Read More

Java Program to Convert Hexadecimal to Decimal

This is a Java Program to Convert a Hexadecimal Number to its decimal Equivalent. Before that, you must have knowledge of the following topics in …
Read More