blog

Home / DeveloperSection / Blogs / Templates in C++

Templates in C++

priyanka kushwaha2047 05-Feb-2015

In this blog, I’m explaining about templates in C++

 

Templates are also known as generic function or class which are used to implement a generic structure for many structures of the same statements with different data types.

 There are two types of templates:

1. Function template                                                                                                                                                   

2. Class template

Syntax
Template<Typename T>                                                                                                                       
 Template<class T>
Feathers of templates:

1.      A class generated from a class template is called a generate class.


2.      A function generated from a function template is called a generate 


function
3.      A static data member generated from a static data member template


is called a generated static data member.

 

Example:
 // TemplatesProgram.cpp : main project file.
 #include"stdafx.h"
#include<iostream>
 usingnamespace std;
usingnamespace System;
template<typenameT>
classoperation
{
public:
void Add(TfirstNum,TSecNum)
{
            cout<<"Result"<<firstNum+SecNum;
}
};
 
int main(array<System::String ^> ^args)
{
            operation<double> op;
            double firstNum,secNum;
            cout<<"Enter first Number";
            cin>>firstNum;
            cout<<"Enter second number";
            cin>>secNum;
            op.Add(firstNum,secNum);
            Console::ReadKey(0);
    return 0;
}

Updated 05-Feb-2015

Leave Comment

Comments

Liked By