Освой самостоятельно С++ за 21 день.
Шрифт:
79: itsLen=len;
80: ASSERT(Invariants);
81: }
82:
83: // Преобразует массив символов к типу String
84: String::String(const char * const cString)
85: {
86: itsLen = strlen(cString);
87: itsString = new char[itsLen+1];
88: for (int i = 0; i<itsLen; i++)
89: itsString[i] = cString[i];
90: itsString[itsLen]='\0';
91: ASSERT(Invariants);
92: }
93:
94: //
95: String::String (const String & rhs)
96: {
97: itsLen=rhs.GetLen;
98: itsString = new char[itsLen+1];
99: for (int i = 0; i<itsLen;i++)
100: itsString[i] = rhs[i];
101: itsString[itsLen] = '\0';
102: ASSERT(Invariants);
103: }
104:
105: // деструктор освобождает выделенную память
106: String::^String
107: {
108: ASSERT(Invariants);
109: delete [] itsString;
110: itsLen = 0;
111: }
112:
113: // оператор выполняет сравнение, освобождает занятую память
114: // затем копирует строку и ее размер
115: String& String::operator=(const String & rhs)
116: {
117: ASSERT(Invariants);
118: if (this == &rhs)
119: return *this;
120: delete [] itsString;
121: itsLen=rhs.GetLen;
122: itsString = new char[itsLen+1];
123: for (int i = 0; i<itsLen;i++)
124: itsString[i] = rhs[i];
125: itsString[itsLen] = '\0';
126: ASSERT(Invariants);
127: return *this;
128: }
129:
130: // неконстантный оператор индексирования
131: char & String:;operator[](int offset)
132: {
133: ASSERT(Invariants);
134: if (offset > itsLen)
135: {
136: ASSERT(Invariants);
137: return itsString[itsLen-1];
138: }
139: else
140: {
141: ASSERT(Invariants);
142: return itsString[offset];
143: }
144: }
145: // константный оператор индексирования
146: char String::operator[](int offset) const
147: {
148: ASSERT(Invariants);
149: char retVal;
150: if (offset > itsLen)
151: retVal = itsString[itsLen-1];
152: else
153: retVal = itsString[offset];
154: ASSERT(Invariants);
155: return retVal;
156: }
157:
158: bool String::Invariants const
159: {
160: PRINT("(String Invariants Checked)");
161: return ( (bool) (itsLen && itsString) ||
162: (!itsLen && !itsString) );
163: }
164:
165: class Animal
166: {
167: public:
168: Anxmal:itsAge(1),itsName("John Q, Animal")
169: { ASSERT(Invariants);}
170:
171: Animal(int, const String&);
172: ~Animal{ }
173:
174: int GetAge
175: {
176: ASSERT(Invariants);
177: return itsAga;
178: }
179:
180: void SetAge(int Age)
181: {
182: ASSERT(Invariants);
183: itsAge = Age;
184: ASSERT(Inva riants);
185: }
186: String& GetName
187: {
188: ASSERT(Invariants);
189: return itsName;
190: }
191:
192: void SetName(const String& name)
193: {
194: ASSERT(Invariants);
195: itsName = name;
196: ASSERT(Invariants);
197: }
198:
199: bool Invariants;
200: private:
201: int itsAge;
202: String itsName;
203: }
204:
205: Animal::Animal(int age, const String& name):
206: itsAge(age),
207: itsName(name)
208: {
209: ASSERT(Invariants);
210: }
211:
212: bool Animal::Invariants
213: {
214: PRINT("(Animal Invariants Checked)");
215: return (itsAge > 0 && itsName.GetLen);
216: }
217:
218: int main
219: {
220: const int AGE = 5;
221: EVAL(AGE);
222: Animal sparky(AGE,"Sparky");
223: cout << "\n" << sparky.GetName.GetStrin;
224: cout << " is ";
225: cout << sparky.GetAge << " years old.";
226: sparky.SetAge(8);
227: cout << "\n" << sparky.GetName.GetString;
228: cout << " is ";