Home > DeveloperSection > Blogs > Templates in C++

Templates in C


.NET Visual C++ 
Ratings:
0 Comment(s)
 538  View(s)
Rate this:

Templates in C++

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>

 using namespace std;

using namespace System;

template<typename T>

class operation

{

public:

void Add(T firstNum,T SecNum)

{

            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;

}


Don't want to miss updates? Please click the below button!

Follow MindStick