Advisory Board Interview Question: whats' the template in c++,ob... | Glassdoor

Interview Question

Software Engineer Interview Austin, TX

whats' the template in c++,object,class.

Answer

Interview Answer

1 Answer

1

Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

Example
// function template
#include
using namespace std;

template
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(i,j);
  n=GetMax(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}

Ciberoid on Jul 24, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.