-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 10.5(Completed)
403 lines (332 loc) · 8.35 KB
/
Exercise 10.5(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
1. (Modify) a. Rewrite Program 10.8 to include an integer static variable named numemps. This
variable should act as a counter that’s initialized to 0 and incremented by the class constructor
each time a new object is declared. Rewrite the static method dispRate() to display this
counter’s value.
Done!!
b. Test the program written for Exercise 1a. Have the main() function call dispRate() after
each Employee object is created.
#include <iostream>
using namespace std;
// class declaration section
class Employee
{
private:
static double taxRate;
int idNum;
static int numemps;
public:
Employee(int = 0); // constructor
void display(); // accessor method
static void dispRate(static int = 0); // static member method
};
// static member definition
double Employee::taxRate = 0.07;
// class implementation section
Employee::Employee(int num)
{
idNum = num;
}
void Employee::display()
{
cout << "Employee number " << idNum
<< " has a tax rate of " << taxRate << endl;
return;
}
void Employee::dispRate(static int i)
{
cout << "The static tax rate is " << taxRate << endl;
cout << i << endl;
return;
}
int main()
{
int i = 0;
Employee emp1(11122), emp2(11133),emp3(11144),emp4(11155),a,b,c;
Employee::dispRate(i++); // call the static member methods
emp1.display();
Employee::dispRate(i++);
emp2.display();
Employee::dispRate(i++);
emp3.display();
Employee::dispRate(i++);
emp4.display();
system("PAUSE");
return 0;
}
2. (Program) a. Construct a class named Circle containing two integer variables named
xCenter and yCenter and a double-precision variable named radius. Additionally, the class
should contain a static data member named scaleFactor. The xCenter and yCenter values
represent a circle’s center point, radius represents the circle’s actual radius, and scaleFactor
represents a scale factor used to scale the circle to fit on a variety of display devices.
Done!!
b. Include the class written for Exercise 2a in a working C++ program.
#include "stdafx.h"
#include <iostream>
using namespace std;
// class declaration section
class Circle
{
private:
int xCenter;
int yCenter;
double radius;
int scaleFactor;
public:
Circle(int = 5, int = 5, double = 3, int = 5); // constructor
void display(); // accessor method
};
// class implementation section
Circle::Circle(int x,int y, double r , int s)
{
xCenter = x;
yCenter = y;
radius = r;
scaleFactor = s;
}
void Circle::display()
{
cout << xCenter * scaleFactor << " , " << yCenter * scaleFactor << endl;
cout << "The radius is " << radius * scaleFactor<< endl;
}
int main()
{
Circle a(2,3,4,1),b(2,1,3,2),c;
a.display();
b.display();
c.display();
system("PAUSE");
return 0;
}
3. (Debug) a. State whether the following three statements in Program 10.9
re = addreal(a,b);
im = addimag(a,b);
Complex c(re,im); // create a new Complex object
could be replaced by this single statement:
Complex c(addreal(a,b), addimag(a,b));
Yes
b. Verify your answer to Exercise 3a by running Program 10.9 with the suggested replacement
statement.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
// class declaration section
class Complex
{
// friends list
friend double addreal(Complex&, Complex&);
friend double addimag(Complex&, Complex&);
private:
double real;
double imag;
public:
Complex(double = 0, double = 0); // constructor
void display();
};
// class implementation section
Complex::Complex(double rl, double im)
{
real = rl;
imag = im;
}
void Complex::display()
{
char sign = '+';
if (imag < 0) sign = '-';
cout << real << sign << abs(imag) << 'i';
return;
}
// friend implementations
double addreal(Complex &a, Complex &b)
{
return(a.real + b.real);
}
double addimag(Complex &a, Complex &b)
{
return(a.imag + b.imag);
}
int main()
{
Complex a(3.2, 5.6), b(1.1, -8.4);
double re, im;
cout << "\nThe first complex number is ";
a.display();
cout << "\nThe second complex number is ";
b.display();
Complex c(addreal(a, b), addimag(a, b));
cout << "\n\nThe sum of these two complex numbers is ";
c.display();
cout <<endl;
system("PAUSE");
return 0;
}
4. (Modify) a. Rewrite the program written for Exercise 2a, but include a friend function that
multiples an object’s radius by a static scaleFactor and then displays the actual radius value
and the scaled value.
Done!!
b. Test the program written for Exercise 4a.
#include "stdafx.h"
#include <iostream>
using namespace std;
// class declaration section
class Circle
{
//friends list
friend double addscale(Circle&);
private:
int xCenter;
int yCenter;
double radius;
int scaleFactor = 2;
public:
Circle(int = 5, int = 5, double = 3); // constructor
void display(); // accessor method
};
// class implementation section
Circle::Circle(int x, int y, double r)
{
xCenter = x;
yCenter = y;
radius = r;
}
void Circle::display()
{
//cout << xCenter * scaleFactor << " , " << yCenter * scaleFactor << endl;
//cout << "The radius is " << radius * scaleFactor << endl;
}
//friend implementations
double addscale(Circle &a)
{
cout << "The orginal radius is " << a.radius << endl;
cout << "THe scale radius is " << (a.radius) * a.scaleFactor << endl;
return 0;
}
int main()
{
Circle a(5,2,10);
Circle c(addscale(a));
c.display();
system("PAUSE");
return 0;
}
5. (Modify) Rewrite Program 10.9 to have only one friend function named addComplex(). This
function should accept two Complex objects and return a Complex object. The real and
imaginary parts of the returned object should be the sum of the real and imaginary parts of the
two objects passed to addComplex().
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <tuple>
using namespace std;
// class declaration section
class Complex
{
// friends list
friend double addComplex(Complex&, Complex&);
private:
double real;
double imag;
public:
Complex(double = 0, double = 0); // constructor
void display();
};
// class implementation section
Complex::Complex(double rl, double im)
{
real = rl;
imag = im;
}
void Complex::display()
{
char sign = '+';
if (imag < 0) sign = '-';
cout << real << sign << abs(imag) << 'i';
return;
}
// friend implementations
double addComplex(Complex &a, Complex &b)
{
double myComp = (a.real + b.real);
double imagComp = (a.imag + b.imag);
char sign = '+';
if (imagComp < 0) sign = '-';
cout << myComp << sign << abs(imagComp) << 'i';
return 0;
}
int main()
{
Complex a(3.2, 5.6), b(1.1, -8.4);
double re, im;
cout << "\nThe first complex number is ";
a.display();
cout << "\nThe second complex number is ";
b.display();
cout << "\n\nThe sum of these two complex numbers is ";
Complex c(addComplex(a, b));
cout << endl;
system("PAUSE");
return 0;
}
6. (Program) a. Construct a class named Coord containing two double-precision variables
named xval and yval, used to store the x and y values of a point in rectangular coordinates.
The class methods should include constructor and display methods and a friend function
named convPol(). The convPol() function should accept two double-precision numbers, r
and theta, representing a point in polar coordinates and convert them into rectangular coordinates.
For conversion from polar to rectangular coordinates, use these formulas:
x = r cos(theta)
y = r sin(theta)
b. Include the class written for Exercise 6a in a working C++ program.
// Exercise.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <tuple>
using namespace std;
// class declaration section
class Coord
{
// friends list
friend double convPol(Coord&);
private:
double xval;
double yval;
public:
Coord(double = 0, double = 0); // constructor
void display();
};
// class implementation section
Coord::Coord(double x, double y)
{
xval = x;
yval = y;
}
void Coord::display()
{
char sign = ';';
cout << xval << sign << (yval);
return;
}
// friend implementations
double convPol(Coord &a)
{
double r = a.xval,theta = a.yval;
double x = r * cos(theta);
double y = r * sin(theta);
char sign = ',';
cout << x << sign << y << endl;
return 0;
}
int main()
{
Coord a(3.2, 5.6), b(1.1, 8.4);
double r, theta;
cout << "\nThe first Coord number is ";
a.display();
cout << "\nThe second Coord number is ";
b.display();
cout << "\n\nCoordiate A values as radius and angle than convered back into Retangler Coordiates ";
Coord c(convPol(a));
cout << endl;
system("PAUSE");
return 0;
}