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
A corresponding definition might look like this:
for class Sphere.
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
with the following figure. Here, we illustrate that B and C have each their own copy of A.
of superclasses used with multiple inheritance.
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 | |||||