One of the
key features of C++ classes, and certainly one of the most useful for
WCM, is inheritance
. Inheritance allows creating classes which are derived from other
classes, so that they automatically include its "parent's" member
functions, character definitions and decals, plus its own, which may
overwrite or supersede the definitions in the parent class. We will
examine ways to do this in the next few sections, but first we will
look at modifying the example presented in the previous section.
For example if we want to create an advanced
callout balloon class based on implemented in previous section (to
support scaling for example) we can do this by deriving a new class
// see the notes after this example for an
explainatin of the ":" notation
class CalloutBalloonEx : public CalloutBalloon
means that we declare a new sub-class
which is derived from
. Deriving means that all member functions and character definitions of
will be accessible the same way in
inherited, however! We need to create a new constructor! Line
CalloutBalloonEx ( string sText ) : CalloutBalloon ( sText )
declares a new constructor for class
with one string parameter sText. Before we add any additional
initialization, however, we want to call the constructor of inherited
by calling it with parameter
. In this case,
does not require any additional initialization so it has an empty body.
But there are more complex examples possible where additional
initialization is required.
It is possible to add new member functions to the new sib-class. For
is a new member function.
It is also possible to overwrite the member functions of the parent
was already declared in
but here we define it to behave a little differently in
in order to take into account the current scale factor.
The new sub-class can be
used in the same way as the parent class: