18
18
Truck::Truck(int engine_mass, int sp, char const *nm,
21
Auto(engine_mass, sp, nm)
21
Car(engine_mass, sp, nm)
23
23
d_trailer_mass = trailer_mass;
25
25
int Truck::mass() const
28
Auto::mass() + // engine part plus
28
Car::mass() + // engine part plus
29
29
trailer_mass; // the trailer
32
32
This definition shows how a tt(Truck) object is constructed to contain two
33
mass fields: one via its derivation from tt(Auto) and one via its own tt(int
33
mass fields: one via its derivation from tt(Car) and one via its own tt(int
34
34
d_trailer_mass) data member. Such a definition is of course valid, but it
35
could also be rewritten. We could derive a tt(Truck) from an tt(Auto)
35
could also be rewritten. We could derive a tt(Truck) from a tt(Car)
36
36
em(and) from a tt(Vehicle), thereby explicitly requesting the double presence
37
37
of a tt(Vehicle); one for the mass of the engine and cabin, and one for the
38
38
mass of the trailer. A slight complication is that a class organization like
40
class Truck: public Auto, public Vehicle
40
class Truck: public Car, public Vehicle
42
42
is not accepted by the bf(C++) compiler. As a tt(Vehicle) is already part
43
of an tt(Auto), it is therefore not needed once again. This organzation may,
43
of a tt(Car), it is therefore not needed once again. This organzation may,
44
44
however be forced using a small trick. By creating an additional class
45
45
inheriting from tt(Vehicle) and deriving tt(Truck) from that additional class
46
46
rather than directly from tt(Vehicle) the problem is solved. Simply derive a
47
class tt(TrailerVeh) from tt(Vehicle), and then tt(Truck) from tt(Auto) and
47
class tt(TrailerVeh) from tt(Vehicle), and then tt(Truck) from tt(Car) and
50
50
class TrailerVeh: public Vehicle
66
66
inline Truck::Truck(int engine_mass, int sp, char const *nm,
69
Auto(engine_mass, sp, nm),
69
Car(engine_mass, sp, nm),
70
70
TrailerVeh(trailer_mass)
72
72
inline int Truck::mass() const
75
Auto::mass() + // engine part plus
75
Car::mass() + // engine part plus
76
76
TrailerVeh::mass(); // the trailer