Inheritance Overview

C++ implements inheritance through the mechanism of derivation. Derivation allows you to reuse code by creating new classes, called derived classes, that inherit properties from one or more existing classes, called base classes. A derived class inherits the properties, including data and function members, of its base class. You can also add new data members and member functions to the derived class. You can modify the implementation of existing member functions or data by overriding base class member functions or data in the newly derived class.

Multiple inheritance allows you to create a derived class that inherits properties from more than one base class.

Because a derived class inherits members from all its base classes, ambiguities can result. For example, if two base classes have a member with the same name, the derived class cannot implicitly differentiate between the two members. Note that, when you are using multiple inheritance, the access to names of base classes may be ambiguous.

Multiple inheritance allows you to have more than one base class for a single derived class. You can create an interconnected inheritance graph of inherited classes by using derived classes as base classes for other derived classes. You can build an inheritance graph through the process of specialization, in which derived classes are more specialized than their base classes. You can also work in the reverse direction and build an inheritance graph through generalization. If you have a number of related classes that share a group of properties, you can generalize and build a base class to embody them . The group of related classes becomes the derived classes of the new base class.

A direct base class is a base class that appears directly as a base specifier in the declaration of its derived class. A direct base class is analogous to a parent in a hierarchical graph.

An indirect base class is a base class that does not appear directly in the declaration of the derived class but is available to the derived class through one of its base classes. An indirect base class is analogous to a grandparent or great grandparent or great-great grandparent in a hierarchical graph. For a given class, all base classes that are not direct base classes are indirect base classes.

Polymorphic functions are functions that can be applied to objects of more than one type. In C++, polymorphic functions are implemented in two ways:

Typically, a base class has several derived classes, each requiring its own customized version of a particular operation. It is difficult for a base class to implement member functions that are useful for all of its derived classes. A base class would have to determine which derived class an object belonged to before it could execute the applicable code for that object. When a virtual function is called, the compiler executes the function implementation associated with the object that the function is called for. The implementation of the base class is only a default that is used when the derived class does not contain its own implementation.



Examples of Single and Multiple Inheritance


Derivation
Multiple Inheritance
Overloading Functions
Virtual Functions