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
Lập trình hướng đối tượng – Templates
7
Function templates (I)
•
The keyword indicates that
what follows is a function template, not an
actual function.
•
The notation is the
template parameter list.
Lập trình hướng đối tượng – Templates
8
Function templates (II)
•
The keyword indicates that is
a generic type i.e. a placeholder for a
data type used by the function.
•
The identifier 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 , 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
!"#"
$#%
#$%
$%
Example 1
Lập trình hướng đối tượng – Templates
11
&$'$(%
$)*+%
,$+-%
!&%
!,%
.%
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.
$''%
/$((%
!,/%
•
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.
$0%
,$11%
!%2
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.
345
$3.4%
$.%$56(%77
34$37'4%
356'4$%
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
8!!9
999
!
!:#:
&9%99
9
;999
#$
&9$#%
&9$%
Example 2
Lập trình hướng đối tượng – Templates
18
*9
:$!
3&97'4%
<$.%
2&99
#%
#%
%
=9
34%
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
>
%
•
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 is the
template parameter list.
•
The keyword indicates that is
generic type i.e. a placeholder for a data
type used by the class.
•
The identifier 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
,/%
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
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
>
followed by the scope resolution operator.
Lập trình hướng đối tượng – Templates
25
?#8@
@
A
5$' %
8@
B
354%
C%
Example 3
(stack.h)