Chapter 9 Object-Oriented (Make web site) Programming: Inheritance 381 ANSWERS TO
Chapter 9 Object-Oriented Programming: Inheritance 381 ANSWERS TO SELF-REVIEW EXERCISES 9.1 a) Inheritance. b) protected. c) is a or inheritance. d) has a or composition or aggregation. e) hierarchical. f) public. g) private. h) internal. i) constructor. j) base. 9.2 a) True. b) True. c) False. A has-a relationship is implemented via composition. An isa relationship is implemented via inheritance. d) False. Overridable methods must be declared explicitly as virtual. e) True. f) False. When a derived class redefines a base-class method using the same signature, the derived class overrides that base-class method. g) False. This is an example of a has a relationship. Class Carhas an is a relationship with class Vehicle. h) True. EXERCISES 9.3 Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite classes Point3, Circle4and Cylinder to use composition, rather than inheritance. After you do this, assess the relative merits of the two approaches for both the Point3, Circle4, Cylinder problem, as well as for object-oriented programs in general. Which approach is more natural, why? 9.4 Some programmers prefer not to use protected access because it breaks the encapsulation of the base class. Discuss the relative merits of using protected access vs. insisting on using private access in base classes. 9.5 Rewrite the case study in Section 9.5 as a Point, Square, Cube program. Do this two ways once via inheritance and once via composition. 9.6 Write an inheritance hierarchy for class Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateralas the base class of the hierarchy. Make the hierarchy as deep (i.e., as many levels) as possible. The private data of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects of each of the classes in your hierarchy and polymorphically outputs each object s dimensions and area. 9.7 Modify classes Point3, Circle4 and Cylinder to contain destructors. Then, modify the program of Fig. 9.19 to demonstrate the order in which constructors and destructors are invoked in this hierarchy. 9.8 Write down all the shapes you can think of both two-dimensional and three-dimensional and form those shapes into a shape hierarchy. Your hierarchy should have base class Shape from which class TwoDimensionalShape and class ThreeDimensionalShape are derived. Once you have developed the hierarchy, define each of the classes in the hierarchy. We will use this hierarchy in the exercises of Chapter 10 to process all shapes as objects of base-class Shape. (This is a technique called polymorphism.)
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.