FAQ 5.10 What is the purpose of inheritance?
In C++, inheritance is for subtyping. It lets developers model the
Inheritance allows developers to make one class a kind-of another class. In an inheritance relationship, the new class is called the derived class and the original class from which the new class is being derived is called the base class. All the data structures and member functions that belong to the base class automatically become part of the derived class.
For example, suppose the class Stack has member functions push() and pop() , and there is a need for a class PrintableStack . PrintableStack is exactly like Stack except PrintableStack also provides the member function print() . Class PrintableStack can be built by inheriting from Stack — Stack would be the base class and PrintableStack would be the derived class. The member functions push() and pop() and any others that belong to Stack automatically become part of PrintableStack , so the only requirement for building PrintableStack is adding the print() member function to it.
To do something similar in C, the existing
source file would be modified (which is trouble if
is being used by other source files and they rely on the exact layout of
FAQ 5.11 What are the advantages of polymorphism and dynamic binding?
They allow old code to call new code in a substitutable fashion.
The real power of object-oriented programming isn't just inheritance; it's the ability to treat objects of derived classes as if they were objects of the base class. The mechanisms that support this are polymorphism and dynamic binding.
Polymorphism allows an object of a derived class to be passed to a function that accepts a reference or a pointer to a base class. A function that receives such objects is a polymorphic function.
When a polymorphic function invokes a member function using a base class reference or pointer, dynamic binding executes the code from the derived class even though the polymorphic function may be unaware that the derived class exists. The code that is executed depends on the type of the object rather than on the type of the reference or pointer. In this way, objects of a derived class can be substituted for objects of a base class without requiring any changes in the polymorphic functions that use the objects.
FAQ 5.12 How does OO help produce flexible and extensible software?
By minimizing the ripple effect of enhancements.
Even in well-designed structured systems, enhancements often require modifications to significant portions of existing design and code. OO achieves flexibility by (1) allowing the past to make use of the future, and (2) designing for comprehensibility.
The past can make use of the future when old user code (the past) can reliably and predictably call new server code (the future) without any modifications to the old
Comprehensibility allows software to be
This approach can be contrasted with software developed using the traditional, structured approach, where enhancements or modifications often lead to a seemingly endless cycle of random modifications (a.k.a. hacks) until the system appears to work.