#ifndef _COIN_H #define _COIN_H // Coin Class // // Author: Alyce Brady // // Creation Date: 1/13/2000 // // Coin objects simulate a coin that, when tossed, has an // equal probability of turning up heads or tails. When the // coin is first constructed, both the Heads and Tails functions // will return false. Once the Toss function has been called // for the first time, however, either Heads or Tails (but not // both) will always be true. Between calls to the Toss function, // the Heads and Tails functions can be called as often as desired // and will always return the same true/false value (in other words, // to change whether the coin is showing heads or tails is to toss // the coin). // // By default, a Coin object will produce a different series of // numbers (through repeated calls to the Toss method) every time the // program is run. When testing, though, it is often useful to have a // program generate the same sequence of numbers each time it is run; // this can be achieved by specifying a "seed" when the first Coin // object in the program is created. // // // Technical Note: // The Coin class makes use of the RandGen class to generate random // numbers. The "seed" used by all random number generation in a // program is set the first time a random number generator object is // constructed by the program. All other random number generator // objects created later in the program will use the same seed. #include "randgen.h" class Coin { public: // Constructors // The default Coin constructor constructs a default RandGen // object; the second Coin constructor constructs a RandGen // object with a seed. // // If the first RandGen object in a program is constructed with // the default constructor, a different series of numbers is // produced every time the program is run. If the first RandGen // object is constructed with a seed, the same series of numbers // is produced every time. Coin(); // default constructor Coin(int seed); // produce same series every time // (most useful during testing) // Accessing functions bool Heads(); // precondition: coin has been tossed at least once // postcondition: returns true if last toss turned up heads bool Tails(); // precondition: coin has been tossed at least once // postcondition: returns true if last toss turned up tails // Modifying functions void Toss(); // postcondition: 50/50 chance that Heads is true private: RandGen randNumGen; // the random number generator bool tossed; // false until first call to Toss; // true from then on bool showingHeads; // true if the coin is showing heads }; #endif // _COIN_H not defined