diff options
author | Jovina Dsouza | 2014-07-08 17:19:20 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-07-08 17:19:20 +0530 |
commit | 84eaa5fbfb3090e912ebe12de1906b7c0bdde908 (patch) | |
tree | f98362fb5e4e43848c150834c3937dc4bde1e176 /C++_from_the_Ground/Chapter_11.ipynb | |
parent | 80751050da776de062000a7d2a5b4e045bfbc9f8 (diff) | |
download | Python-Textbook-Companions-84eaa5fbfb3090e912ebe12de1906b7c0bdde908.tar.gz Python-Textbook-Companions-84eaa5fbfb3090e912ebe12de1906b7c0bdde908.tar.bz2 Python-Textbook-Companions-84eaa5fbfb3090e912ebe12de1906b7c0bdde908.zip |
adding book
Diffstat (limited to 'C++_from_the_Ground/Chapter_11.ipynb')
-rw-r--r-- | C++_from_the_Ground/Chapter_11.ipynb | 877 |
1 files changed, 877 insertions, 0 deletions
diff --git a/C++_from_the_Ground/Chapter_11.ipynb b/C++_from_the_Ground/Chapter_11.ipynb new file mode 100644 index 00000000..fdd70bcf --- /dev/null +++ b/C++_from_the_Ground/Chapter_11.ipynb @@ -0,0 +1,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": {} + } + ] +}
\ No newline at end of file |