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

  1. /*
  2.  
  3.    C++ program that illustrates the following
  4.    small class hierarchy:
  5.  
  6.       aString ------\
  7.          |          |
  8.          |          |
  9.          |          |
  10.          |          |
  11.          |          |
  12.       anInt        LeDouble
  13.          |
  14.          |
  15.          |
  16.          |
  17.          |
  18.       aDouble
  19.  
  20. */
  21.  
  22. #include <iostream.h>
  23. #include <math.h>
  24. #include <string.h>
  25.  
  26. const int STR_LEN = 31;       // declares constant for
  27.                               // string length
  28.  
  29. class aString                 // declares the base class
  30. {
  31.   public:
  32.     aString()                 // constructor (also initializes)
  33.       { someText[0] = '\0'; }
  34.     void setText(const char* pText)   // sets the data member
  35.       { strcpy(someText, pText); }
  36.     void showText()                   // shows the data member
  37.       { cout << someText << "\n"; }
  38.  
  39.   protected:
  40.     char someText[STR_LEN];        // declares the data member
  41. };
  42.  
  43. class anInt : public aString       // declares anInt class as
  44.                                    // a derived class of
  45.                                    // aString
  46. {
  47.   public:
  48.     anInt() : aString()            // invokes aStringÆs 
  49.       { anIntValue = 0; }          // constructor and
  50.                                    // initializes its own
  51.                                    // data member
  52.     void setInt(int IntValue)      // sets the data member
  53.       { anIntValue = IntValue; }
  54.     void showInt()                     // shows the data member
  55.        { cout << anIntValue << "\n"; }
  56.  
  57.   protected:
  58.     int anIntValue;                // declares data member
  59. };
  60.  
  61. class aDouble : public anInt       // declares aDouble as a
  62.                                    // derived class of anInt
  63. {
  64.   public:
  65.     aDouble() : anInt()       // invokes anIntÆs     
  66.       { aDoubleValue = 0; }   // constructor, then initializes
  67.                               // its own data member to 0
  68.  
  69.     void setDouble(double DoubleValue) // sets the data member
  70.       { aDoubleValue = DoubleValue; }
  71.     void showDouble()                  // shows the data member
  72.       { cout << aDoubleValue << "\n"; }
  73.  
  74.   protected:
  75.     double aDoubleValue;    // declares the data member
  76. };
  77.  
  78. class LeDouble : public aString
  79. {
  80.   public:
  81.     LeDouble() : aString()         // invokes aStringÆs
  82.                                    // constructor, then
  83.                                    // initializes member to 0
  84.       { aDoubleValue = 0; }
  85.     void setDouble(double DoubleValue) // sets the data member
  86.       { aDoubleValue = DoubleValue; }
  87.     void showDouble()                  // shows the data member
  88.        { cout << aDoubleValue << "\n"; }
  89.  
  90.   protected:
  91.     double aDoubleValue;            // declares the data member
  92. };
  93.  
  94.