0% found this document useful (0 votes)
14 views16 pages

Chapt 06

Uploaded by

rajkumar184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Chapt 06

Uploaded by

rajkumar184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Template Functions

Template

A C++ template is a powerful feature added to


C++. It allows you to define the generic classes
and generic functions and thus provides support
for generic programming. Generic programming
is a technique where generic types are used as
parameters in algorithms so that they can work
for a variety of data types.
Templates are expanded at compiler time.
Template

Templates can be represented in two ways:


•Function templates
•Class templates
Function Templates

It is a generic function that can be used for different


data types.

We can define a template for a function. For example, if


we have an add() function, we can create versions of the
add function for adding the int, float or double type
values
Finding the Maximum of Two Integers

 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)
return a;
else
return b;
}
Finding the Maximum of Two Doubles

 Here’s a small function that you might write to find


the maximum of two double numbers.
Double maximum(double a, double b)
{
if (a > b)
return a;
else
return b;
}
Finding the Maximum of Two Knafns

 Here’s a small function that you might write to find


the maximum of two knafns.
knafn maximum(knafn a, knafn b)
{
if (a > b)
return a;
else
return b;
}
One Hundred Million Functions...

 Suppose your program uses 100,000,000 different data


types, and you need a maximum function for each...
int maximum(Hoo a, Hoo b)
{ int maximum(Hoo
a, Doo b) a, Hoo b)
int maximum(Noo a, Noo b)if (a > b) int maximum(Moo a, int Moo maximum(Doo
b)
int maximum(Hoo a, Hoo b) { { int maximum(Moo a, int Moomaximum(Doo
b) a, Doo b)
{ { int maximum(Noo a, Noo b)if (a > b)
{ int maximum(Doo return
a, Dooa;b) if (a > b) { {
int maximum(Moo a,
if (a > b) { Moo b) if (a > {b)
int maximum(Noo a, Noo b)if (a > b) else return a; return a; if (a > b)
{ return a;if (a > b) if (a > b)
{ return a; return a; if (a > b) return b; int maximum(Foo a, Foo b) else else return a;
if (a > b)else else return a; return a;
if (a > b) else return }a; { return b; return b; int maximum(Foo a,else Foo b) else
return a; return b; return b;else
return a; return b; int maximum(Foo a,else Foo b) else if (a > b) } } { return b;
} return b; return b;
else } } return b; return a; if (a > b) }
{ return b; }
return b; } return a;
if (a > b) } } else
} return a; return b; else
else int maximum(Poo a, Poo b) } return b;
int maximum(Boo
int maximum(Poo a, Pooa, b)
Boo b)
return b; { } int maximum(Boo a, Boo b)
int maximum(Koo a, Koo b) { {
int maximum(Poo a, Poo b) } if (a > b) {
{
int maximum(Boo a,{ Boo b)
return a; if (a if> (a
b) > b) int maximum(Koo a, Koo b)
int maximum(Joo a, Joo b) return if (a > b)
if (a > b) int maximum(Koo a, Koo b) {
else if (a > b) return a; a; { int maximum(Joo a, Joo b)
if (a > b) a, Knafn {b)return return a;
return a; { int maximum(Joo a, Joo b) int maximum(Knafn
return b; int maximum(Ioo
a; a, Ioo b) else else if (a > b)
{ return a; if (a > b)
{ int maximum(Knafn
return return
b; b; a, Knafn {b)return int maximum(Ioo
a; a, Ioo b) else
else if (a > b) } else if (a > b)
{ int maximum(Ioo a, Ioo b) return a; {} } return b;
int maximum(Knafn
return b; a, Knafn b)return a; if (a >else
b) return ifb;(a > b) else {
return a;
if (a > b) return b; else if (a > b) if (a > b) }
{} else { return a; } return a; return b;
return a; return b; else
if (a > b) returnifb;(a > b) else} else return a; } return a;
return b;
else } else else
return a; } return a; return b; return b;
return b; int maximum(Coo a, Coo b) }
else else } } return b; return b;
} { int maximum(Coo a, Coo b)
return b; return b; } }
int maximum(Coo a, Coo b) if (a > b) int maximum(Goo a, Goo b) {
} } int maximum(Loo a, Loo b) if (a > b) int maximum(Goo a, Goo b)
{ return a; { int maximum(Loo a, Loo b)
{ return a; {
if (a > b) int maximum(Goo a, Goo b) else if (a > b) {
int maximum(Loo a, Loo b) if (a > b) else if (a > b)
return a; { return b; return a; if (a > b)
{ return a; return b; return a;
else if (a > b) else } else return a;
if (a > b) } else
return b; return a; return b; else
return a; return b; return b;
} else } return b;
else } }
return b; }
return b;
}
}
A Template Function for Maximum

 This template function can be used with many data


types.
template <class Item>
Item maximum(Item a, Item b)
{
if (a > b)
return a;
else
return b;
}
A Template Function for Maximum

 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)
{
if (a > b)
return a;
else
return b;
}
A Template Function for Maximum

 A template prefix is also needed immediately


before the function’s implementation:
template <class Item>
Item maximum(Item a, Item b)
{
if (a > b)
return a;
else
return b;
}
Using a Template Function

 Once a template function is defined, it may be used


with any adequate data type in your program...
template <class Item>
Item maximum(Item a, Item b) cout << maximum(1,2);
{ cout << maximum(1.3, 0.9);
if (a > b) ...
return a;
else
return b;
}
Finding the Maximum Item in an Array

 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)
{
size_t i;
int answer;

assert(n > 0);


answer = data[0];
for (i = 1; i < n; i++)
if (data[i] > answer) answer = data[i];
return answer;
}
Finding the Maximum Item in an Array

 Here’s the template function:

template <class Item>


Item array_max(Item data[ ], size_t n)
{
size_t i;
Item answer;

assert(n > 0);


answer = data[0];
for (i = 1; i < n; i++)
if (data[i] > answer) answer = data[i];
return answer;
}
Summary

 A template function depends on an underlying


data type.
 More complex template functions and template
classes are discussed in Chapter 6.
Presentation copyright 2010, Addison Wesley Longman,
For use with Data Structures and Other Objects Using C++
by Michael Main and Walter Savitch.

Some artwork in the presentation is used with permission from Presentation Task Force
(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright
Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club
Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

Students and instructors who use Data Structures and Other Objects Using C++ are welcome
to use this presentation however they see fit, so long as this copyright notice remains
intact.

THE END

You might also like