Exercises
Exercise 8.1: The importance of being virtual
If you don't explicitly label a method as virtual in the base class, then the child class overrides will ignore it. The Pop Framework has deliberately made move
a non-virtual method, because it would be a bad idea for you to override it, like tinkering with the delicate innards of a watch. Even if you were to write a move
method inside one of the child classes, your code would be ignored (unless you were to put virtual in front of the move prototype in critter.h
).
Let's see what happens if we make update
non-virtual. This will mean that all of the critters' specialized update methods will be ignored and they'll just use the base class update method, which in fact does very little.
Open up the Pop project file and remove the world 'virtual' from the line virtual void update(CPopView *pactiveview, Real dt) at the bottom of the critter.h
file. Build and run. You'll find that you're no longer able to shoot bullets in the Spacewar game. This is because the shooting behavior is part of the cArmedCritter::update
code, which is now not being used.
Exercise 8.2: Tweaking the evolution process
Try changing the default _mutationstrength
value, and make some changes to the cPolygon
and cPolyPolygon
mutate methods as well. See what kinds of interesting polypolygons you can come up with.
|