Derivation

Inheritance is implemented in C++ through the mechanism of derivation. Derivation allows you to derive a class, called a derived class , from another class, called a base class.

In the declaration of a derived class, you list the base classes of the derived class. The derived class inherits its members from these base classes. All classes that appear in the list of base classes must be previously defined classes.

Incompletely declared classes are not allowed in base lists.

For example:

class X; // incomplete declaration of class X
class Y: public X      // error
{
};

When you derive a class, the derived class inherits class members of the base class. You can refer to inherited members (base class members) as if they were members of the derived class. If you redefine base class members in the derived class, you can still refer to the base class members by using the :: (scope resolution) operator.

You can manipulate a derived class object as if it were a base class object. You can use a pointer or a reference to a derived class object in place of a pointer or reference to its base class. For example, you can pass a pointer or reference to a derived class object D to a function expecting a pointer or reference to the base class of D. You do not need to use an explicit cast to achieve this; a standard conversion is performed. You can implicitly convert a pointer to a derived class to point to a base class. You can also implicitly convert a reference to a derived class to a reference to a base class.

The reverse case is not allowed. You cannot implicitly convert a pointer or a reference to a base class object to a pointer or reference to a derived class.

If a member of a derived class and a member of a base class have the same name, the base class member is hidden in the derived class. If a member of a derived class has the same name as a base class, the base class name is hidden in the derived class. In both cases, the name of the derived class member is called the dominant name.



Inheritance Overview


Examples of Derived Classes


Syntax of a Derived Class
Scope Resolution Operator
Multiple Inheritance