Tuesday, April 10, 2012

Function Templates with Examples

             Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

Example :

#include <iostream>

using namespace std;


template <class T>
T Add (T a, T b) {
  T result;
  result = a+b;
  return (result);
}

int main()
{
    int i=5, j=6, k;
    k=Add<int>(i,j);
    cout << k << endl;
    return 0;
}

No comments:

Post a Comment