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
|
{
"metadata": {
"name": "",
"signature": "sha256:1198072630a182533bf651767f275df9ee5758e0d781322254bb408a8b4cffaa"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Chapter 15: Virtual Functions and Polymorphism<h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 15.1, Page Number: 358<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"\n",
"class B_class:\n",
" def __init__(self):\n",
" self.author=None\n",
" def put_author(self,s):\n",
" self.author=s\n",
" def show_author(self):\n",
" print self.author\n",
" \n",
"class D_class(B_class):\n",
" def __init__(self):\n",
" self.title=None\n",
" def put_title(self,num):\n",
" self.title=num\n",
" def show_title(self):\n",
" print \"Title:\",self.title\n",
" \n",
"#Variable declaration\n",
"p=[B_class()] #acts as a pointer to B_class type\n",
"B_ob=B_class()\n",
"\n",
"dp=[D_class()] #acts as a pointer to D_class type\n",
"D_ob=D_class()\n",
"\n",
"p[0]=B_ob #assigning p to object of base\n",
"\n",
"\n",
"#Access B_class via pointer\n",
"p[0].put_author(\"Tom Clancy\")\n",
"\n",
"#Access D_class via base pointer\n",
"p[0]=D_ob\n",
"p[0].put_author(\"William Shakespeare\")\n",
"\n",
"#Show that each author went into proper object\n",
"B_ob.show_author()\n",
"D_ob.show_author()\n",
"print \"\\n\"\n",
"\n",
"#Since put_title() and show_title() are not part of the base class, \n",
"#they are not accessible via the base pointer p and must be accessed \n",
"#either directly, or, as shown here, through a pointer to the \n",
"#derived type\n",
"dp[0]=D_ob\n",
"dp[0].put_title(\"The Tempest\")\n",
"p[0].show_author()\n",
"dp[0].show_title()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Tom Clancy\n",
"William Shakespeare\n",
"\n",
"\n",
"William Shakespeare\n",
"Title: The Tempest\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 15.2, Page Number: 361<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"class base:\n",
" def who(self): #virtual function\n",
" print \"Base\"\n",
"\n",
"class first_d(base):\n",
" def who(self): #redifine who() relative to first_d\n",
" print \"First derivation\"\n",
" \n",
"class second_d(base):\n",
" def who(self): #redifine who() relative to second_d\n",
" print \"Second derivation\"\n",
" \n",
" \n",
"#Variable declaration\n",
"base_obj=base()\n",
"p=[base()]\n",
"first_obj=first_d()\n",
"second_obj=second_d()\n",
"\n",
"p[0]=base_obj\n",
"p[0].who() #access base's who\n",
"\n",
"p[0]=first_obj\n",
"p[0].who() #access first_d's who\n",
"\n",
"p[0]=second_obj\n",
"p[0].who() #access second_d's who\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Base\n",
"First derivation\n",
"Second derivation\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 15.3, Page Number: 363<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"class base:\n",
" def who(self): #virtual function\n",
" print \"Base\"\n",
"\n",
"class first_d(base):\n",
" def who(self): #redifine who() relative to first_d\n",
" print \"First derivation\"\n",
" \n",
"class second_d(base):\n",
" #who not defined\n",
" pass\n",
" \n",
" \n",
"#Variable declaration\n",
"base_obj=base()\n",
"p=[base()]\n",
"first_obj=first_d()\n",
"second_obj=second_d()\n",
"\n",
"p[0]=base_obj\n",
"p[0].who() #access base's who\n",
"\n",
"p[0]=first_obj\n",
"p[0].who() #access first_d's who\n",
"\n",
"p[0]=second_obj\n",
"p[0].who() #access base's who because\n",
" #second_d does not redefine it.\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Base\n",
"First derivation\n",
"Base\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 15.4, Page Number: 364<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"class base:\n",
" def who(self): #virtual function\n",
" print \"Base\"\n",
"\n",
"class first_d(base):\n",
" def who(self): #redifine who() relative to first_d\n",
" print \"First derivation\"\n",
" \n",
"#second_d now inherited first_d -- not base\n",
"class second_d(first_d):\n",
" #who not defined\n",
" pass\n",
" \n",
" \n",
"#Variable declaration\n",
"base_obj=base()\n",
"p=[base()]\n",
"first_obj=first_d()\n",
"second_obj=second_d()\n",
"\n",
"p[0]=base_obj\n",
"p[0].who() #access base's who\n",
"\n",
"p[0]=first_obj\n",
"p[0].who() #access first_d's who\n",
"\n",
"p[0]=second_obj\n",
"p[0].who() #access first_d's who because\n",
" #second_d does not redefine it.\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Base\n",
"First derivation\n",
"First derivation\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 15.5, Page Number: 366<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"class figure:\n",
" def __init__(self):\n",
" self._x=None\n",
" self._y=None\n",
" def set_dim(self,i,j):\n",
" self._x=i\n",
" self._y=j\n",
" def show_area(self):\n",
" print \"No area computation defined\",\n",
" print \"for this class.\"\n",
" \n",
"class triangle(figure):\n",
" def show_area(self):\n",
" print \"Triangle with height\",\n",
" print self._x,\"and base\",self._y,\n",
" print \"has an area of\",\n",
" print self._x*0.5*self._y,\".\"\n",
" \n",
"class rectangle(figure):\n",
" def show_area(self):\n",
" print \"Rectangle with dimensions\",\n",
" print self._x,\"x\",self._y,\n",
" print \"has an area of\",\n",
" print self._x*self._y,\".\"\n",
" \n",
"#Variable declaration\n",
"p=[figure()] #pointer to base type\n",
"t=triangle() #objects of derived type\n",
"r=rectangle()\n",
"\n",
"p[0]=t\n",
"p[0].set_dim(10.0,5.0)\n",
"p[0].show_area()\n",
"\n",
"p[0]=r\n",
"p[0].set_dim(10.0,5.0)\n",
"p[0].show_area()\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Triangle with height 10.0 and base 5.0 has an area of 25.0 .\n",
"Rectangle with dimensions 10.0 x 5.0 has an area of 50.0 .\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 15.6, Page Number: 368<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"class figure:\n",
" def __init__(self):\n",
" self._x=None\n",
" self._y=None\n",
" def set_dim(self,i,j=0):\n",
" self._x=i\n",
" self._y=j\n",
" def show_area(self):\n",
" print \"No area computation defined\",\n",
" print \"for this class.\"\n",
" \n",
"class triangle(figure):\n",
" def show_area(self):\n",
" print \"Triangle with height\",\n",
" print self._x,\"and base\",self._y,\n",
" print \"has an area of\",\n",
" print self._x*0.5*self._y,\".\"\n",
" \n",
"class rectangle(figure):\n",
" def show_area(self):\n",
" print \"Rectangle with dimensions\",\n",
" print self._x,\"x\",self._y,\n",
" print \"has an area of\",\n",
" print self._x*self._y,\".\"\n",
" \n",
"class circle(figure):\n",
" def show_area(self):\n",
" print \"Circle with radius\",\n",
" print self._x,\n",
" print \"has an area of\",\n",
" print 3.14*self._x*self._x,\".\"\n",
" \n",
" \n",
"#Variable declaration\n",
"p=[figure()] #pointer to base type\n",
"t=triangle() #objects of derived type\n",
"r=rectangle()\n",
"c=circle()\n",
"\n",
"p[0]=t\n",
"p[0].set_dim(10.0,5.0)\n",
"p[0].show_area()\n",
"\n",
"p[0]=r\n",
"p[0].set_dim(10.0,5.0)\n",
"p[0].show_area()\n",
"\n",
"p[0]=c\n",
"p[0].set_dim(9.0)\n",
"p[0].show_area()\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Triangle with height 10.0 and base 5.0 has an area of 25.0 .\n",
"Rectangle with dimensions 10.0 x 5.0 has an area of 50.0 .\n",
"Circle with radius 9.0 has an area of 254.34 .\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|