Your browser doesn't support JavaScript Inheritance – Windows Programming

Inheritance

Inheritance allows one class to inherit members from another class. When two or more classes have common characteristics or functionality, inheritance allows these common members to be placed in a base class. A derived class can then inherit the members of the base class and define additional members that are specific to the derived class.

When defining a derived class, a colon is placed after the derived class name, followed by the inheritance access specifier (public, protected, or private) and the name of the base class. For example:

class Derived : public Base

In the worked example below, the calls Derived inherits from class Base. The sequence is as follows

So the sequence is:

  1. Derived dobj; creates a Derived object.
  2. C++ constructs the Base part of that object → Base().
  3. C++ then constructs the Derived part → Derived().

#include <iostream>

using namespace std;
class Base {
public:

    Base() {
        cout << "Base Constructor" << endl;
    }
};

class Derived : public Base {
public:
    Derived() {
        cout << "Derived Constructor" << endl;
    }
};

int main() {
    Derived dobj;
    return 0;
}

Public, Protected and Private Inheritance

When one class inherits from another, the derived class contains a base-class subobject and can access the inherited base-class members according to their access levels and the type of inheritance used.

The access specifiers in the base class determine which members can be accessed by the derived class. The inheritance mode (public, protected, or private) determines how the accessible base-class members are exposed through the derived class.

The three access specifiers are:

  • public – members can be accessed from outside the class, subject to the inheritance mode.
  • private – members cannot be accessed directly from outside the class or from a derived class. They can only be accessed by member functions and friends of the class that declares them.
  • protected – members cannot be accessed directly from outside the class, but they can be accessed by member functions of the class and by derived classes.

For example:

#include <iostream>
using namespace std;
class Base
{
public:
    int publicValue;
protected:
    int protectedValue;
private:
    int privateValue;
};

class Derived : public Base
{
public:

    void function()
    {
        publicValue = 10;       // accessible
        protectedValue = 20;    // accessible
        // privateValue = 30;  // NOT accessible

        cout << "Public value: " << publicValue << endl;
        cout << "Protected value: " << protectedValue << endl;
    }
};

int main()
{
    Derived obj;
    obj.function();
    obj.publicValue=20; //direct changes permitted.
    //obj.protectedValue = 20;    // Not accessible. Direct changes not permitted.
    return 0;
}

Multiple Inheritance

C++ also allows a class to inherit from more than one base class. This is known as multiple inheritance.

Each base class is separated by a comma in the derived-class declaration:

class BaseA { }; class BaseB { }; Class Derived: public BaseA,public BaseB { }

Constructors and Destructors

When an object of a derived class is created, the constructors of its base classes are also called. The base-class constructor is called before the derived-class constructor.

Base-class subobjects are constructed before the derived-class part of the object. Constructors are therefore invoked in order, starting with the most base-level class and progressing down to the most derived class.

When the object is destroyed, the destructors are called in the reverse order. The destructor of the most derived class is called first, followed by the destructors of its base classes.

The following example illustrates the order in which constructors and destructors are executed when inheritance is used. A Derived object is created. Before the Derived constructor executes, the Base constructor is automatically called. The output is therefore:

The output is therefore:

BaseA’s Constructor
BaseB’s Constructor
BaseB’s Destructor
BaseA’s Destructor

#include <iostream>
using namespace std;
class BaseA
{
    public:
        BaseA()
        {
            cout<<"BaseA's Constructor"<<endl;
        }
        ~BaseA()
        {
            cout<<"BaseA's Destructor"<<endl;
        }
};
class BaseB : BaseA
{
    public:
        BaseB()
        {
            cout<<"BaseB's Constructor"<<endl;
        }
        ~BaseB()
        {
            cout<<"BaseB's Destructor"<<endl;
        }
};

int main()
{
    BaseB b;
    return 0;
}

Passing Parameters during Base Class Initialisation

If a base class has a constructor that requires arguments, the derived class must specify which base-class constructor should be called when a derived-class object is created.

The base-class constructor is called from the member-initialiser list of the derived-class constructor.

#include <iostream>
using namespace std;
class Base
{
public:
    Base(int value)
    {
        cout << "Base constructor called with value: "
             << value << endl;
    }
};

class Derived : public Base
{
public:
    Derived() : Base(5)
    {
        cout << "Derived constructor" << endl;
    }
};

int main()
{
    Derived obj;
    return 0;
}

n this example, the Derived constructor uses the member-initialiser list:

Derived() : Base(5)

This calls the Base constructor and passes it the value 5.

When a Derived object is created:

Derived obj;

the Base constructor is called first with the argument 5. The Derived constructor then executes.

The order is therefore:

  1. The Base constructor is called with 5.
  2. The Derived constructor executes.

The base-class constructor must be called before the body of the derived-class constructor executes. If the base class has no default constructor, the derived class must explicitly call an appropriate base-class constructor in its member-initialiser list.