-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 12.1 (Completed)
340 lines (305 loc) · 10.4 KB
/
Exercise 12.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
1. (Review) Define the following terms:
a. inheritance - constructing one class from another which is the capability of deriving one class form another class.
b. base class - the class used as the basis for a derived class
c. derived class - is a new class incorporating all data members and member functions of its base class.
d. simple inheritance - in which each derived type has only one base type.
e. multiple inheritance- in wihic a derived type has two or more base types.
f. class hierarchy - order in which one class is derived from another.
2. (Review) Describe the difference between private and protected class members.
Protected access behaves the same as private access, in that it permits access only to
member or friend functions, but it allows any derived class to inherit this restriction. The
derived class then defines the type of inheritance it’s willing to take on, subject to the base
class’s access restrictions. This definition is done by the class access specifier, which is listed
after the colon at the start of the class declaration section.
3. (Review) What three features must a programming language include to be classified as an
object-oriented language?
classes, inheritance and polymorphism.
4. (Modify) a. Modify Program 12.1 to include a derived class named Sphere from the base
Circle class. The only additional class members of Sphere should be a constructor and a
calcval() function that returns the sphere’s volume. (Note: Volume = 4 / 3 πr3.)
Done!!
b. Include the class constructed for Exercise 4a in a working C++ program. Have your program
call all the member functions in the Sphere class.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 2.0 * asin(1.0);
// class declaration section
class Circle
{
protected:
double radius;
public:
Circle(double = 1.0); // constructor
double calcval();
};
// class implementation section for Circle
Circle::Circle(double r) // constructor
{
radius = r;
}
// calculate the area of a Circle
double Circle::calcval()
{
return(PI * radius * radius);
}
// class declaration section where Cylinder is derived from Circle
class Cylinder : public Circle
{
protected:
double length; // add one data member and
public: // two member functions
Cylinder(double r = 1.0, double l = 1.0) : Circle(r), length(l) {}
double calcval();
};
// class implementation section for Cylinder
double Cylinder::calcval() // calculates a volume
{
return (length * Circle::calcval()); // note the base function call
}
class Sphere : public Circle
{
public:
Sphere(double r = 1.0) : Circle(r){}
double calcval();
};
double Sphere::calcval(){
return ((4 / 3) * PI * pow(radius, 3));
}
int main()
{
Circle Circle_1, Circle_2(2); // create two Circle objects
Cylinder Cylinder_1(3, 4); // create one Cylinder object
Sphere Sphere_1(5);
cout << "The area of Circle_1 is " << Circle_1.calcval() << endl;
cout << "The area of Circle_2 is " << Circle_2.calcval() << endl;
cout << "The volume of Cylinder_1 is " << Cylinder_1.calcval() << endl;
Circle_1 = Cylinder_1; // assign a Cylinder to a Circle
cout << "\nThe area of Circle_1 is now " << Circle_1.calcval() << endl;
Circle_1 = Sphere_1;
cout << "\nThe area of Circle_1 is now " << Circle_1.calcval() << endl;
cout << "\nThe volume of Sphere_1 is " << Sphere_1.calcval() << endl;
system("PAUSE");
return 0;
}
5. (Program) a. Create a base class named Point consisting of x and y data members representing
point coordinates. From this class, derive a class named Circle with another data member
named radius. For this derived class, the x and y data members represent a circle’s center
coordinates. The member functions of the Point class should consist of a constructor, an
area() function that returns 0, and a distance() function that returns the distance between
two points, (x1, y1) and (x2, y2), where
distance =sqrt( (x - x )^2 + (y - y )^2)
Additionally, the derived class should have a constructor and an override function named
area() that returns a circle’s area.
Done!!
b. Include the classes constructed for Exercise 5a in a working C++ program. Have your program
call all the member functions in each class. In addition, call the base class’s
distance() function with two Circle objects and explain the result this function returns.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 2.0 * asin(1.0);
// class declaration section
class Point
{
protected:
double x1;
double x2;
double y1;
double y2;
public:
Point(double = 14.0, double = 18.0, double = 19.0, double = 31.0); // constructor
double distance();
};
// class implementation section for Point
Point::Point(double xx, double yy, double yy2, double xx2) // constructor
{
x1 = xx;
y1 = yy;
y2 = yy2;
x2 = xx2;
}
// calculate the area of a Point
double Point::distance()
{
double distance = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
return distance;
}
// class declaration section where Cylinder is derived from Point
class Circle : public Point
{
protected:
double radius; // add one data member and
public: // two member functions
Circle(double r = 1.0) : Point(r) {}
double area();
};
// class implementation section for Cylinder
double Circle::area() // calculates a volume
{
double area = PI * pow(radius, 2);
return area; // note the base function call
}
int main()
{
Point Point_1, Point_2(2); // create two Point objects
Circle Circle_1(4),Circle_2(2); // create one Cylinder object
cout << "The distance of Point_1 is " << Point_1.distance() << endl;
cout << "The area of Circle_1 is " << Circle_1.area() << endl;
Circle_2 = Circle_1.distance();
cout << Circle_2.distance() << endl;
system("PAUSE");
return 0;
}
*The distance that is returned replaced all unfilled requirments with 0 and then prints the formula required.
6. (Modify) a. Using the classes constructed for Exercise 5a, derive a class named Cylinder
from the Circle class. The Cylinder class should have a constructor and a member function
named area() that determines a cylinder’s surface area. For this function, use the algorithm
surface area = 2 π r (l + r), where r is the radius of the cylinder and l is the length.
Done!!
b. Include the classes constructed for Exercise 6a in a working C++ program. Have your program
call all the member functions in the Cylinder class.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 2.0 * asin(1.0);
// class declaration section
class Point
{
protected:
double x1;
double x2;
double y1;
double y2;
public:
Point(double = 14.0, double = 18.0, double = 19.0, double = 31.0); // constructor
double distance();
};
// class implementation section for Point
Point::Point(double xx, double yy, double yy2, double xx2) // constructor
{
x1 = xx;
y1 = yy;
y2 = yy2;
x2 = xx2;
}
// calculate the area of a Point
double Point::distance()
{
double distance = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
return distance;
}
// class declaration section where Cylinder is derived from Point
class Circle : public Point
{
protected:
double radius; // add one data member and
public: // two member functions
Circle(double r = 1.0) : Point(r) {}
double area();
};
// class implementation section for Cylinder
double Circle::area() // calculates a volume
{
double area = PI * pow(radius, 2);
return area; // note the base function call
}
class Cylinder : public Circle
{
protected:
double length;
public:
Cylinder(double r = 1.0, double l = 1.0) : Circle(r), length(l) {}
double surf_area();
};
double Cylinder::surf_area()
{
double surface_area = 2 * PI * radius *(radius + length);
return surface_area;
}
int main()
{
Point Point_1, Point_2(2); // create two Point objects
Circle Circle_1(4),Circle_2(2); // create one Circle object
Cylinder Cylinder_1,Cylinder_2;
cout << "The distance of Point_1 is " << Point_1.distance() << endl;
cout << "The area of Circle_1 is " << Circle_1.area() << endl;
Circle_2 = Circle_1.distance();
cout << Circle_2.distance() << endl;
cout << "The suface area of Cylinder_1 is " << endl;
Cylinder_1 = Cylinder_2.area();
cout << Cylinder_1.area()<< endl;
system("PAUSE");
return 0;
}
c. What do you think the result might be if the Point (base) class’s distance() function is
called with two Cylinder objects?
The function will behave the same but will have access to the Point (base) class's distance() function.
7. (Program) a. Create a base class named Rectangle containing length and width data members.
From this class, derive a class named Box with another data member named depth. The
member functions of the base Rectangle class should consist of a constructor and an area()
function. The derived Box class should have a constructor, a volume() function, and an override
function named area() that returns the surface area of the box.
Done!!
b. Include the classes constructed for Exercise 7a in a working C++ program. Have your program
call all the member functions in each class, and verify the results manually.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 2.0 * asin(1.0);
// class declaration section
class Rectangle
{
protected:
double length;
double width;
public:
Rectangle(double = 4.0, double = 8.0); // constructor
double area();
};
// class implementation section for Point
Rectangle::Rectangle(double ll, double ww) // constructor
{
length = ll;
width = ww;
}
double Rectangle::area()
{
double area = length * width;
return area;
}
// class declaration section where Box is derived from Rectangle
class Box : public Rectangle
{
protected:
double depth; // add one data member and
public: // two member functions
Box(double d = 5.0, double l = 3.0, double w = 4.0) : Rectangle(l,w), depth(d) {}
double volume();
double surf_area();
};
// class implementation section
double Box::surf_area() // calculates a volume
{
double surf_area = (2*length*width) +(2 *length * depth) + (2*width * depth);
return surf_area; // note the base function call
}
double Box::volume()
{
double volume = length * width * depth;
return volume;
}
int main()
{
Rectangle Rect_1, Rect_2; // create two Rectangle objects
Box Box_1; // create one BOX object
cout << "The area of box 1 is " << Box_1.area() << endl;
cout << "The volume of box 1 is " << Box_1.volume() << endl;
cout << "The area of the rectangle is " << Rect_2.area() << endl;
system("PAUSE");
return 0;
}