home *** CD-ROM | disk | FTP | other *** search
- /*
-
- C++ program that illustrates the following
- small class hierarchy:
-
- aString ------\
- | |
- | |
- | |
- | |
- | |
- anInt LeDouble
- |
- |
- |
- |
- |
- aDouble
-
- */
-
- #include <iostream.h>
- #include <math.h>
- #include <string.h>
-
- const int STR_LEN = 31; // declares constant for
- // string length
-
- class aString // declares the base class
- {
- public:
- aString() // constructor (also initializes)
- { someText[0] = '\0'; }
- void setText(const char* pText) // sets the data member
- { strcpy(someText, pText); }
- void showText() // shows the data member
- { cout << someText << "\n"; }
-
- protected:
- char someText[STR_LEN]; // declares the data member
- };
-
- class anInt : public aString // declares anInt class as
- // a derived class of
- // aString
- {
- public:
- anInt() : aString() // invokes aStringÆs
- { anIntValue = 0; } // constructor and
- // initializes its own
- // data member
- void setInt(int IntValue) // sets the data member
- { anIntValue = IntValue; }
- void showInt() // shows the data member
- { cout << anIntValue << "\n"; }
-
- protected:
- int anIntValue; // declares data member
- };
-
- class aDouble : public anInt // declares aDouble as a
- // derived class of anInt
- {
- public:
- aDouble() : anInt() // invokes anIntÆs
- { aDoubleValue = 0; } // constructor, then initializes
- // its own data member to 0
-
- void setDouble(double DoubleValue) // sets the data member
- { aDoubleValue = DoubleValue; }
- void showDouble() // shows the data member
- { cout << aDoubleValue << "\n"; }
-
- protected:
- double aDoubleValue; // declares the data member
- };
-
- class LeDouble : public aString
- {
- public:
- LeDouble() : aString() // invokes aStringÆs
- // constructor, then
- // initializes member to 0
- { aDoubleValue = 0; }
- void setDouble(double DoubleValue) // sets the data member
- { aDoubleValue = DoubleValue; }
- void showDouble() // shows the data member
- { cout << aDoubleValue << "\n"; }
-
- protected:
- double aDoubleValue; // declares the data member
- };
-
-