Tempaltes

This blog has been migrated to learn c programming for beginners


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



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; 
}



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

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
}

Example program of Function Templates:

Q) Use Template to add two integers , two floats and one integer and one float respectively and display final result in float


Output


Result of adding two integer : 13

Result of adding two float : 13.7

Result of adding one integer and one float : 13.2



Q) Find the sum and average of elements in an array using function templates


Output


Result of adding  integer array : 73

Result of average of  integer array : 12

Result of adding  float array : 74.1

Result of average of  float array : 12.35


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>

Defining class with members of template type :

class class_name {
// class member with datatypes template_name whenever appropriate
}

Defining objects :

class_name <specific_data_type> object_name

Example program of Class Templates:

Q) Use Class Template to add data of different data type


Output


Passing integer :
The sum  is :7
Passing float :
The sum  is :73.68

Q) Create Stack Using Class Template


Output


7
hello
Exception: Stack<>::pop(): empty stack

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

Example program of Standard Template Library:

Q) Use Vector to create array of integer

Output


Leatest pushed element is :12

The size of array is : 2

Element at position '0' is :32


Comments

Popular posts from this blog

Copy Constructor

Print pattern in c - explore new perspectives in pattern printing

C program to check if the supplied year is leap year or not.