summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_11.ipynb
blob: fdd70bcf0e324e4eead9b80e3455fd98af7319d6 (plain)
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
{
 "metadata": {
  "name": "",
  "signature": "sha256:5428ee61d548a6c3eefd868dac96cac83b29f85d9bfd7876e9904bdfe87b0fad"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 11: Introducing the Class<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.1, Page Number: 249<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class queue:\n",
      "    #Initialize the queue \n",
      "    def __init__(self):\n",
      "        self.__sloc=0\n",
      "        self.__rloc=-1\n",
      "        self.__q=[]\n",
      "        \n",
      "    #Put an integer into the queue\n",
      "    def qput(self,i):\n",
      "        if self.__sloc==100:\n",
      "            print \"Queue is full.\"\n",
      "            return\n",
      "        self.__sloc+=1\n",
      "        self.__q.append(i)\n",
      "        \n",
      "    #Get an integer from the queue\n",
      "    def qget(self):\n",
      "        if self.__rloc==self.__sloc:\n",
      "            print \"Queue underflow\"\n",
      "            return\n",
      "        self.__rloc+=1\n",
      "        return self.__q[self.__rloc]     \n",
      "    \n",
      "\n",
      "    \n",
      "#Create two queue objects\n",
      "b=queue()\n",
      "a=queue()\n",
      "\n",
      "a.qput(10)\n",
      "b.qput(19)\n",
      "\n",
      "a.qput(20)\n",
      "b.qput(1)\n",
      "\n",
      "#Result\n",
      "print \"Contents of queue a: \",\n",
      "print a.qget(),' ',a.qget()\n",
      "\n",
      "print \"Contents of queue b: \",\n",
      "print b.qget(),' ',b.qget()\n",
      "\n",
      "\n",
      "    \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Contents of queue a:  10   20\n",
        "Contents of queue b:  19   1\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.2, Page Number: 250<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "    \n",
      "class myclass:\n",
      "    def __init__(self):\n",
      "        self.__a=None  #private member\n",
      "        self.b=None    #public member\n",
      "    #public functions\n",
      "    def setlab(self,i):\n",
      "        self.__a=i      #refer to a\n",
      "        self.b=i*i    #refer to b\n",
      "        return\n",
      "    def geta(self):\n",
      "        return self.__a #refer to a\n",
      "    def reset(self):\n",
      "        #call setlab using self\n",
      "        self.setlab(0)     #the object is already known\n",
      "            \n",
      "        \n",
      "ob=myclass()\n",
      "ob.setlab(5)      #set ob.a and ob.b\n",
      "print \"ob after setlab(5): \",ob.geta(),' ',\n",
      "print ob.b #can access b because it is public\n",
      "\n",
      "ob.b=20    #can access b because it is public\n",
      "print \"ob after ob.b=20: \",\n",
      "print ob.geta(),' ',ob.b\n",
      "\n",
      "ob.reset()\n",
      "print \"ob after ob.reset(): \",ob.geta(),' ',ob.b\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "ob after setlab(5):  5   25\n",
        "ob after ob.b=20:  5   20\n",
        "ob after ob.reset():  0   0\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.3, Page Number: 254<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class queue:\n",
      "    \n",
      "    #Constructor\n",
      "    def __init__(self):      \n",
      "        self.__q=[]\n",
      "        self.__rloc=-1\n",
      "        self.__sloc=0\n",
      "        print \"Queue initialized\"\n",
      "        \n",
      "    #Destructor\n",
      "    def __del__(self):\n",
      "        print (\"Queue destroyed\")\n",
      "        \n",
      "    #Put an integer into the queue\n",
      "    def qput(self,i):\n",
      "        if self.__sloc == 100:\n",
      "            print \"Queue is full\"\n",
      "            return \n",
      "        self.__sloc+=1\n",
      "        self.__q.append(i)\n",
      "        \n",
      "    #Get an integer from the queue\n",
      "    def qget(self):\n",
      "        if self.__rloc==self.__sloc:\n",
      "            print \"Queue underflow\"\n",
      "            return\n",
      "        self.__rloc+=1\n",
      "        return self.__q[self.__rloc]\n",
      "        \n",
      "#Create two queue objects\n",
      "a=queue()\n",
      "\n",
      "\n",
      "a.qput(10)\n",
      "a.qput(20)\n",
      "b=queue()\n",
      "b.qput(19)\n",
      "\n",
      "b.qput(1)\n",
      "\n",
      "#Result\n",
      "print a.qget(),' ',\n",
      "print a.qget(),' ',\n",
      "print b.qget(),' ',\n",
      "print b.qget(),' '\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Queue initialized\n",
        "Queue destroyed\n",
        "Queue initialized\n",
        "Queue destroyed\n",
        "10   20   19   1  \n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.4, Page Number: 257<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class queue:                            \n",
      "    \n",
      "    #Constructor\n",
      "    def __init__(self,i):      \n",
      "        self.__q=[]\n",
      "        self.__rloc=-1\n",
      "        self.__sloc=0\n",
      "        self.__who=i\n",
      "        print \"Queue \",self.__who,\" initialized.\"\n",
      "        \n",
      "    #Destructor\n",
      "    def __del__():\n",
      "        print \"Queue \",self.__who,\" destroyed\"\n",
      "        \n",
      "    #Put an integer into the queue\n",
      "    def qput(self,i):\n",
      "        if self.__sloc == 100:\n",
      "            print \"Queue is full\"\n",
      "            return \n",
      "        self.__sloc+=1\n",
      "        self.__q.append(i)\n",
      "        \n",
      "    #Get an integer from the queue\n",
      "    def qget(self):\n",
      "        if self.__rloc==self.__sloc:\n",
      "            print \"Queue underflow\"\n",
      "            return\n",
      "        self.__rloc+=1\n",
      "        return self.__q[self.__rloc]\n",
      "        \n",
      "a=queue(1)\n",
      "b=queue(2)\n",
      "\n",
      "a.qput(10)\n",
      "b.qput(19)\n",
      "\n",
      "a.qput(20)\n",
      "b.qput(1)\n",
      "\n",
      "#Result\n",
      "print a.qget(),' ',\n",
      "print a.qget(),' ',\n",
      "print b.qget(),' ',\n",
      "print b.qget(),' '\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Queue  1  initialized.\n",
        "Queue destroyed\n",
        "Queue  2  initialized.\n",
        "Queue destroyed\n",
        "10   20   19   1  \n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.5, Page Number: 258<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class widget:\n",
      "    \n",
      "    #Pass two arguments to the constructor\n",
      "    def __init__(self,a,b):\n",
      "        self.__i=a\n",
      "        self.__j=b\n",
      "        \n",
      "    def put_widget(self):\n",
      "        print self.__i,\" \",self.__j\n",
      "\n",
      "#Initializing\n",
      "x=widget(10,20)\n",
      "y=widget(0,0)\n",
      "\n",
      "x.put_widget()\n",
      "y.put_widget()\n",
      "\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10   20\n",
        "0   0\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.6, Page Number: 259<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class myclass:\n",
      "    \n",
      "    #Constructor\n",
      "    def __init__(self,x):\n",
      "        self.a=x\n",
      "    #To get the vale of a\n",
      "    def get_a(self):\n",
      "        return self.a\n",
      "\n",
      "#Initializing\n",
      "ob=myclass(4)\n",
      "\n",
      "#Result\n",
      "print ob.get_a()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.7, Page Number: 260<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "from ctypes import *\n",
      "\n",
      "class c1(Structure):\n",
      "    _fields_=[(\"__i\", c_int)]  #private member\n",
      "    def get_i(self):           #public finctions\n",
      "        return self.__i\n",
      "    def put_i(self,j):\n",
      "        self.__i=j\n",
      "\n",
      "#Variable declaration\n",
      "s=c1()\n",
      "\n",
      "s.put_i(10)\n",
      "\n",
      "#Result\n",
      "print s.get_i()\n",
      "        \n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.8, Page Number: 261<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class c1:\n",
      "    def __init__(self):\n",
      "        self.__i=None   #private member\n",
      "    def get_i(self):    #public finctions\n",
      "        return self.__i\n",
      "    def put_i(self,j):\n",
      "        self.__i=j\n",
      "\n",
      "#Variable declaration\n",
      "s=c1()\n",
      "\n",
      "s.put_i(10)\n",
      "\n",
      "#Result\n",
      "print s.get_i()\n",
      "        \n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.9, Page Number: 263<h3> "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "from ctypes import *\n",
      "\n",
      "#Creates a union\n",
      "class u_type(Union):\n",
      "    _fields_ = [(\"i\",c_short),\n",
      "            (\"ch\", c_char*2)]\n",
      "    #Constructor\n",
      "    def __init__(self,a):\n",
      "        self.i=a\n",
      "        \n",
      "    #Show the characters that comprise a short int.\n",
      "    def showchars(self):\n",
      "        print self.ch[0]\n",
      "        print self.ch[1]\n",
      "        \n",
      "        \n",
      "u=u_type(1000)\n",
      "\n",
      "#Displays char of 1000\n",
      "u.showchars()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\ufffd\n",
        "\u0003\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.10, Page Number: 264<h3> "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class c1:\n",
      "    def __init__(self):\n",
      "        self.__i=None\n",
      "    def get_i(self):\n",
      "        return self.i\n",
      "    def put_i(self,j):\n",
      "        self.i=j\n",
      "\n",
      "#Variable declaration\n",
      "s=c1()\n",
      "\n",
      "s.put_i(10)\n",
      "\n",
      "#Result\n",
      "print s.get_i()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.11, Page Number: 265<h3> "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class c1:\n",
      "    def __init__(self):\n",
      "        self.__i=None\n",
      "    def get_i(self):\n",
      "        return self.i\n",
      "    def put_i(self,j):\n",
      "        self.i=j\n",
      "\n",
      "#Variable declaration\n",
      "s=c1()\n",
      "\n",
      "s.put_i(10)\n",
      "\n",
      "#Result\n",
      "print s.get_i()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.12, Page Number: 267<h3> "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class display:\n",
      "    def __init__(self):\n",
      "        width=None\n",
      "        height=None\n",
      "        res=None\n",
      "    def set_dim(self,w,h):\n",
      "        self.width=w\n",
      "        self.height=h\n",
      "    def get_dim(self):\n",
      "        return self.width,self.height\n",
      "    def set_res(self,r):\n",
      "        self.res=r\n",
      "    def get_res(self):\n",
      "        return self.res\n",
      "        \n",
      "#Variable decleration\n",
      "names=[\"low\",\"medium\",\"high\"]   \n",
      "(low,medium,high)=(0,1,2)    #For enumeration type\n",
      "w=None\n",
      "h=None\n",
      "display_mode=[]*3\n",
      "\n",
      "for i in range(3):\n",
      "    display_mode.append(display())\n",
      "\n",
      "#Initialize the array of objects using member functions\n",
      "display_mode[0].set_res(low)\n",
      "display_mode[0].set_dim(640,480)\n",
      "\n",
      "display_mode[1].set_res(medium)\n",
      "display_mode[1].set_dim(800,600)\n",
      "\n",
      "display_mode[2].set_res(high)\n",
      "display_mode[2].set_dim(1600,1200)\n",
      "\n",
      "#Result\n",
      "print \"Available display modes: \"\n",
      "for i in range(3):\n",
      "    print names[display_mode[i].get_res()],\" : \",\n",
      "    w,h=display_mode[i].get_dim()\n",
      "    print w,\" by \",h"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Available display modes: \n",
        "low  :  640  by  480\n",
        "medium  :  800  by  600\n",
        "high  :  1600  by  1200\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.13, Page Number: 268<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class samp:\n",
      "    __a=None\n",
      "    def __init__(self,n):\n",
      "        self.__a=n\n",
      "    def get_a(self):\n",
      "        return self.__a\n",
      "\n",
      "#Initializing the list\n",
      "sampArray=[samp(-1),samp(-2),samp(-3),samp(-4)]\n",
      "\n",
      "#Display\n",
      "for i in range(4):\n",
      "    print sampArray[i].get_a(),' ',"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "-1   -2   -3   -4  \n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.14, Page Number: 269<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class samp:\n",
      "    __a=None\n",
      "    __b=None\n",
      "    def __init__(self,n,m):\n",
      "        self.__a=n\n",
      "        self.__b=m\n",
      "    def get_a(self):\n",
      "        return self.__a\n",
      "    def get_b(self):\n",
      "        return self.__b\n",
      " \n",
      "#Initializing the list\n",
      "sampArray=[[samp(1,2),samp(3,4)],\n",
      "           [samp(5,6),samp(7,8)],\n",
      "           [samp(9,10),samp(11,12)],\n",
      "           [samp(13,14),samp(15,16)]]\n",
      "\n",
      "#Display\n",
      "for i in range(4):\n",
      "    print sampArray[i][0].get_a(),' ',\n",
      "    print sampArray[i][0].get_b()\n",
      "    print sampArray[i][1].get_a(),' ',\n",
      "    print sampArray[i][1].get_b()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1   2\n",
        "3   4\n",
        "5   6\n",
        "7   8\n",
        "9   10\n",
        "11   12\n",
        "13   14\n",
        "15   16\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.15, Page Number: 270<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from ctypes import *\n",
      "\n",
      "class P_example(Structure):\n",
      "    __num=None\n",
      "    def set_num(self,val):\n",
      "        self.__num=val\n",
      "    def show_num(self):\n",
      "        print self.__num\n",
      "\n",
      "#Variable declaration\n",
      "ob=P_example()          #Declare an object to the structure\n",
      "p=POINTER(P_example)    #Declare a pointer to the structure\n",
      "\n",
      "ob.set_num(1)           #access ob directly\n",
      "ob.show_num()\n",
      "\n",
      "p=ob                    #assign p the address of ob\n",
      "p.show_num()            #access ob using pointer\n",
      "        \n",
      "        "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n",
        "1\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 11.16, Page Number: 271<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class P_example(Structure):\n",
      "    __num=None\n",
      "    def set_num(self,val):\n",
      "        self.__num=val\n",
      "    def show_num(self):\n",
      "        print self.__num\n",
      "        \n",
      "#Variable declaration\n",
      "ob=[P_example(),P_example()]          #Declare an object to the structure\n",
      "p=POINTER(P_example)                  #Declare a pointer to the structure\n",
      "\n",
      "ob[0].set_num(10)                     #access objects directly\n",
      "ob[1].set_num(20)\n",
      "\n",
      "p=ob                             #obtain pointer to first element\n",
      "p[0].show_num()                  #access ob using pointer\n",
      "\n",
      "p[1].show_num()\n",
      "\n",
      "p[0].show_num()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10\n",
        "20\n",
        "10\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}