-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 10.3 (Completed)
345 lines (285 loc) · 8.59 KB
/
Exercise 10.3 (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
1. (Review) Determine whether the following statements are true or false:
a. A constructor method must have the same name as its class.
True
b. A class can have only one constructor method.
False
c. A class can have only one default constructor method.
True
d. A default constructor can be supplied only by the compiler.
False
e. A default constructor can have no parameters or all parameters must have default values.
True
f. A constructor must be declared for each class.
False
g. A constructor must be declared with a return type.
False
h. A constructor is called automatically each time an object is created.
True
i. A class can have only one destructor method.
True
j. A destructor must have the same name as its class, preceded by a tilde (~).
True
k. A destructor can have default arguments.
False
l. A destructor must be declared for each class.
True
m. A destructor must be declared with a return type.
False
n. A destructor is called automatically each time an object goes out of existence.
True
o. Destructors aren’t useful when the class contains a pointer data member.
False
2. (Desk check) For Program 10.3, what date is initialized for object c if the declaration
Date c(15); is used instead of the declaration Date c(20090515L);?
The date is 15/04/12
3. (Modify) Modify Program 10.3 so that the only data member of the class is a long integer
named yyyymmdd. Do this by substituting the declaration long yyyymmdd; for these existing
declarations:
int month;
int day;
int year;
Using the same constructor prototypes currently declared in the class declaration section,
rewrite them so that the Date(long) method becomes the default constructor, and the
Date(int, int, int) method converts a month, day, and year into the correct form for the
class data members.
class Date
{
private:
int month;
int day;
int year;
public:
Date(int, int, int); // constructor
Date(long = 20120704); // another constructor
void showDate(); // member method to display a date
};
4. (Program) a. Construct a Time class containing integer data members seconds, minutes,
and hours. Have the class contain two constructors: The first should be a default constructor
having the prototype Time(int, int, int), which uses default values of 0 for each data
member. The second constructor should accept a long integer representing a total number of
seconds and disassemble the long integer into hours, minutes, and seconds. The final member
method should display the class data members.
class Time
{
private:
int seconds;
int minutes;
int hours;
public:
Time(int = 0, int = 0, int = 0); // constructor
Time(long); // another constructor
void showTime(); // member method to display a Time
};
// class implementation section
Time::Time(int ss, int mm, int hh)
{
seconds = ss;
minutes = mm;
hours = hh;
}
Time::Time(long hhmmss)
{
hours = int(hhmmss / 10000.0); // extract the year
minutes = int((hhmmss - hours * 10000.0) / 100.00); // extract the month
seconds = int(hhmmss - hours * 10000.0 - minutes * 100.0); // extract the day
}
void Time::showTime()
{
cout << "The Time is ";
cout << setfill('0')
<< setw(2) << hours << ' '
<< setw(2) << minutes << ' '
<< setw(2) << seconds; // extract the last 2 year digits
cout << endl;
return;
}
b. Include the class written for Exercise 4a in the context of a complete program.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
// class declaration section
class Time
{
private:
int seconds;
int minutes;
int hours;
public:
Time(int = 0, int = 0, int = 0); // constructor
Time(long); // another constructor
void showTime(); // member method to display a Time
};
// class implementation section
Time::Time(int ss, int mm, int hh)
{
seconds = ss;
minutes = mm;
hours = hh;
}
Time::Time(long hhmmss)
{
hours = int(hhmmss / 10000.0); // extract the year
minutes = int((hhmmss - hours * 10000.0) / 100.00); // extract the month
seconds = int(hhmmss - hours * 10000.0 - minutes * 100.0); // extract the day
}
void Time::showTime()
{
cout << "The Time is ";
cout << setfill('0')
<< setw(2) << hours << ' '
<< setw(2) << minutes << ' '
<< setw(2) << seconds; // extract the last 2 year digits
cout << endl;
return;
}
int main()
{
Time a, b(4, 1, 19), c(103515L); // declare three objects
a.showTime(); // display object a's values
b.showTime(); // display object b's values
c.showTime(); // display object c's values
system("PAUSE");
return 0;
}
5. (Program) a. Construct a class named Student consisting of an integer student ID number,
an array of five double-precision grades, and an integer representing the total number of grades
entered. The constructor for this class should initialize all Student data members to 0.
Included in the class should be member methods to 1) enter a student ID number, 2) enter a
single test grade and update the total number of grades entered, and 3) compute an average
grade and display the student ID followed by the average grade.
Done!!
b. Include the class constructed in Exercise 5a in the context of a complete program. Your
program should declare two objects of type Student and accept and display data for the two
objects to verify operation of the member methods.
#include <iostream>
#include <iomanip>
using namespace std;
// class declaration section
class Student
{
private:
int IDnum;
double grade;
int total;
public:
Student(int = 0, double = 0, int = 0); // constructor
void averageGrade(int); // another constructor
void displayIDAvg(); // member method to display a Student
};
// class implementation section
Student::Student(int ID, double grad, int tot)
{
IDnum = ID;
total = tot;
grade = grad;
}
void Student::averageGrade( int grades){
int result = grades;
int average = result ;
cout << average << endl;
return;
}
void Student::displayIDAvg()
{
cout << "The Student is ID & Average is ";
cout << IDnum << endl;
averageGrade(100);
cout << endl;
return;
}
int main()
{
Student a, b(374, 98, 1); // declare three objects
b.displayIDAvg();
system("PAUSE");
return 0;
}
6. (Modify) a. In Exercise 4, you were asked to construct a Time class. For this class, include a
tick() method that increments the time by one second. Test your method to ensure that it
increments time into a new minute and a new hour correctly.
void Time::tick(){
if (minutes != 60 && seconds == 60) {
minutes++;
seconds = 1;
}
else if (minutes == 60 && seconds == 60){
hours++;
seconds = 1;
minutes = 1;
}
else if (seconds < 60 && minutes != 60){
seconds++;
}
cout << hours << ':' << minutes << ':' << seconds << endl;
}
b. Modify the Time class written for Exercise 6a to include a detick() method that decrements
the time by one second. Test your method to ensure that it decrements time into a
prior hour and into a prior minute correctly.
void Time::detick(){
if (minutes != 1 && seconds == 1) {
minutes--;
seconds = 59;
}
else if (minutes == 1 && seconds == 1){
--hours;
seconds = 59;
minutes = 59;
}
else if (seconds < 60 && minutes != 60){
seconds--;
}
cout << hours<< ':' << minutes << ':' << seconds << endl;
}
7. (Program) a. Construct a class named Coord containing two double-precision data members
named xval and yval, used to store a point’s x and y values in Cartesian coordinates. The
member methods should include constructor and display methods and a method named
convToCartesian(). The convToCartesian() method should accept two double-precision
numbers named r and theta representing a point in polar coordinates and convert them into
Cartesian coordinates. For conversion from polar to Cartesian coordinates, use these formulas:
x = r cos (theta)
y = r sin (theta)
Done!!
b. Include the program written for Exercise 7a in a working C++ program.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// class declaration section
class Coord
{
private:
double xval;
double yval;
public:
Coord(double = 20, double = 30); // constructor
void convToCartesian(double,double);
void showCoord(); // member method to display a Coord
};
// class implementation section
Coord::Coord(double xx, double yy)
{
xval = xx;
yval = yy;
}
void Coord::convToCartesian(double r,double theta){
double x = r * cos(theta);
double y = r * sin(theta);
cout << x << ',' << y << endl;
return;
}
void Coord::showCoord(){
cout << xval << ',' << yval << endl;
}
int main()
{
Coord a, b(4, 1), c; // declare three objects
a.showCoord();
b.showCoord();
c.showCoord();
a.convToCartesian(10, 23);
b.convToCartesian(24, 45);
c.convToCartesian(23, 12);
system("PAUSE");
return 0;
}