1
Chapter 6 introduces templates,
which are a C++ feature that easily
permits the reuse of existing code
for new purposes.
Template Functions
2
Here’s a small function that you might write to find
the maximum of two integers.
int maximum(int a, int b)
{
if (a > b)
Finding the Maximum of Two Integers
3
Here’s a small function that you might write to find
the maximum of two double numbers.
int maximum(double a, double b)
Finding the Maximum of Two Doubles
4
Suppose your program also has another data type, called Knafn. This
Here’s a small function that you might write to find
the maximum of two Knafns.
int maximum(Knafn a, Knafn b)
{
Finding the Maximum of Two Knafns
5
Suppose your program uses 100,000,000 different data
types, and you need a maximum function for each…
One Hundred Million Functions…
int maximum(Foo a, Foo b)
{
if (a > b)
int maximum(Noo a, Noo b)
{
if (a > b)
return a;
else
return b;
int maximum(Moo a, Moo b)
{
if (a > b)
return a;
else
return b;
int maximum(Hoo a, Hoo b)
{
if (a > b)
return a;
else
return b;
}
int maximum(Doo a, Doo b)
{
if (a > b)
return a;
else
return b;
}
int maximum(Foo a, Foo b)
{
if (a > b)
int maximum(Noo a, Noo b)
{
if (a > b)
return a;
else
return b;
}
int maximum(Moo a, Moo b)
{
if (a > b)
return a;
else
return b;
}
int maximum(Hoo a, Hoo b)
{
if (a > b)
return a;
else
return b;
}
int maximum(Doo a, Doo b)
{
if (a > b)
return a;
else
return b;
}
int maximum(Foo a, Foo b)
int maximum(Noo a, Noo b)
{
if (a > b)
return a;
else
int maximum(Moo a, Moo b)
{
if (a > b)
return a;
else
int maximum(Hoo a, Hoo b)
{
if (a > b)
return a;
else
return b;
}
int maximum(Doo a, Doo b)
{
if (a > b)
return a;
else
6
This is our first example of a template function. It is a single function
that depends on an underlying data type that I’ve called Item. The
This template function can be used with many data
types.
template <class Item>
Item maximum(Item a, Item b)
A Template Function for Maximum
7
Our maximum function depends on the type of the item that we are
When you write a template function, you choose a
data type for the function to depend upon…
template <class Item>
Item maximum(Item a, Item b)
A Template Function for Maximum
8
Before an implementation, a special template prefix is also require. The
A template prefix is also needed immediately
before the function’s implementation:
template <class Item>
Item maximum(Item a, Item b)
A Template Function for Maximum
9
Once a template function is defined, it may be used
with any adequate data type in your program…
template <class Item>
Using a Template Function
10
Here’s another function that can be made more
general by changing it to a template function:
int array_max(int data[ ], size_t n)
Finding the Maximum Item in an Array
11
Here’s the template version. Notice these things:
Here’s the template function:
template <class Item>
Item array_max(Item data[ ], size_t n)
Finding the Maximum Item in an Array
12
A template function depends on an underlying
data type.
Summary
13
Presentation copyright 1997, Addison Wesley Longman,
For use with
Data Structures and Other Objects Using C++
by Michael Main and Walter Savitch.