-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 13.1 (Completed)
619 lines (519 loc) · 11.8 KB
/
Exercise 13.1 (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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
1. (Review) Define the following terms:
a. Container - The STL lists and arrays are held in containers
b. Collection - The STL list and arrays are considered collections
c. Data structure - is a container of data organized in a way that facilitates inserting, retrieving, and deleting data.
d. Iterator - are used to spacify the means of accessing list objects, in much the same way as in index does for an array.
e. List - is considered a container that can hold a collection of zero or more items of the same type.
f. Standard Template Library - provides a tested and generic set of easyly used lists that can be maintained in various configurations.
2. (Review) What sequential container types are supported in the STL?
vector, list, deque
3. (Review) What associative container types are supported in the STL?
set, multiset, map, multimap
4. (Practice) For each of the following, define a class containing only a class declaration section
that can be used to create the following objects:
a. An object (known as a student record) containing a student ID number, the number of
credits completed, and a cumulative grade point average
class Students
{
private:
int ID_number;
double credits;
double GPA;
public:
};
b. An object (known as a student record) capable of holding a student’s name, date of birth,
number of credits completed, and cumulative grade point average
class Students
{
private:
string name;
int date_of_birth;
double credits;
double GPA;
public:
};
c. A mailing list containing a title field, last name field, first name field, two street address
fields, a city field, a state field, and a zip code field
class Mail
{
private:
string title;
string last;
string first;
string address,address2;
string city,state,zip;
public:
};
d. A stock object containing the stock’s name, the purchase price, and the date of purchase
class Stock
{
private:
string name;
double price;
string date;
public:
};
e. An inventory object containing an integer part number, a string part description, an integer
number of parts in inventory, and an integer reorder value
class Inventory
{
int partN;
string partD;
int numberP;
int reorderV;
};
5. (Practice) For each class declared in Exercise 4, add a suitable constructor and accessor
method. Test each method to initialize and display the following data:
a. ID Number: 4672
Number of Credits Completed: 68
Grade Point Average: 3.01
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Students
{
private:
int ID_number;
double credits;
double GPA;
public:
Students(int = 4672, double = 68, double = 3.01); // constructor
void display();
};
// class implementation section
Students::Students(int ii, double cc, double gg)
{
ID_number = ii;
credits = cc;
GPA = gg;
}
void Students::display()
{
cout << ID_number << endl; cout << credits << endl;
cout << GPA << endl;
}
int main()
{
Students student_record;
student_record.display();
system("PAUSE");
return 0;
}
b. Name: Rhona Karp
Date of Birth: 8/4/60
Number of Credits Completed: 96
Grade Point Average: 3.89
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Students
{
private:
int day,month,year;
double credits;
double GPA;
string name;
public:
Students(int = 8,int = 4, int = 60, double = 96, double = 3.89, string = "Rhona Karp"); // constructor
void display();
};
// class implementation section
Students::Students(int dd,int mm, int yy, double cc, double gg, string ss)
{
day = dd;
month = mm;
year = yy;
credits = cc;
GPA = gg;
name = ss;
}
void Students::display()
{
cout << name << endl;
cout << day << '/' << month << '/'<< year << endl;
cout << credits << endl;
cout << GPA << endl;
}
int main()
{
Students student_record;
student_record.display();
system("PAUSE");
return 0;
}
c. Title: Dr.
Last Name: Kingsley
First Name: Kay
Address 1: Apt. 2B
Address 2: 614 Freeman Street
City: Indianapolis
State: IN
Zip Code: 07030
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Mail
{
private:
string title;
string last;
string first;
string address, address2;
string city, state, zip;
public:
Mail(string = "Dr. ", string = " Kingsley ", string = " Kay ", string = "Apt. 2B", string = "614 Freeman Street", string = "Indianpolis", string = "IN", string = "07030"); // constructor
void display();
};
// class implementation section
Mail::Mail(string tt, string ll, string ff, string ad1, string ad2, string cc, string ss, string zz)
{
title = tt;
last = ll;
first = ff;
address = ad1;
address2 = ad2;
city = cc;
state = ss;
zip = zz;
}
void Mail::display()
{
cout << title << first << last <<endl;
cout << address << endl;
cout << address2 << endl;
cout << city << endl;
cout << state << endl;
cout << zip << endl;
}
int main()
{
Mail student_record;
student_record.display();
system("PAUSE");
return 0;
}
d. Stock: IBM
Purchase Price: 134.5
Date Purchased: 10/1/2010
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Students
{
private:
string name;
double price;
string date;
public:
Students(string = "IBM", double = 134.5, string = "10/1/2010"); // constructor
void display();
};
// class implementation section
Students::Students(string ii, double pp , string dd)
{
name = ii;
price = pp;
date = dd;
}
void Students::display()
{
cout << name << endl;
cout << price << endl;
cout << date << endl;
}
int main()
{
Students student_record;
student_record.display();
system("PAUSE");
return 0;
}
e. Part Number: 16879
Description: Battery
Number in Stock: 10
Reorder Number: 3
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Inventory
{
private:
int partN;
string partD;
int numberP;
int reorderV;
public:
Inventory(int = 16879, string = "Batery", int = 10, int = 3); // constructor
void display();
};
// class implementation section
Inventory::Inventory(int pp, string pd, int nn, int rr)
{
partN = pp;
partD = pd;
numberP = nn;
reorderV = rr;
}
void Inventory::display()
{
cout << partN << endl;
cout << partD << endl;
cout << numberP << endl;
cout << reorderV << endl;
}
int main()
{
Inventory student_record;
student_record.display();
system("PAUSE");
return 0;
}
6. (Program) a. Write a C++ program that prompts a user to input the current month, day, and
year. Store the data entered in a suitably defined object and display the date in an appropriate
manner.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Current
{
private:
int month;
int day;
int year;
public:
Current(int = 0, int = 0, int = 0000); // constructor
void display();
};
// class implementation section
Current::Current(int dd, int mm, int yy)
{
}
void Current::display()
{
cout << "Enter date:" << endl;
cin >> day;
cin >> month;
cin >> year;
cout << day <<'/'<< month << '/' << year << endl;
}
int main()
{
Current student_record;
student_record.display();
system("PAUSE");
return 0;
}
b. Modify the program written in Exercise 6a to use an object that accepts the current time in
hours, minutes, and seconds.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Current
{
private:
int hours;
int minutes;
int seconds;
public:
Current(int = 0, int = 0, int = 0); // constructor
void display();
};
// class implementation section
Current::Current(int hh, int mm, int ss)
{
hours = hh;
minutes = mm;
seconds = ss;
}
void Current::display()
{
cout << "Enter time:" << endl;
cin >> hours;
cin >> minutes;
cin >> seconds;
cout << hours <<':'<< minutes << ':' << seconds << endl;
}
int main()
{
Current time;
time.display();
system("PAUSE");
return 0;
}
7. (Program) Define a class capable of creating objects that can store a business’s name, description
of its product or services, address, number of employees, and annual revenue.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Current
{
private:
string business_name;
string description_product;
string address;
int number;
int annual;
public:
Current(string = "NASA", string = "Rocket",string = "1000 Rocket St", int = 10000, int = 1000000); // constructor
void display();
};
// class implementation section
Current::Current(string bb, string dd, string aa, int nn, int an)
{
business_name = bb;
description_product = dd;
address = aa;
number = nn;
annual = an;
}
void Current::display()
{
cout << business_name << endl;
cout << description_product << endl;
cout << address << endl;
cout << number << endl;
cout << annual << endl;
}
int main()
{
Current business;
business.display();
system("PAUSE");
return 0;
}
8. (Practice) Define a class capable of creating objects for different screw types held in inventory.
Each object should contain a field for an integer inventory number, a double-precision
screw length, a double-precision diameter, the kind of head (Phillips or standard slot), the
material (steel, brass, other), and the cost.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Current
{
private:
int num;
double screw_len;
double diameter;
string head;
string material;
public:
Current(int = 7088, double = 5, double = 2, string = "Phillips", string = "steel" ); // constructor
void display();
};
// class implementation section
Current::Current(int nn, double sl, double dd, string hh, string mm)
{
num = nn;
screw_len = sl;
diameter = dd;
head = hh;
material = mm;
}
void Current::display()
{
cout << num << endl;
cout << screw_len << endl;
cout << diameter << endl;
cout << head << endl;
cout << material << endl;
}
int main()
{
Current business;
business.display();
system("PAUSE");
return 0;
}
9. (Program) Write a C++ program that defines a class capable of creating objects for storing the
name of a stock, its estimated earnings per share, and its estimated price-to-earnings ratio.
Have the program prompt the user to enter these items for five different stocks. When data
has been entered for a particular stock, have the program compute and display the anticipated
stock price based on the entered earnings and price-per-earnings values. For example, if a user
enters the data XYZ 1.56 12, the anticipated price for a share of XYZ stock is (1.56)*(12)
= $18.72.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
// class declaration section
class Stock
{
private:
string stock;
double earnings;
double pri_earn_ratio;
public:
Stock(string = "XYZ", double = 1.56, double = 12 ); // constructor
void display();
};
// class implementation section
Stock::Stock(string ss, double ee, double per)
{
stock = ss;
earnings = ee;
pri_earn_ratio = per;
}
void Stock::display()
{
for (int i = 0; i < 5; i++){
cin >> stock;
cin >> earnings;
cin >> pri_earn_ratio;
cout << earnings * pri_earn_ratio;
}
}
int main()
{
Stock business;
business.display();
system("PAUSE");
return 0;
}