CPTS 122 DATA STRUCTURE
Washington State University
Nadra Guizani
OVERLOADING FUNCTIONS
FUNCTION TEMPLATE
• The only differences between these two functions are the data types of their return
values and their parameters.
• In situations like this, it is more convenient to write a function template than an
overloaded function
• Function templates allow you to write a single function definition that works with many
different data types, instead of having to write a separate function for each data type
used.
• a “mold” the compiler uses to generate one or more functions
FUNCTION TEMPLATE
• template prefix
• A generic data type starts with the key word class followed
by a parameter name that stands for the data type.
FUNCTION TEMPLATE
FUNCTION TEMPLATE
CLASS TEMPLATES
• Templates may also be used to create generic classes and abstract data types.
• Declaring a class template is very similar to declaring a function template.
• First, a template prefix, such as template<class T>
• The data type parameter is used where you wish to support any data type
• Defining Objects of the Class Template:
NOTES
• Templates will only take literal values of the type specified
• Main source files need to include both header and source file of template class
in the library includes
• If class members function are defined outside the class. It must include the
template prefix as part of the function definition (whether they are utilizing the
template variable or not)
EXAMPLE:
DynamicQueue.h
DynamicQueue.cpp
mainTemplate.cpp