CSE2305 - Object-Oriented Software Engineering
Week 5

Topic 9: C++ Inheritance


Synopsis


Inheritance in C++


An example

class Name
{
public:
        Name(void) { myName = 0; }
        ~Name(void) { delete[] myName; }

        void SetName(char* n)
        {
                myName = new char[strlen(n)+1];
                strcpy(myName,n);
        }

        void Print(void) { cout << myName << endl; }

private:
        char* myName;
};

class Contact: public Name
{
public:
        Contact(void) { myAddress = 0; }

        ~Contact(void) { delete[] myAddress; }

        void SetAddress(char* c)
        {
                myAddress = new char[strlen(c)+1];
                strcpy(myAddress,c);
        }

        void Print(void)
        {
                Name::Print();
                cout << myAddress << endl;
        }

private:
        char* myAddress;
};

int main(void)
{
        Contact c;

        c.SetName("John McClane");
        c.SetAddress("137th floor, Nakatome Towers");

        c.Print();
}

What a derived class¤¤ inherits


What a derived class¤¤ doesn't inherit


What a derived class¤¤ can add


What happens when a derived-class¤ object is created and destroyed

  1. Space is allocated (on the stack or the heap) for the full object (that is, enough space to store the data members¤¤ inherited from the base class¤¤ plus the data members¤¤ defined in the derived class¤¤ itself)
     
  2. The base class¤¤'s constructor¤ is called to initialize the data members¤¤ inherited from the base class¤¤
     
  3. The derived class¤¤'s constructor¤ is then called to initialize the data members¤¤ added in the derived class¤¤
     
  4. The derived-class¤ object is then usable
     
  5. When the object is destroyed (goes out of scope or is deleted) the derived class¤¤'s destructor¤ is called on the object first
     
  6. Then the base class¤¤'s destructor¤ is called on the object
     
  7. Finally the allocated space for the full object is reclaimed
     

Public vs private inheritance¤


Another example

class Setter
{
public:
        void Set(char*& variable, char* value)
        {
                variable = new char[strlen(value)+1];
                strcpy(variable,value);
        }
};

class Name: private Setter
{
public:
        Name(void)  { myName = 0; }
        ~Name(void) { delete[] myName; }

        void SetName(char* n)
        { Set(myName,n); }

        void Print(void)
        { cout << myName << endl; }

private:
        char* myName;
};

class Contact: public Name
{
public:
        Contact(void)  { myAddress = 0; }
        ~Contact(void) { delete[] myAddress; }

        void SetAddress(char* c)
        { Set(myAddress,c); }   // ERROR! WHY?

        void Print(void)
        {
                Name::Print();
                cout << myAddress << endl;
        }

private:
        char* myAddress;
};

int main(void)
{
        Contact c;

        c.SetName("John McClane");
        c.SetAddress("137th floor, Nakatome Towers");

        c.Print();
}

Protected access


Initializer lists¤


Reading


This material is part of the CSE2305 - Object-Oriented Software Engineering course.
Copyright © Jon McCormack & Damian Conway, 1998–2005. All rights reserved.