Sams Teach Yourself C++ in 21 Days

(singke) #1
What’s Next 767

21


117: char & String::operator[](int offset)
118: {
119: ASSERT(Invariants());
120: if (offset > itsLen)
121: {
122: ASSERT(Invariants());
123: return itsString[itsLen-1];
124: }
125: else
126: {
127: ASSERT(Invariants());
128: return itsString[offset];
129: }
130: }
131:
132: // constant offset operator
133: char String::operator[](int offset) const
134: {
135: ASSERT(Invariants());
136: char retVal;
137: if (offset > itsLen)
138: retVal = itsString[itsLen-1];
139: else
140: retVal = itsString[offset];
141: ASSERT(Invariants());
142: return retVal;
143: }
144:
145: BOOL String::Invariants() const
146: {
147: #ifdef SHOW_INVARIANTS
148: cout << “String Tested OK “;
149: #endif
150: return ( (itsLen && itsString) || (!itsLen && !itsString) );
151: }
152:
153: class Animal
154: {
155: public:
156: Animal():itsAge(1),itsName(“John Q. Animal”)
157: {ASSERT(Invariants());}
158: Animal(int, const String&);
159: ~Animal(){}
160: int GetAge() { ASSERT(Invariants()); return itsAge;}
161: void SetAge(int Age)
162: {
163: ASSERT(Invariants());
164: itsAge = Age;

LISTING21.4 continued

Free download pdf