wap in c++ to understand class ]emplate code example

Example 1: templates classes in c++

// function template
#include <iostream>
using namespace std;

template <class T>
T GetMax (T a, T b) {
  T result;
  result = (a>b)? a : b;
  return (result);
}

int main () {
  int i=5, j=6, k;
  long l=10, m=5, n;
  k=GetMax<int>(i,j);
  n=GetMax<long>(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}

Example 2: how to write a template c++

template <class myType>
myType GetMax (myType a, myType b) {
 return (a>b?a:b);
}

Tags:

Cpp Example