Home | API | MFC | C++ | C | Previous | Next

Programming With C++

C++ Inheritance

Inheritance allows one class to incorporate another class into its declaration. In a situation where two or more classes might have similar attributes, inheritance allows this sharing of common hierarchical functionality. This is usually achieved by creating a base class containing all the common attributes and allowing the derived class to inherit these shared attributes in addition to defining its own unique attributes.

When defining a new derived class a colon is placed after the derived class name followed by the level of access ( public, protected, or private ) and then the name of the base class.

In the worked example below two derived classes: company and personnel, are derived from the base class company. Instantiating the derived class results in the instantiation of the base class -

#include <iostream>
using namespace std;
class company//define base class
{
public: company()
{
cout << "base class" << endl;
}
};
class personnel: public company//define inherited class personnel
{
public: personnel()
{
cout << "derived class personnel" << endl;
}
};
class sales: public company//define inherited sales
{
public: sales()
{
cout << "derived class sales" << endl;
}
};
int main()
{
sales sales_y1;//instantiate inherited class sales
personnel personnel_y1;//instantiate inherited class personnel
return 0; }

Public, Protected and Private Inheritance

When one class inherits another, the members of the base class become members of the derived class. These base class members can only be accessed from within the derived class if the access specifier allows them to be inherited from the base class. The type of inheritance is specified by the access-specifier which may be public, protected or private

Multiple Inheritance

A C++ class can inherit members from more than one class with each base class being separated in the derived class declaration by a comma -

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

Passing Parameters during Base Class Initialisation

If a base class contains an overloaded constructor that requires arguments at the time of instantiation then the base class can be initialised by invoking the appropriate base class constructor via the constructor of the derived class-

class Base
{
public:
Base(int value) // overloaded constructor
{
}
};
Class Derived: public Base
{
public:
Derived(): Base(5) // instantiate Base with argument 5
{
// derived class constructor code
}
}

Derived Class Overriding Base Class’s Methods

When a derived class creates a member function with the same return type and signature as a member function in the base class it is said to be overriding that function.  To invoke the Overridden Methods of a Base Class use the scope resolution operator ( :: ) as below

derivedobject.base::method();

In the example code below the base class in instantiated from the derived class and the base class method, same is then called from the derived class

#include <iostream>
using namespace std; class base //define base class
{
public: base(int a)
{
cout << "base class" << "value a is "<< a << endl;
}
public:
void same()
{
cout << "base shared" << endl;
}
};
class derived: public base //define inherited class personnel
{
public: derived():base(2) //initiate base call with value 2
{
cout << "derived class personnel" << endl;
}
//base():
void getsame()
{
base::same();
}
void same()
{
cout << "derived shared" << endl;
}
};
int main()
{
base base1(1);//instantiate base class
derived derived1;//instantiate derived class derived1.same();//call method same from derived class
derived1.base::same();//call base method same from derived class
return 0; }

Avoiding Inheritance Using final

In C++11 the ability to prevent inheriting from a class or to prevent the overriding of a single method is done with the special identifier final. Any attempts to derive a class from a base class marked as final will result in a compiler error - 

class Base final// no class can be derived from class Base
{
method();
};

class Base {
virtual void method() final;// no class can override method()
};

Override

The override identifier makes the compiler check the base class(es) to see if there is an equivalent virtual function to that in the derived class. If there is not, the compiler will indicate an error.

class Base
{
virtual void method(int);
};
class Derived : public Base
{
virtual void method(float) override; // This will produce an error
};

Constructors and Destructors

In C++ more than one constructor is called when an object of a derived class is created. When any derived class is instantiated the constructor and deconstructor of the base call will also be called.  Base class objects are instantiated before the derived class. The sequence of destruction is the opposite to that of construction, with the derived class object going out of scope before the base class.


The Basics | Variables and Constants | Arrays | C-strings | Expressions and Operators | Controlling Program Flow | C++ Functions | Pointers and References | Memory Map and Free Store | Smart Pointers | Classes | Structures | Inheritance | Polymorphism | Templates | The Standard Template Library | The STL String Class | Namespace | Type Conversions | Input and Output Streams | The C++ Preprocessor | Exception Handling

Last Updated: 15 September 2022