home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 10 / ioProg_10.iso / soft / optima / samples.z / Abstract.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-30  |  2.1 KB  |  77 lines

  1. // ABSTRACT.CPP
  2. /*
  3.  
  4.    C++ program that illustrates the following
  5.    small class hierarchy:
  6.  
  7.              AbstractArray
  8.                   |
  9.                   |
  10.           /-----------------\
  11.          |                   |
  12.          |                   |
  13.          |                   |
  14.          |                   |
  15.          |                   |
  16.       OneChar             ArrChar
  17.  
  18. */
  19.  
  20. #include <iostream.h>
  21.  
  22. const int ARRAY_SIZE = 100;
  23.  
  24. enum boolean { bFalse, bTrue };
  25.  
  26. class AbstractChar
  27. {
  28.   public:
  29.     // pure-virtual functions (thus, this is an abstract class)
  30.     virtual void store(unsigned char aCharacter) = 0;
  31.     virtual unsigned char recall() = 0;
  32.  
  33.     // non-virtual member functions        
  34.     void nextChar();
  35.     void prevChar();
  36.     void show(const char* pMessage = "",
  37.               boolean bNewLine = bTrue);
  38. };
  39.  
  40. class OneChar : public AbstractChar      // concrete class that 
  41.                                          // inherits from
  42.                                          // AbstractChar class
  43. {
  44.   public:
  45.     // concrete implementations of pure-virtual functions
  46.     virtual void store(unsigned char aCharacter)
  47.       { aCharField = aCharacter; }
  48.     virtual unsigned char recall()
  49.       { return aCharField; }
  50.  
  51.   protected:
  52.     unsigned char aCharField;
  53. };
  54.  
  55. class ArrChar : public AbstractChar      // concrete class that
  56.                                          // inherits from
  57.                                          // AbstractChar class
  58. {
  59.   public:
  60.     ArrChar(const unsigned IndexValue = 0)
  61.       { theIndexValue = (IndexValue < ARRAY_SIZE) ?
  62.                                   IndexValue : 0; }
  63.     virtual void store(unsigned char aCharacter)
  64.       { characterArray[theIndexValue] = aCharacter; }
  65.     virtual unsigned char recall()
  66.       { return characterArray[theIndexValue]; }
  67.     void setIndex(const unsigned IndexValue = 0)
  68.       { theIndexValue = (IndexValue < ARRAY_SIZE) ?
  69.                                    IndexValue : 0; }
  70.  
  71.   protected:
  72.     unsigned char characterArray[ARRAY_SIZE];
  73.     unsigned theIndexValue;
  74. };
  75.  
  76.  
  77.