|
Exercises
See solutions.
- Design a program that calculates all the prime numbers up to 100 by eliminating all the numbers divisible by 2, 3, 5 and 7. Create an abstract class Sieve with one pure virtual method NextNumber. Implement a SourceSieve that simply iterates over all numbers from 1 to 100 in order. Implement Sieve2 that takes a reference to a Sieve in its constructor, and when asked to give the next number, keeps retrieving numbers from its child sieve until it finds one that is not divisible by 2 and returns it. Do the same for 3, 5 and 7. Create all these Sieves as local objects in main, chain them, and print all the numbers returned by the top Sieve. What order of Sieves is best?
- Create an abstract class Command with two pure virtual methods Execute and Undo. Create command classes for our stack-based calculator, corresponding to all possible calculator inputs. For every user input construct a corresponding Command object, Execute it and push it on a two-deep stack of Command s. Add the ‘u’ (undo ) command to the calculator. The u-command pops the last Command from the command stack and calls its virtual method Undo.
|