/******************************* INHERIT.CPP **********************************
                                          Example showing inheritance in a derived class.                
                                                                                                                       
* The class TRandomGenerator defines a random number generator.              
* The class TLottoGenerator, derived from TRandomGenerator, generates a list
* of DIFFERENT random numbers. You cannot make such a list by simply calling
* the random number generator repeatedly, because in this way you cannot be  
* sure that the numbers are different.                                                                                                                   
* (Better random number generators can be found at                          
*  www.announce.com/agner/random )                                          
******************************************************************************/

#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <time.h>

// Define random number generator class
class TRandomGenerator {
   public: // public members are accessible from outside the class
   TRandomGenerator(void);  // constructor
   void Initialize(double seed); // initialize random sequence
   double FRandom(void);  // random floating point number between 0 and 1
   int IRandom(void); // random integer number within defined interval
   void SetInterval(int min, int max); // set interval for IRandom
   private: // private members are accessible only in this class
   double ran;  // last random number
   protected: // protected members are accessible in this class and derived classes
   int rmin, rmax; // interval for IRandom
};

// Constructor
TRandomGenerator::TRandomGenerator() {
   Initialize(time(0));} // initialize to random time

// Initialize sequence.
// Call this function with any number for seed to start a new sequence of
// random numbers.
void TRandomGenerator::Initialize(double seed) {
   rmin=0; rmax=1;
   ran = seed * M_PI;        // get some funny decimals
   FRandom(); FRandom();}    // advance twice

// Get a random floating point number between 0 and 1
double TRandomGenerator::FRandom(void) {
   ran = fmod(ran*9821.+0.211327, 1.);
   return ran;}

// Get a random integer number in interval set by SetInterval
int TRandomGenerator::IRandom(void) {
   int ir = rmin + FRandom()*(rmax-rmin+1);
   if (ir > rmax) ir = rmax;
   return ir;}

// set interval for IRandom
void TRandomGenerator::SetInterval(int min, int max) {
   rmin = min;  rmax = max;}


// make class TLottoGenerator derived from TRandomGenerator:
class TLottoGenerator : public TRandomGenerator {
   public:
   int * MakeRandomList (int length);
};

// this member function makes a list of n different random numbers
int * TLottoGenerator::MakeRandomList (int n) {
   int savemin, savemax, length, temp, i, j;
   int * allnumbers, * list;
   // save minimum and maximum:
   savemin = rmin;  savemax = rmax;
   // length = number of all possible numbers:
   length = rmax - rmin + 1;
   // check if parameters are valid
   if (length > 10000 || length < 1 || n > length || n < 1) {
      // error: illegal value
      return NULL;}
   // make dynamic array for all possible numbers
   allnumbers = new int[length];
   // make list of all possible numbers
   for (i=0;  i<length;  i++) allnumbers[i] = i + rmin;
   // make dynamic array for returning result
   list = new int[n];
   // pick n numbers from allnumbers:
   for (i=0; i<n; i++) {
      // there are (length-i) numbers left to choose between:
      SetInterval(i, length-1);
      // get index of next random number
      j = IRandom();
      // get random number into list
      list[i] = allnumbers[j];
      // swap numbers to avoid getting the same number again
      temp = allnumbers[i]; allnumbers[i] = allnumbers[j]; allnumbers[j] = temp;}
   // restore minimum and maximum
   SetInterval (savemin, savemax);
   // delete temporary dynamic array:
   delete[] allnumbers;
   // return dynamic array with results
   // NOTE: list must be deleted by calling program
   return list;}



/* This example shows the use of the base class TRandomGenerator for making
   a soccer pools ('tips') coupon and the use of the derived class
   TLottoGenerator for making a Lotto coupon where the numbers have to be
   different.
*/
void main() {
   int i;  TRandomGenerator rg;
   // make soccer pools coupon using TRandomGenerator:
   rg.SetInterval(0, 2);
   cout << "\nPools coupon:";
   for (i=0; i<13; i++) {
      cout << "\n" << "1x2"[rg.IRandom()];}

   // make Lotto coupon using derived class TLottoGenerator:
   TLottoGenerator lg;  int * LottoNumbers;
   cout << "\n\nLotto coupon:";
   lg.SetInterval(1, 36); // member function of base class
   LottoNumbers = lg.MakeRandomList(7); // member function of derived class
   if (LottoNumbers != NULL) {
      for (i=0; i<7; i++) {
         cout << "\n" << LottoNumbers[i];}
      // delete dynamic array generated by MakeRandomList
      delete[] LottoNumbers;}

   // wait for user to press key
   getch();}
   

************************************************************************
PS.: The chance of winning the great prize in the lottery is less than
the chance of being run over by a car in the street. You better invest
your money in something else!
************************************************************************/


Posted By :-Cplusplusprogramming

Designed By Blogger Templates | Templatelib & Distributed By Blogspot Templates