Chapter 9 Object-Oriented Programming: Inheritance
Introduces one of the most fundamental capabilities of object-oriented programming languages
. Inheritance is a form of software reusability in which programmers create classes that absorb an existing class's data and behaviors and enhance them with new capabilities. The chapter discusses the notions of base classes and derived classes, protected
inheritance, direct base classes, indirect base classes, constructors and destructors in base classes and derived classes, and software engineering with inheritance. The chapter compares inheritance ("is a" relationships) with composition ("has a" relationships) and introduces "uses-a" and "knows-a" relationships. A feature of the chapter is the example that implements a point, circle, cylinder class hierarchy. Using this "mechanical" example, we examine the relationship between base classes and derived classes, then show how derived classes use inherited data members and member functions. In the "Thinking About Objects" section, we update the elevator simulation design and implementation to incorporate inheritance. We also suggest further modifications that the student may design and implement.
Chapter 10 Object-Oriented Programming: Polymorphism
Deals with another fundamental capability of object-oriented programming, namely polymorphic behavior. When many classes are related to a common base class through inheritance, each derived-class object may be treated as a base-class object. This enables programs to be written in a general manner independent of the specific types of the derived-class objects. New kinds of objects can be handled by the same program, thus making systems
more extensible. Polymorphism enables programs to eliminate complex switch
logic in favor of simpler "straight-line" logic. A screen manager of a video game, for example, can send a draw message to every object in a linked list of objects to be drawn. Each object knows how to draw itself. An object of a new class can be added to the program without modifying that program (as long as that new object also knows how to draw itself). This style of programming is typically used to implement today's popular graphical user interfaces
(GUIs). The chapter discusses the mechanics of achieving polymorphic behavior via virtual
functions. It distinguishes between abstract classes (from which objects cannot be instantiated) and concrete classes (from which objects can be instantiated). Abstract classes are useful for providing an inheritable interface to classes throughout the hierarchy. We demonstrate abstract classes and polymorphic behavior by revisiting the point, circle, cylinder hierarchy of Chapter . We introduce an abstract Shape
base class, from which class Point
inherits directly and classes Circle
inherit indirectly. In response to this hierarchy, our professional audiences insisted that we provide a deeper explanation that shows precisely how polymorphism is implemented in C++, and hence, precisely what execution time and memory "costs" are incurred when programming with this powerful capability. We responded by developing an illustration and a precision explanation of the vtables
function tables) that the C++ compiler builds automatically to support polymorphism. To conclude Chapter , we introduce run-time type information (RTTI) and dynamic casting, which enable a program to determine an object's type at execution time, then act on that object accordingly. We show this in the context of a more "natural" inheritance hierarchy—several classes derived from an abstract Employee
base class, in which each employee has a common earnings
function to calculate an employee's weekly pay. Using RTTI and dynamic casting, we give a 10% pay increase to employees of a specific type, then calculate the earnings for such employees. For all other employee types, we calculate their earnings.
Chapter 11 Templates
Discusses one of the more recent additions to C++. Function templates were introduced in Chapter . Chapter presents an additional function template example. Class templates enable the programmer
to capture the essence of an abstract data type (such as a stack, an array, or a queue) and create—with minimal additional code—versions of that ADT for particular types (such as a queue of int
, a queue of float
, a queue of strings, etc.) and to provide specific type information as a parameter when creating an instance of that ADT. For this reason, class templates often are called parameterized types. The chapter discusses using type parameters and nontype parameters and considers the interaction among templates and other C++ concepts, such as inheritance, friends
members. The exercises challenge the student to write a variety of function templates and class templates and to employ these in complete programs. We greatly enhance the treatment of templates in our discussion of the Standard Template Library (STL) containers, iterators and algorithms in Chapter .
Chapter 12 C++ Stream Input/Output
Contains a comprehensive treatment of standard C++ object-oriented
input/output. The chapter discusses the various I/O capabilities of C++, including output with the stream insertion operator, input with the stream-extraction operator, type-safe I/O, formatted I/O, unformatted I/O (for performance), stream manipulators for controlling the numeric base (decimal, octal, or hexadecimal), floating-point-number formatting, controlling field widths, user-defined manipulators, stream format states, stream error states, I/O of objects of user-defined types and tying output streams to input streams (to ensure that prompts appear before the user is expected to enter responses).
| 3 | 4