11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 27Tempting C++ TemplatesIn This Chapter Examining how templates can be applied to functions Combining common functions into a single template definition Defining a template or class Reviewing the advantages of a template over the more generic “void” approachThe Standard C++ Library provides a set of basic functions. The C++ librarypresents 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-calledcharacter string functions defined in the include file strings.h. The argumenttypes for many of these functions are fixed. For example, both of the argumentsto strcpy(char*, char*) must be a pointer to a null-terminated characterstring — nothing else makes sense.There are functions that are applicable to multiple types. Consider the exampleof the lowly max() function, which returns the maximum of two arguments.The function declarations in Table 27-1 all make sense.Table 27-1Function Namemaximum(int, int):maximum (unsignedint, unsigned int)Maximum(double, double)Maximum(char, char)Possible Variants of maximum() FunctionOperation PerformedReturns the maximum of two integers.Returns the maximum of two unsigned values.Because there are no negative numbers, theexpression (0 - 1) “rolls over” to become a verylarge unsigned value, rather than the “normal” orsigned value –1.Performs the same comparison operation but onfloating numbers.Returns the character that occurs later in the alphabet(including all special characters).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!