home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX09091.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-25  |  3.5 KB  |  108 lines

  1. // \EXAMPLES\EX09091.H
  2.  
  3. // Files used in this example:
  4. //---------------------------------------------------------
  5. // EX09091.H      this file
  6. // %F,15,EX09091.CPP%EX09091.CPP
  7. // %F,15,EX0909.CPP%EX0909.CPP
  8. //---------------------------------------------------------
  9.  
  10. // Used to store a pair of integers
  11. typedef struct { long first, second;} LongPair;
  12.  
  13. //---------------------------------------------------------
  14. // The Grades Class manages an associative array of grades
  15. //---------------------------------------------------------
  16. class Grade
  17. {
  18. public:
  19.    // Used to associate a letter grade to a numeric grade
  20.    // range
  21.    typedef struct
  22.    {
  23.       char letter[4];  // Used to store a letter grade
  24.       LongPair range;  // Used to store a numeric grade range
  25.    } GradeAssoc;
  26.  
  27.    static const LongPair null;
  28.  
  29.    //------------------------------------------------------
  30.    // FUNCTION:     operator[]
  31.    // PARAMETERS:   char * p A letter grade
  32.    // Return Value: LongPair A numeric range
  33.    //------------------------------------------------------
  34.    LongPair
  35.    operator[]( char* p);
  36.  
  37.    //------------------------------------------------------
  38.    // FUNCTION:      operator[]
  39.    // PARAMETERS:   LongPair A numeric range
  40.    // RETURN VALUE: char * p A letter grade
  41.    //------------------------------------------------------
  42.    char*
  43.    Grade::operator[]( int x);
  44.  
  45.    //------------------------------------------------------
  46.    // Iterates through all values in the Grade Associative
  47.    // array
  48.    //------------------------------------------------------
  49.    friend class GradeIterator;
  50.  
  51.    //------------------------------------------------------
  52.    // FUNCTION:      NumElem
  53.    // RETURN VALUE: The number of grades in the associative
  54.    // array
  55.    //------------------------------------------------------
  56.    const int
  57.    NumElem() const { return 5;}
  58.  
  59. private:
  60.    //------------------------------------------------------
  61.    // The associative array of Numeric grade ranges and Letter
  62.    // grades
  63.    //------------------------------------------------------
  64.    static GradeAssoc GradeList[];
  65.  
  66.    //------------------------------------------------------
  67.    // Used to compare two Letter grades
  68.    //------------------------------------------------------
  69. //   friend int _Optlink     // IBM CSet II version
  70. //   CompareLetter( const void* keyval, const void* datum);
  71.    friend int            // Borland and Microsoft version
  72.    CompareLetter( const void* keyval, const void* datum);
  73.  
  74. };
  75.  
  76.  
  77. ostream&
  78. operator<<( ostream& os, LongPair range);
  79.  
  80.  
  81. ostream&
  82. operator<<( ostream& os, Grade::GradeAssoc assoc);
  83.  
  84. //---------------------------------------------------------
  85. // Use to iterate through a the associative array of grades
  86. //---------------------------------------------------------
  87. class GradeIterator
  88. {
  89.    const Grade* pGrade;      // points to the associative array
  90.    int index; // used to increment through the associative array
  91. public:
  92.   //-------------------------------------------------------
  93.   // FUNCTION: constructor
  94.   // PARAMETERS: Grade& grade
  95.   //-------------------------------------------------------
  96.    GradeIterator( const Grade& grade);
  97.  
  98.   //-------------------------------------------------------
  99.   // FUNCTION: operator()
  100.   // RETURN VALUE: the next Grade association in
  101.   // the associative array
  102.   //-------------------------------------------------------
  103.    Grade::GradeAssoc*
  104.    operator()();
  105. };
  106.  
  107.  
  108.