Software reuse reduces program development time and cost.
Inheritance is a form of software reuse in which the programmer creates a class that absorbs an existing class's data and behaviors and enhances them with new capabilities. The existing class is called the base class, and the new class is referred to as the derived class.
A direct base class is the one from which a derived class explicitly inherits (specified by the class name to the right of the : in the first line of a class definition). An indirect base class is inherited from two or more levels up in the class hierarchy.
With single inheritance, a class is derived from one base class. With multiple inheritance, a class inherits from multiple (possibly unrelated) base classes.
A derived class represents a more specialized group of objects. Typically, a derived class contains behaviors inherited from its base class plus additional behaviors. A derived class can also customize behaviors inherited from the base class.
Every object of a derived class is also an object of that class's base class. However, a base-class object is not an object of that class's derived classes.
The is-a relationship represents inheritance. In an is-a relationship, an object of a derived class also can be treated as an object of its base class.
The has-a relationship represents composition. In a has-a relationship, an object contains one or more objects of other classes as members, but does not disclose their behavior directly in its interface.
A derived class cannot access the private members of its base class directly; allowing this would violate the encapsulation of the base class. A derived class can, however, access the public and protected members of its base class directly.
A derived class can effect state changes in private base-class members, but only through non-private member functions provided in the base class and inherited into the derived class.
When a base-class member function is inappropriate for a derived class, that member function can be redefined in the derived class with an appropriate implementation.
Single-inheritance relationships form treelike hierarchical structuresa base class exists in a hierarchical relationship with its derived classes.
It is possible to treat base-class objects and derived-class objects similarly; the commonality shared between the object types is expressed in the data members and member functions of the base class.
A base class's public members are accessible anywhere that the program has a handle to an object of that base class or to an object of one of that base class's derived classesor, when using the binary scope resolution operator, whenever the class's name is in scope.
A base class's private members are accessible only within the definition of that base class or from friends of that class.
A base class's protected members have an intermediate level of protection between public and private access. A base class's protected members can be accessed by members and friends of that base class and by members and friends of any classes derived from that base class.
Unfortunately, protected data members often present two major problems. First, the derivedclass object does not have to use a set function to change the value of the base-class's protected data. Second, derived-class member functions are more likely to depend on base-class implementation details.
When a derived-class member function redefines a base-class member function, the base-class member function can be accessed from the derived class by qualifying the base-class member function name with the base-class name and the binary scope resolution operator (::).
When an object of a derived class is instantiated, the base class's constructor is called immediately (either explicitly or implicitly) to initialize the base-class data members in the derived-class object (before the derived-class data members are initialized).
Declaring data members private, while providing non-private member functions to manipulate and perform validation checking on this data, enforces good software engineering.
When a derived-class object is destroyed, the destructors are called in the reverse order of the constructorsfirst the derived-class destructor is called, then the base-class destructor is called.
When deriving a class from a base class, the base class may be declared as either public, protected or private.
When deriving a class from a public base class, public members of the base class become public members of the derived class, and protected members of the base class become protected members of the derived class.
When deriving a class from a protected base class, public and protected members of the base class become protected members of the derived class.
When deriving a class from a private base class, public and protected members of the base class become private members of the derived class.