~ubuntu-branches/ubuntu/trusty/c++-annotations/trusty

« back to all changes in this revision

Viewing changes to yo/polymorphism/notvirtual.yo

  • Committer: Package Import Robot
  • Author(s): tony mancill, Frank B. Brokken, tony mancill
  • Date: 2014-01-18 08:55:27 UTC
  • mfrom: (1.1.26)
  • Revision ID: package-import@ubuntu.com-20140118085527-ph5upqqn423cmnmz
Tags: 9.8.0-1
[ Frank B. Brokken ]
* New upstream release, adds a section about static polymorphism, removes
  the concrete/a2x section, and removes C++11 indicators from the section
  headers. 

[ tony mancill ]
* Use debhelper 9.
* Tweak build-deps for g++ to use >= 4.8.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
base classes is appropriate. Consider the definition of a tt(Truck) (cf.
4
4
section ref(Truck)):
5
5
        verb(
6
 
    class Truck: public Auto
 
6
    class Truck: public Car
7
7
    {
8
8
        int d_trailer_mass;
9
9
 
18
18
    Truck::Truck(int engine_mass, int sp, char const *nm,
19
19
                  int trailer_mass)
20
20
    :
21
 
        Auto(engine_mass, sp, nm)
 
21
        Car(engine_mass, sp, nm)
22
22
    {
23
23
        d_trailer_mass = trailer_mass;
24
24
    }
25
25
    int Truck::mass() const
26
26
    {
27
27
        return                  // sum of:
28
 
            Auto::mass() +    //   engine part plus
 
28
            Car::mass() +    //   engine part plus
29
29
            trailer_mass;         //   the trailer
30
30
    }
31
31
        )
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
39
39
        verb(
40
 
    class Truck: public Auto, public Vehicle
 
40
    class Truck: public Car, public Vehicle
41
41
        )
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
48
48
tt(TrailerVeh):
49
49
        verb(
50
50
    class TrailerVeh: public Vehicle
55
55
                Vehicle(mass)
56
56
            {}
57
57
    };
58
 
    class Truck: public Auto, public TrailerVeh
 
58
    class Truck: public Car, public TrailerVeh
59
59
    {
60
60
        public:
61
61
            Truck();
66
66
    inline Truck::Truck(int engine_mass, int sp, char const *nm,
67
67
                        int trailer_mass)
68
68
    :
69
 
        Auto(engine_mass, sp, nm),
 
69
        Car(engine_mass, sp, nm),
70
70
        TrailerVeh(trailer_mass)
71
71
    {}
72
72
    inline int Truck::mass() const
73
73
    {
74
74
        return                      // sum of:
75
 
            Auto::mass() +        //   engine part plus
 
75
            Car::mass() +        //   engine part plus
76
76
            TrailerVeh::mass();   //   the trailer
77
77
    }
78
78
        )