Introduction to Pointers, Structures and Linked-Lists Part 10
Brief Introduction to Inheritance
by Chris Bennett aka Dwarfsoft


You have covered everything up to and including member functions. Well, now we take the structure example a little further and apply it to classes. So what is a class. For all you C programmers out there it is almost exactly the same as a struct. The main difference is that it classes allow for inheritance.

Inheritance is one of those dirty words that gets thrown around everywhere these days. I will up-front state that I do not use Inheritance exclusively in my projects, although sometimes it does come in handy, I use a slightly different method which I believe is much more Object Oriented, but inheritance does have its uses, which I will touch on briefly now.

Inheritance works on the principle of the "is a" relationship (I usually work on a "has a" relationship). If you were to define a class of type Mammal it might have the following properties:

class cMammal
    typedef enum _eSex {Male,Female} Sex_t;
    typedef enum _eEyes {Green, Blue, Brown} Eyes_t;

    cMammal(class cMammal Mother, class cMammal Father)
        _Mother = Mother
        _Father = Father
    SetSex(Sex_t Sex)
        _Sex = Sex;
    SetEyes(Eyes_t Eyes)
        _Eyes = Eyes;
    Sex_t _Sex;
    Eyes_t _Eyes;
    class cMammal *_Mother;
    class cMammal *_Father;

This seems to have very little relevance to anything, but I put what I could here to set up a base class. Now, if we think of narrowing our field a little more we can think up two new classes. Dog's are mammals and so are Humans. So if we want to create these classes we can create them from our base class of cMammal:

class cDog : public cMammal // Dog "is a" Mammal so cDog inherits cMammal
    SetName(String Name)
        _Name = Name;
    cDog(class cDog Mother, class cDog Father)
        _Mother = Mother;
        _Father = Father;
// Redefine _Mother and _Father to Dog's instead of Mammal's
    class cDog *_Mother;
    class cDog *_Father;
// And add a new feature
    String _Name;

In the cDog class we have to redefine _Mother and _Father so that they are current with the current class, but we also then have to redefine the constructor to be cDog(class cDog, class cDog);. Eyes and Sex do not need to be changed because they operate quite well on their own and then we also add the Name feature which allows our Dog mammal to be uniquely identified by name. Other features that cDog might have are ScratchFleas(); MarkTerritory(); Bark(); etc.

We would approach cHuman in the same kind of way giving it similar Human features. Once we have cDog we can create another specific kind of Dog as cWolf for a Wolf. This might have methods similar to Dog but specific attributes that apply only to it.

A quick peak at Multiple Inheritance

Now that we have two classes (cHuman and cWolf) that I didn't define in code, but I did brush by loosely we can create something of a hybrid. Let us think of there being a WereWolf which is part man and part wolf. It is a mammal (as both Wolves and Humans are mammals) but it takes on both characteristics of Humans and Wolves. You could assume that we just create another creature with Mammal as its base class and have its own WereWolf features, but this might skip some of the Wolf only attributes and some of the Human only attributes in translation. But we can incorporate more than one base class:

class cWereWolf : public cWolf, public cHuman
    ShotBySilverBullet() { BecomeMan(); }
    BecomeWolf() { _state = Wolf; }
    BecomeMan() { _state = Man; }
    typedef enum _eState { Wolf, Man } State_t;
    State_t _state;

So now I have given you a breif look into the way you can handle classes to inherit some attributes of other classes. Remembering this is sometimes useful, especially when coming to OO object nesting. Hope you are enjoying these articles as much as I do writing them. Good Luck.

Author: Chris Bennett aka Dwarfsoft
February 24th, 2002
© Copyright Chris Bennett, 2002

Discuss this article in the forums

Date this article was posted to 9/30/2004
(Note that this date does not necessarily correspond to the date the article was written)

See Also:

© 1999-2011 All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!