File: intro.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (70 lines) | stat: -rw-r--r-- 4,114 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Templates can not only be constructed for functions but also for complete
classes. Consider constructing a i(class template) when a class should be able
to handle different types of data. Class templates are frequently used in
bf(C++): chapter ref(CONTAINERS) discusses data structures like tt(vector,
stack) and tt(queue), that are implemented as em(class templates). With class
templates, the algorithms and the data on which the algorithms operate are
completely separated from each other. To use a particular data structure in
combination with a particular data type only the data type needs to be
specified when defining or declaring a class template object (as in
tt(stack<int> iStack)).

In this chapter constructing and using class templates is discussed. In a
sense, class templates compete with object oriented programming (cf. chapter
ref(POLYMORPHISM)), that uses a mechanism resembling that of
templates. Polymorphism allows the programmer to postpone the implementation
of algorithms by deriving classes from base classes in which algorithms are
only partially implemented. The actual definition and processing of the data
upon which the algorithms operate may be postponed until derived classes are
defined. Likewise, templates allow the programmer to postpone the
specification of the data upon which the algorithms operate. This is most
clearly seen with abstract containers, which completely specify the algorithms
and at the same time leave the data type on which the algorithms operate
completely unspecified. 

There exists an intriguing correspondence between the kind of polymorphism
we've encountered in chapter ref(POLYMORPHISM) and certain uses of class
templates.  In their book bf(C++ Coding Standards) (Addison-Wesley, 2005)
        hi(Sutter, H.)hi(Alexandrescu, A.)
    Sutter and Alexandrescu refer to
        emi(static polymorphism)hi(polymorphism: static) and
        hi(polymorphism: dynamic) emi(dynamic polymorphism).
    em(Dynamic) polymorphism is what we use when overriding virtual members.
Using em(vtable)s the function that is actually called depends on the type of
object a (base) class pointer points at. em(Static) polymorphism is
encountered in the context of templates, and is discussed and compared to
dynamic polymorphism in section ref(STATICPOLY).

Generally, class templates are easier to use than polymorphism. It is
certainly easier to write tt(stack<int> istack) to create a stack of tt(ints)
than to derive a new class tt(Istack: public stack) and to implement all
necessary member functions defining a similar stack of tt(ints) using object
oriented programming. On the other hand, for each different type for which an
object of a class template is defined another, possibly complete class must be
reinstantiated.  This is not required in the context of object oriented
programming where derived classes em(use), rather than em(copy), the functions
that are already available in their base classes (but see also section
ref(DERIVEDTEMPCLASS)).

Previously we've already used class templates. Objects like tt(vector<int> vi)
and tt(vector<string> vs) are commonly used. The data types for which these
templates are defined and instantiated are an inherent part of such container
types. It is stressed that it is the em(combination) of a class template type
and its template parameter(s), rather than the mere class template's type
that defines or generates a
 hi(class template: generate types) type. So tt(vector<int>) is a type as is
tt(vector<string>). Such types could very well be specified through
using-declarations: 
        verb(    using IntVector =  std::vector<int>;
    using StringVector = std::vector<std::string>;

    IntVector vi;
    StringVector vs;
        )

Like function templates, class templates, both the class interface and the
implementations of the class member functions, must be available at their
points of instantiation. Therefore the interfaces and implementations of class
templates are commonly provided in a header file, which is then included by
source files using those classes (see also section ref(NAMERESFUN)).