Exercises in this lecture  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Go to the slide, where this exercise belongs -- Keyboard shortcut: 'u'  

Exercise 3.2
Color Point Extensions ***


On this page we have seen the class ColorPoint, which inherits from Point. The classes make use of virtual methods.

In this exercise you should start with the code in this file.

  1. First take a look at the existing stuff, and make sure you understand it. Be aware that both of the classes Point and ColorPoint use virtual methods.

  2. Add a method class-of to both Point and ColorPoint that returns the class of an instance.

  3. Now repair the method add in Point, such that it always instantiate a class corresponding to the class of the receiver. In other words, if the receiver of add is a Point, instantiate a Point. If the receiver of add is a ColorPoint, instantiate a ColorPoint. You can probably use the method class-of from above. You may encounter a problem, because the constructor of point and color-point take a different number of parameters. You are welcome to default the color of a color point to your favorite color!


Solution