Templates

In this article, you will learn about Templates in C++ and it's useful for generic programming in C++.

Templates are a feature of the C++ programming language that allows single functions and classes to handle different data types. This helps you to write generic programs.

Generic Programming

Generic programming is a style of computer programming in which program are not compiled as it is but rather 'Templates' of source code are transform into source code at the time of compilation according to supplied datatype as parameters

In Templates of C++ template of class or function is defined with general data type and is replaced by a specified data type at the time of actual use of class or function , Hence it follow Generic programming

(adsbygoogle = window.adsbygoogle || []).push({});

An Example to Begin With

This program add two number of different data type according to type of supplied argument

Output

Result of adding two float : 13.7

Result of adding two integer : 13

At the time of compilation 'a' is replaced by datatype according to supplied data type such as :

int add (int c , int d)
{
      int sum ;
   sum = c + d ;
   return sum; 
}
1
2
3
4
5
6

Type of Templates:

  • Function Templates
  • Class Templates

Function Templates:

Function Template is a template which is used to define function which can handle arguments of different types in different times

This avoids overhead of rewriting a function having body of same patterns but operating for different data types.Defining a function template contains two steps

  1. Defining general data type :
template <class template_name>

Defining a function with general data type :

template_name function_name(arguments.....)
{
//body of function with data type
}
1
2
3
4
5
6
7
8

Class Templates:

A class template is a kind of class which has member of template type and the actual type of data being manipulated will be specified as a parameter when the object of that class will be created

Template classes provide mechanism for creating applications with generic types , which are common applications such as lists ,stacks and queues etc .Defining a class template contains three steps

Defining template :

template <class template_name>
1

Defining class with members of template type :

class class_name {
// class member with datatypes template_name whenever appropriate
}
1
2
3

Defining objects :

class_name <specific_data_type> object_name
1

Standard Template Library:

Standard Template Library is a software library for the C++ programming language that mainly operate in data providing container to manage collection of objects of a certain type , algorithms to perform actions on object of container , and iterators to steps through elements of container. Containers

Containers are used to manage collections of objects of certain kind. There are several different types of containers like list , map , vector etc Algorithms

Algorithms act on containers . They provide means by which we will perform initialization , sorting , searching etc Iterators

Iterators are used to step through elements of collections of objects . These collections may be containers or subsets of containers

var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date(); (function(){ var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0]; s1.async=true; s1.src='https://embed.tawk.to/5bd7b562476c2f239ff683f0/default'; s1.charset='UTF-8'; s1.setAttribute('crossorigin','*'); s0.parentNode.insertBefore(s1,s0); })();
(adsbygoogle = window.adsbygoogle || []).push({});