You are here: TWiki > CPP Web > TableOfContents > ChapterFiveExercises r1 - 11 Mar 2002 - 05:08 - TWikiGuest


Start of topic | Skip to actions

Chapter 5: Exercises

(1) Inheritance. Consider the drawing program example again.

(1.1) Define class Rectangle by inheriting from class Point. The point should indicate the upper left corner of the rectangle. What are your class attributes? What additional methods do you introduce?

(1.2) All current examples are based on a two-dimensional view. You now want to introduce 3D objects such as spheres, cubes or cuboids. Design a class Sphere by using a class 3D-Point. Specify the role of the point in a sphere. What relationship do you use between class Point and 3D-Point?

(1.3) What functionality does move() provide for 3D objects? Be as precise as you can.

(1.4) Draw the inheritance graph including the following classes DrawableObject, Point, Circle, Rectangle, 3D-Point and Sphere.

(1.5) Have a look at the inheritance graph below:

Figure 5.8: Alternative inheritance graph
for class Sphere.

A corresponding definition might look like this:

  class Sphere inherits from Circle {
  attributes:
    int z        /* Add third dimension */

  methods:
    setZ(int newZ)
    getZ()
  }

Give reasons for advantages/disadvantages of this alternative.

(2) Multiple inheritance. Compare the inheritance graph shown in the following figure which has been shown earlier in this tutorial:

Figure 5.7: A name conflict introduced by a shared superclass
of superclasses used with multiple inheritance.

with the following figure. Here, we illustrate that B and C have each their own copy of A.

Figure 5.9: Illustration of the second multiple inheritance semantics.

What naming conflicts can occur? Try to define cases by playing with simple example classes.


%HELPEDITING%

Comments

Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r1 | More topic actions
This page is part of the GNA C++ Web.
Copyright © 2002 Globewide Network Academy. All material on this web is subject to the GNU rel="nofollow" Free Documentation License . We encourage you to copy the material on this site.
Ideas, requests, problems regarding TWiki? Send feedback.