FAQ 2.24 What are the basics of inheritance and dynamic binding?

graphics/new_icon.gif

Inheritance is a powerful tool that enables extensibility. It allows the software to capture the is-a or kind-of relationship (although as will be shown in FAQ 7.01, the phrase, "is substitutable for," more accurately captures the true meaning of inheritance).

In the following example, class Vehicle is defined with = 0; after the declaration of the startEngine() member function. This syntax means that the startEngine() member function is pure virtual and the Vehicle class is an abstract base class, or ABC. In practice, this means that Vehicle is an important class from which other classes inherit, and those other derived classes are, in general, required to provide a startEngine() member function.

 class Vehicle { public:   virtual void startEngine() = 0;   virtual ~Vehicle();                                <-- 1 }; Vehicle::~Vehicle() {   // Intentionally left blank } 

(1) Destructors of ABCs are often virtual

The idea with ABCs is to build the bulk of the application so that it knows about the ABCs but not the derived classes. For example, the following function is aware of the ABC Vehicle but is not aware of any of the derived classes.

 void f(Vehicle& v) {   // ...   v.startEngine();   // ... } 

If the ABCs are designed properly, a large percentage of the application will be written at that level. Then new derived classes can be added without impacting the bulk of the application. In other words, the goal is to minimize the ripple effect when adding new derived classes. For example, the following derived classes can be added without disturbing function f().

 #include <iostream> using namespace std; class Car : public Vehicle { public:   virtual void startEngine(); }; void Car::startEngine() {   cout << "Starting a Car's engine\n"; } class NuclearSubmarine: public Vehicle { public:   virtual void startEngine(); }; void NuclearSubmarine::startEngine() {   cout << "Starting a NuclearSubmarine's engine\n"; } 

The reason these won't disturb the code in function f() (and recall, function f() represents the bulk of the application) is because of two features of C++: the is-a conversion and dynamic binding. The is-a conversion says that an object of a derived class, such as an object of class Car, can be passed as a base reference. For example, the following objects c and s can be passed to function f(). Thus the compiler allows a conversion from a derived class (e.g., a Car object) to a base class (e.g., a Vehicle reference).

 int main() {   Car c;   NuclearSubmarine s;   f(c);   f(s); } 

The is-a conversion is always safe because inheritance means "is substitutable for." That is, a Car is substitutable for a Vehicle, so it won't surprise function f() if v is in fact referring to a Car.

Dynamic binding is the flip side of the same coin. Whereas the is-a conversion safely converts from derived class to base class, dynamic binding safely converts from base class back to derived class. For example, the line v.startEngine() in function f() actually calls the appropriate startEngine() member function associated with the object. That is, when main() passes a NuclearSubmarine into f() (line f(s) in main()), v.startEngine() calls the startEngine() member function associated with class NuclearSubmarine. This is extremely powerful, since class NuclearSubmarine might have been written long after function f() was written and compiled and put into a library. In other words, dynamic binding allows old code (f()) to call new code (NuclearSubmarine::startEngine()) without the old code needing to be modified or even recompiled. This is the essence of extensibility: the ability to add new features to an application without significant impact to existing code. It is doable with C++, but only when the design considerations are carefully thought through ahead of time; it does not come free.

UML uses the following notation to show inheritance.

graphics/02fig04.gif



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net