Lập trình hướng đối tượng – Templates
1
Chương 5
TEMPLATES
Lập trình hướng đối tượng – Templates
2
Tài liệu đọc
Lập trình hướng đối tượng – Templates
3
Templates
•
Function templates
•
Class templates
•
Inheriting Template Classes
Lập trình hướng đối tượng – Templates
4
Templates
•
According to the dictionary, a template
is a pattern or guide used to replicate
an object e.g. a biscuit cutter.
Lập trình hướng đối tượng – Templates
5
Function templates
•
A function template is a pattern for creating a
family of similar functions.
•
If you need two or more functions with
identical behaviour that differ only in their
parameter types, you can write a function
template that the compiler will use to generate
the definitions of the actual functions.
Lập trình hướng đối tượng – Templates
6
Function templates
•
A function template has the general form
template <class T, >
returnType functionName(parameterList)
{
// Body of the function
}
Lập trình hướng đối tượng – Templates
7
Function templates (I)
•
The keyword template indicates that what follows
is a function template, not an actual function.
•
The notation <class T, > is the template
parameter list.
Lập trình hướng đối tượng – Templates
8
Function templates (II)
•
The keyword class indicates that T is a
generic type i.e. a placeholder for a data type
used by the function.
•
The identifier T appears throughout the
function definition wherever this type needs
to be written.
Lập trình hướng đối tượng – Templates
9
Generated functions
•
When the function template is called, the
compiler deduces the type of the actual
argument and substitutes it for the generic
type T, creating what is known as a
generated function.
•
The act of generating a function is referred
to as instantiating the template.
Lập trình hướng đối tượng – Templates
10
template <class T>
void swap ( T& first, T& second)
{
T temporary = first;
first = second;
second = temporary;
}
Example 1
Lập trình hướng đối tượng – Templates
11
int main(void)
{
int x = 1 , y = 2 ;
char a = 'A‘ ;
char b = ‘B’;
swap(x, y);
swap(a, b);
//
return 0;
}
Example 1 2
Lập trình hướng đối tượng – Templates
12
Explicitly specifying the type
•
The actual type that is substituted for a template's
generic type can be explicitly specified in a function
call by writing it between angle brackets immediately
after the function name.
int i = 11;
int j = 22;
swap<double>(i, j);
•
The type that is explicitly specified takes precedence
over the type that is deduced by the compiler.
Lập trình hướng đối tượng – Templates
13
Instantiating with different types
•
You cannot instantiate a function template if
the types of arguments in your function call
fail to match the template's expectations.
int r = 3;
double s = 4.4;
swap(r, s); // Error
Lập trình hướng đối tượng – Templates
14
Mixing generic types and actual types
•
A function template's parameter list may
contain a mixture of generic and actual types.
template <class T>
void rotate ( T array[ ], int size)
{
T temporary = array[0];
for (int i = 0; i <= size - 2; i++)
array[i] = array[i + 1];
array[size - 1] = temporary;
}
Lập trình hướng đối tượng – Templates
15
Making templates available to the
compiler
•
Do not separate a function template's
declaration from its definition by placing
them in separate header and implementation
files.
•
Place a function template's declaration and
its definition in the header file.
Lập trình hướng đối tượng – Templates
16
Overloading a function template
•
If a function template cannot handle all of
the instantiations that you want it to
perform, you may wish to overload it.
•
If you explicitly overload a function
template, then the overloaded function will
take precedence over the generated
function in the event of an exact match.
Lập trình hướng đối tượng – Templates
17
// Swaps two character strings
// The strings are large enough for the swap to
//occur
template<>
void swap(char* first, char* second)
{
int maxLength; // Length of longest string
// Find length of longest string
if (strlen(first) >= strlen(second))
maxLength = strlen(first);
else
maxLength = strlen(second);
Example 2
Lập trình hướng đối tượng – Templates
18
// Allocate memory for temporary string
char* temporary = new char [maxLength + 1];
assert(temporary != 0);
// Exchange strings
strcpy(temporary, first);
strcpy(first, second);
strcpy(second, temporary);
// Delete temporary string
delete [ ] temporary;
}
Example 2 2
Lập trình hướng đối tượng – Templates
19
Class templates
•
A class template is a pattern for creating
a family of similar classes.
•
If you need two or more classes with
identical members that differ only in
their parameter types, you can write a
class template that the compiler will use
to generate the definitions of the actual
classes.
Lập trình hướng đối tượng – Templates
20
Class templates
•
A class template has the general form
template <class T, >
class ClassName
{
//
};
•
The keyword template indicates that what
follows is a class template, not an actual class.
Lập trình hướng đối tượng – Templates
21
Class templates
•
The notation <class T, > is the template
parameter list.
•
The keyword class indicates that T is
generic type i.e. a placeholder for a data
type used by the class.
•
The identifier T appears throughout the
class definition wherever this type needs
to be written.
Lập trình hướng đối tượng – Templates
22
Generated classes
•
When an object of a class template is
declared, the compiler instantiates the
template, creating what is known as a
generated class.
•
The declaration of a generated class has the
general form
className<typeName, > objectName;
Lập trình hướng đối tượng – Templates
23
Member functions of a generated class
•
The member functions of a class template
are function templates, and must be
written as such.
•
Each member function definition that
appears outside the class definition must
begin with the prefix
template <class T, >
Lập trình hướng đối tượng – Templates
24
Member functions of a generated class
•
In addition, the member function's name
must be qualified with the parameterised
class name
ClassName<T, >
followed by the scope resolution operator.
Lập trình hướng đối tượng – Templates
25
// Header file for the Stack class,
stack.h
#include <iostream>
const int size = 100;
template <class T>
class Stack
{
private:
T array[size];
int numItems;
Example 3
(stack.h)