next up previous contents
Next: Templates Up: Modularization, inheritance, templates, and Previous: Introduction   Contents

Links, Simlinks, and Inheritance

It makes sense to separate the graph functionality that will be used by several modules from the graph functionality that is used by a single module only. The mechanism to do this is inheritance. For example


{}
class Link {
private: 
    Id id_ ;
public:
    void set_id( Id val ) { id_ = val ; }
    Id   id() { return id_ ; }
private:
    Len len_ ;
public:
    void set_length( Len val ) { len_ = val ; }
    Len  length() { return len_ ;}
    ...
} ;

class SimLink : public Link {
private:
    Cells cells_ ;
public:
    void build() ;
    void addVehToLink( Veh* veh ) ;
    ....
}
This means that SimLink can do everything that Link can do, plus additional things. For example:


{}
...
Link* link ;
SimLink* simLink ;
...
cout << link->id() ; // o.k.
cout << simLink->id() ; // o.k., simlink is a link
link->build() ; // not o.k., link is not a simlink
simLink->build() ; // o.k.
The word public in class SimLink : public Link means that everything that was public in Link will be available for SimLink. For the purposes of these things, SimLink will behave exactly as Link.

This is the only type of inheritance that we will consider.



2004-02-02