Purpose And Use Of Inheritance

 < Day Day Up > 



Inheritance allows you to adopt or extend the behavior of an existing class or set of classes. Creating new classes via inheritance offers many benefits. First, and perhaps primarily, any class that inherits the behavior of another class is said to implement an “is a...” relationship. For instance, if class B inherits behavior from class A then a class B object is also a class A object.

Another benefit of inheritance is code reuse. When designing with inheritance, base classes should declare or define behavior common to all subclasses. If you find yourself repeating code in a set of subclasses that share a common base class you should migrate that code up the inheritance hierarchy and put it in the base class in which it belongs.

The most powerful benefit you gain from using inheritance is the ability to define abstract base classes and defer implementation of their pure virtual functions to their subclasses. Designing with abstract base classes allows you to define a stable application architecture in terms of class interfaces and the behavior they declare. Given a well- designed application architecture, new application features can be added by extending the architecture through inheritance rather than modifying the architecture itself. This is the idea behind an advanced object-oriented design principle known as the open-closed principle (OCP). (open for extension, closed for modification)

Understanding how to implement and use inheritance prepares you for learning two other advanced object-oriented design principles: Liskov substitution principle (LSP) and dependency inversion principle (DIP). Chapter 19 formally presents all three of these design principles.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

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