File: derivationtypes.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 (47 lines) | stat: -rw-r--r-- 2,601 bytes parent folder | download | duplicates (4)
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
With inheritance public derivation is frequently used. When public derivation
is used the access rights of the base class's interface remains unaltered in
the derived class. But the type of inheritance may also be defined as
em(private) or em(protected).


Protected derivation is used when the keyword tt(protected) is put in front of
the derived class's base class:
        verb(    class Derived: protected Base)

When protected derivation is used all the base class's public and
protected members become protected members in the derived class. The derived
class may access all the base class's public and protected members. Classes
that are in turn derived from the derived class view the base class's members
as protected. Any other code (outside of the inheritance tree) is unable to
access the base class's members.

Private derivation is used when the keyword tt(private) is put in front of the
derived class's base class:
        verb(    class Derived: private Base)

When private derivation is used all the base class's members turn into
private members in the derived class. The derived class members may access
all base class public and protected members but base class members cannot be
used elsewhere.

    Public derivation should be used to define an em(is-a) relationship
between a derived class and a base class: the derived class object em(is-a)
base class object allowing the derived class object to be used polymorphically
as a base class object in code expecting a base class object. Private
inheritance is used in situations where a derived class object is defined
in-terms-of the base class where composition cannot be used. There's little
documented use for protected inheritance, but one could maybe encounter
protected inheritance when defining a base class that is itself a derived
class making its base class members available to classes derived from it.

    Combinations of inheritance types do occur. For example, when designing a
stream-class it is usually derived from tt(std::istream) or
tt(std::ostream). However, before a stream can be constructed, a
tt(std::streambuf) must be available. Taking advantage of the fact that the
inheritance order is defined in the class interface, we use multiple
inheritance (see section ref(MULTIPLE)) to derive the class from both
tt(std::streambuf) and (then) from tt(std::ostream). To the class's users it
is a tt(std::ostream) and not a tt(std::streambuf). So private derivation is
used for the latter, and public derivation for the former class:
        verb(    class Derived: private std::streambuf, public std::ostream)