In This Chapter
Examining how templates can be applied to functions
Combining common functions into a single template definition
Defining a template or class
Implementing an initializer list for a user-defined class
The standard C++ library provides a complete set of math, time, input/output, and DOS operations, to name just a few. Many of the earlier programs in this book use the so-called character string functions defined in the include file strings. The argument types for many of these functions are fixed. For example, both arguments to strcpy(char*, char*) must be a pointer to a null-terminated character string — nothing else makes sense.
There are functions that are applicable to multiple types. Consider the example of the lowly maximum() function, which returns the maximum of two arguments. All of the following variations make sense:
int maximum(int n1, int n2); // return max of two integersunsigned maximum (unsigned u1, unsigned u2);double maximum (double d1, double d2);char maximum (char c1, char c2);
I would like to implement ...