File: notvirtual.yo

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (75 lines) | stat: -rw-r--r-- 2,793 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
71
72
73
74
75
Virtual inheritance can be used to merge multiply occurring base
classes. However, situations may be encountered where multiple occurrences of
base classes is appropriate. Consider the definition of a tt(Truck) (cf.
section ref(Truck)):
        verb(    class Truck: public Car
    {
        int d_trailer_mass;

        public:
            Truck();
            Truck(int engine_mass, int sp, char const *nm,
                   int trailer_mass);

            void setMass(int engine_mass, int trailer_mass);
            int mass() const;
    };
    Truck::Truck(int engine_mass, int sp, char const *nm,
                  int trailer_mass)
    :
        Car(engine_mass, sp, nm)
    {
        d_trailer_mass = trailer_mass;
    }
    int Truck::mass() const
    {
        return                  // sum of:
            Car::mass() +    //   engine part plus
            trailer_mass;         //   the trailer
    })

This definition shows how a tt(Truck) object is constructed to contain two
mass fields: one via its derivation from tt(Car) and one via its own tt(int
d_trailer_mass) data member. Such a definition is of course valid, but it
could also be rewritten. We could derive a tt(Truck) from a tt(Car)
em(and) from a tt(Vehicle), thereby explicitly requesting the double presence
of a tt(Vehicle); one for the mass of the engine and cabin, and one for the
mass of the trailer. A slight complication is that a class organization like
        verb(    class Truck: public Car, public Vehicle)

is not accepted by the bf(C++) compiler. As a tt(Vehicle) is already part
of a tt(Car), it is therefore not needed once again. This organization may,
however be forced using a small trick. By creating an additional class
inheriting from tt(Vehicle) and deriving tt(Truck) from that additional class
rather than directly from tt(Vehicle) the problem is solved. Simply derive a
class tt(TrailerVeh) from tt(Vehicle), and then tt(Truck) from tt(Car) and
tt(TrailerVeh):
        verb(    class TrailerVeh: public Vehicle
    {
        public:
            TrailerVeh(int mass)
            :
                Vehicle(mass)
            {}
    };
    class Truck: public Car, public TrailerVeh
    {
        public:
            Truck();
            Truck(int engine_mass, int sp, char const *nm, int trailer_mass);
            void setMass(int engine_mass, int trailer_mass);
            int mass() const;
    };
    inline Truck::Truck(int engine_mass, int sp, char const *nm,
                        int trailer_mass)
    :
        Car(engine_mass, sp, nm),
        TrailerVeh(trailer_mass)
    {}
    inline int Truck::mass() const
    {
        return                      // sum of:
            Car::mass() +        //   engine part plus
            TrailerVeh::mass();   //   the trailer
    })