C++
Chapter 9 - Inheritance

The goal in this chapter is to create IS-A relationships between classes.  This type of relationship is formed through inheritance.  Take for example:  An elephant IS-An animal. A canary is an animal.  Each of these classes derives their basic date and behavior from a base class.

Most all inheritance is in the form of linear inheritance.  If the base class is called "Animal" then the following linear inheritance model can hold.  Animal <---- Elephant.  Note, in inheritance diagrams, the derived class always points back to it's parent or base class.  The second form of inheritance is multiple inheritance.  Please study this in your book.  Using multiple inheritance should be implemented from the initial design and never used as a way of joining two classes to gain some simple data members or behavior.

Specifying Inheritance

Syntax:  Use the : operator to identify inheritance on the class definition line

            class Derived : public Base

The constructor of the derived class will fire the constructor of the base class.   Although the derived class inherits from the base class and we have two constructor calls, we have only one object.  The base class becomes a subpart of the derived class and is contained within the constructed derived object.

Type of Inheritance


Linear inheritance:  Animal <--- Mammal <--- Bear

        class Animal
        {};

        class Mammal : public Animal
        {};

class Bear : public Mammal
{};

Here we see that Bear IS-A Mammal and Mammal IS-A Animal.  This is a linear chain of inheritance.

Multiple inheritance: 
              Animal <----------+
                                |------Bird
              Flying_Object <---+

         class Bird : public Animal, public Flying_Object
         {};

Here we see that a Bird IS-A Flying_Object and an Animal.  This is an example of a class having multiple  base classes or multiple inheritance.

Method of Inheritance ( see Page 535 in you book )

A derived class may inherit from the base class and change the protection of the base class members.

When a derived class inherits public from the base class. 
The base class remains unmodified within the derived class.

class Derived : public Base
Base class public remains public, Base class protected remain protected and Base class private remain private.

class Derived : protected Base
Encapsulation to the base class in increased.  All public members of the Base class now become protected members in the Derived class.  Protected members of a class are accessible down the inheritance chain.

class Derived : private Base
Encapsulation is further increased.  Public members of the base class become private and protected members of the base
class become private.

In Class Work Shop

Use the following code to create a working example of inheritance.  Perform the following
modifications.  Click here, inherit.cpp to access the file or copy it from the page.

Save the code to inherit.cpp
compile:  inherit.cpp
execute: inherit.exe

    1. Observe the order of construction and destruction
    2. Create a new animal called a Dog, derived from base class Animal

Note the use of the constructors and passing parameters into the base class.


Example Output

Constructor Base Class Animal
Constructor Elephant
Overloaded Constructor Base Class Animal
Overloaded Constructor Canary
Object = Elephant
Height = 14
Weight = 1000
State  = Big, Slow and Hungry
Object = Canary
Height = 0.55
Weight = 0.733
State  = Teeny, Tiny and PEEP!
Destructor Canary
Destructor Base Class Animal
Destructor Elephant
Destructor Base Class Animal

07/04/01 09:56 AM