Non-Template Functions Next Slide

Suppose we want to write a function that finds the maximum of two things. To find the maximum of two integers we would write:

int maximum(int a, int b) 
{
  if (a > b) 
    return a; 
  else 
    return b; 
} 

and to find the maximum of two doubles we would write:

double maximum(double a, double b) 
{
 if (a > b) 
   return a; 
 else 
   return b; 
} 

and so on. You can imagine that we would have to write a separate function for every data type (chars, strings, dates, etc.), but notice that the body of each function is exactly the same!!!


BU CAS CS - Templates
This page created by Jonathan Alon <jalon@cs.bu.edu>.
Material adapted from the textbook "Data Structures and Other Objects Using C++", by Main and Savitch.
Modified by Robert I. Pitts <rip@bu.edu>