165: ASSERT(Invariants());
166: }
167: String& GetName()
168: {
169: ASSERT(Invariants());
170: return itsName;
171: }
172: void SetName(const String& name)
173: {
174: ASSERT(Invariants());
175: itsName = name;
176: ASSERT(Invariants());
177: }
178: BOOL Invariants();
179: private:
180: int itsAge;
181: String itsName;
182: };
183:
184: Animal::Animal(int age, const String& name):
185: itsAge(age),
186: itsName(name)
187: {
188: ASSERT(Invariants());
189: }
190:
191: BOOL Animal::Invariants()
192: {
193: #ifdef SHOW_INVARIANTS
194: cout << “Animal Tested OK”;
195: #endif
196: return (itsAge > 0 && itsName.GetLen());
197: }
198:
199: int main()
200: {
201: Animal sparky(5,”Sparky”);
202: cout << endl << sparky.GetName().GetString() << “ is “;
203: cout << sparky.GetAge() << “ years old.”;
204: sparky.SetAge(8);
205: cout << endl << sparky.GetName().GetString() << “ is “;
206: cout << sparky.GetAge() << “ years old.”;
207: return 0;
208: }
768 Day 21
LISTING21.4 continued