-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 12.4 (Completed)
384 lines (314 loc) · 8.83 KB
/
Exercise 12.4 (Completed)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
1. (Program) Include the copy constructor and assignment operator explained in this section in
Program 12.6, and run the program to verify their operation.
#include "stdafx.h"
#include <iostream>
#include <string>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Book
{
private:
char *title; // a pointer to a book title
public:
Book(char* = '\0');
Book(Book& oldbook); // constructor
void showtitle(void); // display the title
void operator =(Book& oldbook);
};
// class implementation section
Book::Book(char* strng)
{
title = new char[strlen(strng) + 1]; // allocate memory
strcpy(title, strng); // store the string
}
Book::Book(Book& oldbook)
{
cout << "Copy constructor allocating ptr." << endl;
title = new char[strlen(oldbook.title) + 1]; // allocate new memory
strcpy(title, oldbook.title); // copy the title
}
void Book::operator =(Book& oldbook)
{
if (oldbook.title != NULL) // check that it exists
delete(title); // release existing memory
title = new char[strlen(oldbook.title) + 1]; // allocate new memory
strcpy(title, oldbook.title); // copy the title
}
void Book::showtitle(void)
{
cout << title << endl;
return;
}
int main()
{
Book book1("Windows Primer"); // create 1st title
Book book2("A Brief History of Western Civilization"); // 2nd title
book2 = book1;
book1.showtitle(); // display book1's title
book2.showtitle(); // display book2's title
system("PAUSE");
return 0;
}
2. (Program) Write a suitable destructor method for Program 12.6.
#include "stdafx.h"
#include <iostream>
#include <string>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Book
{
private:
char *title; // a pointer to a book title
public:
Book(char* = '\0');
Book(Book& oldbook); // constructor
void showtitle(void); // display the title
void operator =(Book& oldbook);
~Book();
};
// class implementation section
Book::Book(char* strng)
{
title = new char[strlen(strng) + 1]; // allocate memory
strcpy(title, strng); // store the string
}
Book::Book(Book& oldbook)
{
title = new char[strlen(oldbook.title) + 1]; // allocate new memory
strcpy(title, oldbook.title); // copy the title
}
void Book::operator =(Book& oldbook)
{
if (oldbook.title != NULL) // check that it exists
delete(title); // release existing memory
title = new char[strlen(oldbook.title) + 1]; // allocate new memory
strcpy(title, oldbook.title); // copy the title
}
void Book::showtitle(void)
{
cout << title << endl;
return;
}
Book:: ~Book(void)
{
cout << "Freeing memory from allocation!" << endl;
delete title;
}
int main()
{
Book book1("Windows Primer"); // create 1st title
Book book2("A Brief History of Western Civilization"); // 2nd title
book2 = book1;
book1.showtitle(); // display book1's title
book2.showtitle(); // display book2's title
book2.~Book();
system("PAUSE");
return 0;
}
3. (Program) a. Construct a class named Car containing these four data members: a doubleprecision
variable named engineSize, a character variable named bodyStyle, an integer
variable named colorCode, and a character pointer named vinPtr to a vehicle ID code. The
member functions should include a constructor that provides default values of 0 for each
numeric data member, an X for each character variable, and a NULL for each pointer; a display
function that prints the engine size, body style, color code, and vehicle ID code; and an assignment
operator that performs a memberwise assignment between two Car objects and handles
the pointer member correctly.
Done!!
b. Include the class written for Exercise 3a in a working C++ program that creates two Car
objects; the second object should be assigned the values of the first object.
#include "stdafx.h"
#include <iostream>
#include <string>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Car
{
private:
double engineSize;
char bodyStyle;
int colorCode;
char *vinPtr; // a pointer to a Car vinPtr
public:
Car(double = 0, char = 'X', int = 0, char* = '\0');
Car(Car& oldCar); // constructor
void showvinPtr(void); // display the vinPtr
void operator =(Car& oldCar);
~Car();
};
// class implementation section
Car::Car(double ee, char bb, int cc, char* strng)
{
engineSize = ee;
bodyStyle = bb;
colorCode = cc;
vinPtr = new char[strlen(strng) + 1]; // allocate memory
strcpy(vinPtr, strng); // store the string
}
Car::Car(Car& oldCar)
{
engineSize = oldCar.engineSize;
bodyStyle = oldCar.bodyStyle;
colorCode = oldCar.colorCode;
vinPtr = new char[strlen(oldCar.vinPtr) + 1]; // allocate new memory
strcpy(vinPtr, oldCar.vinPtr); // copy the vinPtr
}
void Car::operator =(Car& oldCar)
{
if (oldCar.vinPtr != NULL) // check that it exists
delete(vinPtr); // release existing memory
vinPtr = new char[strlen(oldCar.vinPtr) + 1]; // allocate new memory
strcpy(vinPtr, oldCar.vinPtr); // copy the vinPtr
}
void Car::showvinPtr(void)
{
cout << "The car's engine size is " << engineSize << endl;
cout << "The body style is " << bodyStyle << endl;
cout << "The color code is " << colorCode << endl;
cout << "The Car vechicle ID code is " << vinPtr << endl;
return;
}
Car:: ~Car(void)
{
cout << "Freeing memory from allocation!" << endl;
delete vinPtr;
}
int main()
{
Car Car1(52.23,'A',5,"Ford Focus"); // create 1st vinPtr
Car Car2 = Car1;
Car1.showvinPtr(); // display Car1's vinPtr
Car2.showvinPtr(); // display Car2's vinPtr
system("PAUSE");
return 0;
}
4. (Modify) Modify Program 12.6 to include the assignment statement b = a, and then run the
modified program to assess any error messages that occur.
#include "stdafx.h"
#include <iostream>
#include <string>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Book
{
private:
char *title; // a pointer to a book title
public:
Book(char * = '\0'); // constructor
void showtitle(void); // display the title
};
// class implementation section
Book::Book(char *strng)
{
title = new char[strlen(strng) + 1]; // allocate memory
strcpy(title, strng); // store the string
}
void Book::showtitle(void)
{
cout << title << endl;
return;
}
int main()
{
Book a("Land Before Time"),b("Land After Time"),book1("Windows Primer"); // create 1st title
Book book2("A Brief History of Western Civilization"); // 2nd title
b = a;
b.showtitle();
a.showtitle();
book1.showtitle(); // display book1's title
book2.showtitle(); // display book2's title
system("PAUSE");
return 0;
}
5. (Modify) Using Program 12.6 as a start, write a program that creates five Book objects. The
program should allow the user to enter the five book titles interactively and then display the
titles entered.
#include "stdafx.h"
#include <iostream>
#include <string>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Book
{
private:
char *title; // a pointer to a book title
public:
Book(char * = '\0'); // constructor
void showtitle(void); // display the title
};
// class implementation section
Book::Book(char *strng)
{
title = new char[strlen(strng) + 1]; // allocate memory
strcpy(title, strng); // store the string
}
void Book::showtitle(void)
{
cin >> title;
cout << title << endl;
return;
}
int main()
{
Book book1(" "); // create 1st title
Book book2(" "); // 2nd title
Book book3(" ");
Book book4(" ");
Book book5(" ");
book1.showtitle(); // display book1's title
book2.showtitle(); // display book2's title
book3.showtitle();
book4.showtitle();
book5.showtitle();
system("PAUSE");
return 0;
}
6. (Modify) Modify the program written in Exercise 5 so that the program sorts the entered book
titles in alphabetical order before it displays them. (Hint: You have to define a sort routine for
the titles; refer back to Section 7.7.)
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Book
{
private:
char *title; // a pointer to a book title
public:
Book(char * = '\0'); // constructor
void showtitle(void); // display the title
};
// class implementation section
Book::Book(char *strng)
{
title = new char[strlen(strng) + 1]; // allocate memory
strcpy(title, strng); // store the string
}
void Book::showtitle(void)
{
vector<string> stringVec = { " ", " ", " ", " ", " " };
for (int i = 0; i < 5; i++){
cin >> stringVec[i];
}
sort(stringVec.begin(), stringVec.end());
for (string &s : stringVec){
cout << s << " ";
}
//cout << title << endl;
return;
}
int main()
{
Book books(" "); // create 1st title
books.showtitle(); // display book1's title
system("PAUSE");
return 0;
}