diff options
Diffstat (limited to 'C++_from_the_Ground/Chapter_12(1).ipynb')
-rw-r--r-- | C++_from_the_Ground/Chapter_12(1).ipynb | 648 |
1 files changed, 593 insertions, 55 deletions
diff --git a/C++_from_the_Ground/Chapter_12(1).ipynb b/C++_from_the_Ground/Chapter_12(1).ipynb index abad8d81..f92ff06c 100644 --- a/C++_from_the_Ground/Chapter_12(1).ipynb +++ b/C++_from_the_Ground/Chapter_12(1).ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "Chapter 12" + "name": "", + "signature": "sha256:16b30eb675083837fc31b76aa8f3c6bd5549baef25d739f4f8bead7d865cbcbf" }, "nbformat": 3, "nbformat_minor": 0, @@ -10,24 +11,51 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h1>Chapter 12: A Closer Look at Classes<h1>" + "source": [ + "<h1>Chapter 12: A Closer Look at Classes<h1>" + ] }, { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.1, Page Number: 274<h3>" + "source": [ + "<h3>Example 12.1, Page Number: 274<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementation of friend functions in python'''\n\nclass myclass:\n __a=None\n __b=None\n def __init__(self,i,j):\n self.__a=i\n self.__b=j\n def sum(self,x): #Friend function\n return sum1(x)\n \ndef sum1(x): \n return x._myclass__a +x._myclass__b #accessing private members\n\n#Variable declaration\nn=myclass(3,4)\n\n#Result\nprint n.sum(n)\n\n", + "input": [ + "\n", + "\n", + "class myclass:\n", + " __a=None\n", + " __b=None\n", + " def __init__(self,i,j):\n", + " self.__a=i\n", + " self.__b=j\n", + " def sum(self,x): #Friend function\n", + " return sum1(x)\n", + " \n", + "def sum1(x): \n", + " return x._myclass__a +x._myclass__b #accessing private members\n", + "\n", + "#Variable declaration\n", + "n=myclass(3,4)\n", + "\n", + "#Result\n", + "print n.sum(n)\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "7\n" + "text": [ + "7\n" + ] } ], "prompt_number": 1 @@ -35,19 +63,65 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.2, Page Number: 275<h3>" + "source": [ + "<h3>Example 12.2, Page Number: 275<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Use a friend function'''\n\nclass c1:\n __status=None\n def set_status(self,state):\n self.__status=state\n \nclass c2:\n __status=None\n def set_status(self,state):\n self.status=state\n \n#Friend function \ndef idle(a,b):\n if a._c1__status or b._c2__status :\n return 0\n else:\n return 1\n \n#variable declarations\ndef IDLE(): #Constants\n return 0\ndef INUSE():\n return 1\nx=c1()\ny=c2()\n\nx.set_status(IDLE())\ny.set_status(IDLE())\n\nif idle(x,y):\n print \"Screen Can Be Used.\"\n \nx.set_status(INUSE())\n\nif idle(x,y):\n print \"Screen Can Be Used.\"\nelse:\n print \"Pop-up In Use.\"\n \n ", + "input": [ + "\n", + "class c1:\n", + " __status=None\n", + " def set_status(self,state):\n", + " self.__status=state\n", + " \n", + "class c2:\n", + " __status=None\n", + " def set_status(self,state):\n", + " self.status=state\n", + " \n", + "#Friend function \n", + "def idle(a,b):\n", + " if a._c1__status or b._c2__status :\n", + " return 0\n", + " else:\n", + " return 1\n", + " \n", + "#variable declarations\n", + "def IDLE(): #Constants\n", + " return 0\n", + "def INUSE():\n", + " return 1\n", + "x=c1()\n", + "y=c2()\n", + "\n", + "x.set_status(IDLE())\n", + "y.set_status(IDLE())\n", + "\n", + "if idle(x,y):\n", + " print \"Screen Can Be Used.\"\n", + " \n", + "x.set_status(INUSE())\n", + "\n", + "if idle(x,y):\n", + " print \"Screen Can Be Used.\"\n", + "else:\n", + " print \"Pop-up In Use.\"\n", + " \n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Screen Can Be Used.\nPop-up In Use.\n" + "text": [ + "Screen Can Be Used.\n", + "Pop-up In Use.\n" + ] } ], "prompt_number": 2 @@ -55,19 +129,64 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.3, Page Number: 277<h3>" + "source": [ + "<h3>Example 12.3, Page Number: 277<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''A function can be member of one class and a friend of another'''\n\n #Constants\ndef IDLE(): \n return 0\ndef INUSE():\n return 1\n\nclass c1:\n __status=None\n def set_status(self,state):\n self.__status=state\n def idle(self,b): #now a member of c1\n if self.__status or b._c2__status :\n return 0\n else:\n return 1\n \nclass c2:\n __status=None #IDLE if off INUSE if on screen\n def set_status(self,state):\n self.status=state\n \n#Variable declarations \nx=c1()\ny=c2()\n\nx.set_status(IDLE())\ny.set_status(IDLE())\n\nif idle(x,y):\n print \"Screen Can Be Used.\"\n \nx.set_status(INUSE())\n\nif idle(x,y):\n print \"Screen Can Be Used.\"\nelse:\n print \"Pop-up In Use.\"\n ", + "input": [ + "\n", + "\n", + "def IDLE(): \n", + " return 0\n", + "def INUSE():\n", + " return 1\n", + "\n", + "class c1:\n", + " __status=None\n", + " def set_status(self,state):\n", + " self.__status=state\n", + " def idle(self,b): #now a member of c1\n", + " if self.__status or b._c2__status :\n", + " return 0\n", + " else:\n", + " return 1\n", + " \n", + "class c2:\n", + " __status=None #IDLE if off INUSE if on screen\n", + " def set_status(self,state):\n", + " self.status=state\n", + " \n", + "#Variable declarations \n", + "x=c1()\n", + "y=c2()\n", + "\n", + "x.set_status(IDLE())\n", + "y.set_status(IDLE())\n", + "\n", + "if idle(x,y):\n", + " print \"Screen Can Be Used.\"\n", + " \n", + "x.set_status(INUSE())\n", + "\n", + "if idle(x,y):\n", + " print \"Screen Can Be Used.\"\n", + "else:\n", + " print \"Pop-up In Use.\"\n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Screen Can Be Used.\nPop-up In Use.\n" + "text": [ + "Screen Can Be Used.\n", + "Pop-up In Use.\n" + ] } ], "prompt_number": 4 @@ -75,29 +194,70 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.4, Page Number: 278<h3>" + "source": [ + "<h3>Example 12.4, Page Number: 278<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Use of overloaded constructors'''\n#Printing the time passed since the functon started instead of ringing the bell.\n\nimport time,string\n\nclass timer:\n __seconds=None\n \n def __init__(self,t1,t2=None):\n if t2==None:\n if isinstance(t1,int): #seconds specified as an integer\n self.__seconds=t1\n else: #seconds specified as a string\n self.__seconds=string.atoi(t1)\n else: #time in minutes and seconds\n self.__seconds=t1*60+t2\n \n def run(self):\n t1=time.clock()\n while (time.clock()-t1)<self.__seconds:\n a=10\n print time.clock()-t1\n \n \na=timer(10)\nb=timer(\"20\")\nc=timer(1,10)\na.run() #count 10 seconds\nb.run() #count 20 seconds\nc.run() #count 1 minute,10 seconds\n ", + "input": [ + "\n", + "\n", + "import time,string\n", + "\n", + "class timer:\n", + " __seconds=None\n", + " \n", + " def __init__(self,t1,t2=None):\n", + " if t2==None:\n", + " if isinstance(t1,int): #seconds specified as an integer\n", + " self.__seconds=t1\n", + " else: #seconds specified as a string\n", + " self.__seconds=string.atoi(t1)\n", + " else: #time in minutes and seconds\n", + " self.__seconds=t1*60+t2\n", + " \n", + " def run(self):\n", + " t1=time.clock()\n", + " while (time.clock()-t1)<self.__seconds:\n", + " a=10\n", + " print time.clock()-t1\n", + " \n", + " \n", + "a=timer(10)\n", + "b=timer(\"20\")\n", + "c=timer(1,10)\n", + "a.run() #count 10 seconds\n", + "b.run() #count 20 seconds\n", + "c.run() #count 1 minute,10 seconds\n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "10.0000009774\n20.0000009774" + "text": [ + "10.0000009774\n", + "20.0000009774" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\n70.0000004887" + "text": [ + "\n", + "70.0000004887" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\n" + "text": [ + "\n" + ] } ], "prompt_number": 1 @@ -105,29 +265,77 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.5, Page Number: 280<h3>" + "source": [ + "<h3>Example 12.5, Page Number: 280<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Demonstarate dynamic initialization'''\n#Printing the time passed since the functon started instead of ringing the bell.\n\nimport time,string\n\nclass timer:\n __seconds=None\n \n def __init__(self,t1,t2=None):\n if t2==None:\n if isinstance(t1,int): #seconds specified as an integer\n self.__seconds=t1\n else: #seconds specified as a string\n self.__seconds=string.atoi(t1)\n else: #time in minutes and seconds\n self.__seconds=t1*60+t2\n \n def run(self):\n t1=time.clock()\n while (time.clock()-t1)<self.__seconds:\n a=10\n print time.clock()-t1\n \na=timer(10)\na.run()\n\nprint \"Enter number of seconds: \"\nstr=\"20\"\nb=timer(str) #initialize at the run time\nc.run()\n\nprint \"Enter minutes and seconds: \"\nmin=1\nsec=10\nc=timer(min,sec) #initialize at the run time\nc.run()\n ", + "input": [ + "\n", + "import time,string\n", + "\n", + "class timer:\n", + " __seconds=None\n", + " \n", + " def __init__(self,t1,t2=None):\n", + " if t2==None:\n", + " if isinstance(t1,int): #seconds specified as an integer\n", + " self.__seconds=t1\n", + " else: #seconds specified as a string\n", + " self.__seconds=string.atoi(t1)\n", + " else: #time in minutes and seconds\n", + " self.__seconds=t1*60+t2\n", + " \n", + " def run(self):\n", + " t1=time.clock()\n", + " while (time.clock()-t1)<self.__seconds:\n", + " a=10\n", + " print time.clock()-t1\n", + " \n", + "a=timer(10)\n", + "a.run()\n", + "\n", + "print \"Enter number of seconds: \"\n", + "str=\"20\"\n", + "b=timer(str) #initialize at the run time\n", + "c.run()\n", + "\n", + "print \"Enter minutes and seconds: \"\n", + "min=1\n", + "sec=10\n", + "c=timer(min,sec) #initialize at the run time\n", + "c.run()\n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "10.0000004887\nEnter number of seconds: \n70.0000009774" + "text": [ + "10.0000004887\n", + "Enter number of seconds: \n", + "70.0000009774" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\nEnter minutes and seconds: \n70.0000009774" + "text": [ + "\n", + "Enter minutes and seconds: \n", + "70.0000009774" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\n" + "text": [ + "\n" + ] } ], "prompt_number": 2 @@ -135,19 +343,66 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.6, Page Number: 282<h3>" + "source": [ + "<h3>Example 12.6, Page Number: 282<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Demonstrate object assignment'''\n\nclass myclass:\n __a=None #private members\n __b=None\n def setab(self,i,j): #publc functons\n self.__a=i\n self.__b=j\n def showab(self):\n print \"a is \",self.__a\n print \"b is \",self.__b\n\n#Variable declaration\nob1 = myclass()\nob2 = myclass()\n\n#Intalizing\nob1.setab(10,20)\nob2.setab(0,0)\n\nprint \"ob1 before assignment: \"\nob1.showab()\nprint \"ob2 before assignment: \"\nob2.showab()\n\nob2 = ob1 #assign ob1 to ob2\n\n#Result\nprint \"ob1 after assignment: \"\nob1.showab()\nprint \"ob2 after assignment: \"\nob2.showab()\n", + "input": [ + "\n", + "class myclass:\n", + " __a=None #private members\n", + " __b=None\n", + " def setab(self,i,j): #publc functons\n", + " self.__a=i\n", + " self.__b=j\n", + " def showab(self):\n", + " print \"a is \",self.__a\n", + " print \"b is \",self.__b\n", + "\n", + "#Variable declaration\n", + "ob1 = myclass()\n", + "ob2 = myclass()\n", + "\n", + "#Intalizing\n", + "ob1.setab(10,20)\n", + "ob2.setab(0,0)\n", + "\n", + "print \"ob1 before assignment: \"\n", + "ob1.showab()\n", + "print \"ob2 before assignment: \"\n", + "ob2.showab()\n", + "\n", + "ob2 = ob1 #assign ob1 to ob2\n", + "\n", + "#Result\n", + "print \"ob1 after assignment: \"\n", + "ob1.showab()\n", + "print \"ob2 after assignment: \"\n", + "ob2.showab()\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "ob1 before assignment: \na is 10\nb is 20\nob2 before assignment: \na is 0\nb is 0\nob1 after assignment: \na is 10\nb is 20\nob2 after assignment: \na is 10\nb is 20\n" + "text": [ + "ob1 before assignment: \n", + "a is 10\n", + "b is 20\n", + "ob2 before assignment: \n", + "a is 0\n", + "b is 0\n", + "ob1 after assignment: \n", + "a is 10\n", + "b is 20\n", + "ob2 after assignment: \n", + "a is 10\n", + "b is 20\n" + ] } ], "prompt_number": 1 @@ -155,19 +410,44 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.7, Page Number: 283<h3>" + "source": [ + "<h3>Example 12.7, Page Number: 283<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Demonstration of passing objects to functions'''\n'''Implementing call by value method in python'''\n\nfrom copy import deepcopy\n \nclass OBJ:\n def set_i(self,x):\n self.__i=x\n def out_i(self):\n print self.__i,\n \ndef f(x):\n x=deepcopy(x)\n x.out_i() #outputs 10\n x.set_i(100) #this affects only local copy\n x.out_i() #outputs 100\n \n#Variable declaration\no=OBJ()\no.set_i(10)\nf(o) \no.out_i() #still outputs 10, value of i unchanged\n", + "input": [ + "\n", + "from copy import deepcopy\n", + " \n", + "class OBJ:\n", + " def set_i(self,x):\n", + " self.__i=x\n", + " def out_i(self):\n", + " print self.__i,\n", + " \n", + "def f(x):\n", + " x=deepcopy(x)\n", + " x.out_i() #outputs 10\n", + " x.set_i(100) #this affects only local copy\n", + " x.out_i() #outputs 100\n", + " \n", + "#Variable declaration\n", + "o=OBJ()\n", + "o.set_i(10)\n", + "f(o) \n", + "o.out_i() #still outputs 10, value of i unchanged\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "10 100 10\n" + "text": [ + "10 100 10\n" + ] } ], "prompt_number": 3 @@ -175,19 +455,44 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.8, Page Number: 284<h3> " + "source": [ + "<h3>Example 12.8, Page Number: 284<h3> " + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Constructors, destructors, and passing objects'''\n\nclass myclass: \n def __init__(self,i):\n self.__val=i\n print \"Constructing\"\n def __del__(self):\n print \"Destructing\"\n def getval(self):\n return self.__val\n \ndef display(ob):\n print ob.getval()\n\n#Varable declaration\na=myclass(10)\n\ndisplay(a)\n", + "input": [ + "\n", + "\n", + "class myclass: \n", + " def __init__(self,i):\n", + " self.__val=i\n", + " print \"Constructing\"\n", + " def __del__(self):\n", + " print \"Destructing\"\n", + " def getval(self):\n", + " return self.__val\n", + " \n", + "def display(ob):\n", + " print ob.getval()\n", + "\n", + "#Varable declaration\n", + "a=myclass(10)\n", + "\n", + "display(a)\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Constructing\nDestructing\n10\n" + "text": [ + "Constructing\n", + "Destructing\n", + "10\n" + ] } ], "prompt_number": 2 @@ -195,19 +500,46 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.9, Page Number: 286<h3>" + "source": [ + "<h3>Example 12.9, Page Number: 286<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Passing objects with a pointer'''\n#The problem shown in C++, will not occur in python because it does not have pointers.\n\nfrom ctypes import *\n\nclass myclass:\n def __init__(self,i):\n print \"Allocating p\"\n self.p=pointer(c_int(i))\n def __del__(self):\n print \"Freeing p\"\n def getval(self):\n return self.p[0]\n \ndef display(ob):\n print ob.getval()\n\n#Variable declaration\na=myclass(10)\n\ndisplay(a)\n ", + "input": [ + "\n", + "\n", + "from ctypes import *\n", + "\n", + "class myclass:\n", + " def __init__(self,i):\n", + " print \"Allocating p\"\n", + " self.p=pointer(c_int(i))\n", + " def __del__(self):\n", + " print \"Freeing p\"\n", + " def getval(self):\n", + " return self.p[0]\n", + " \n", + "def display(ob):\n", + " print ob.getval()\n", + "\n", + "#Variable declaration\n", + "a=myclass(10)\n", + "\n", + "display(a)\n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Allocating p\n10\n" + "text": [ + "Allocating p\n", + "10\n" + ] } ], "prompt_number": 4 @@ -215,19 +547,47 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.10, Page Number: 287<h3>" + "source": [ + "<h3>Example 12.10, Page Number: 287<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Passing objects by reference'''\n\nfrom ctypes import *\nclass myclass:\n def __init__(self,i):\n print \"Allocating p\"\n self.p=pointer(c_int(i))\n def __del__(self):\n print \"Freeing p\"\n def getval(self):\n return self.p[0]\n \ndef display(ob):\n print ob[0].getval()\n\n#Variable declaration\na=[]\na.append(myclass(10))\n\ndisplay(a)\n ", + "input": [ + "'''Passing objects by reference'''\n", + "\n", + "from ctypes import *\n", + "class myclass:\n", + " def __init__(self,i):\n", + " print \"Allocating p\"\n", + " self.p=pointer(c_int(i))\n", + " def __del__(self):\n", + " print \"Freeing p\"\n", + " def getval(self):\n", + " return self.p[0]\n", + " \n", + "def display(ob):\n", + " print ob[0].getval()\n", + "\n", + "#Variable declaration\n", + "a=[]\n", + "a.append(myclass(10))\n", + "\n", + "display(a)\n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Freeing p\nAllocating p\n10\n" + "text": [ + "Freeing p\n", + "Allocating p\n", + "10\n" + ] } ], "prompt_number": 5 @@ -235,19 +595,48 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.11, Page Number: 288<h3>" + "source": [ + "<h3>Example 12.11, Page Number: 288<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Returning an object'''\n\nclass sample:\n __s=None\n def show(self):\n print self.__s\n def set(self,str):\n self.__s=str\n\n#Return an object of type sample\ndef input():\n str=sample()\n instr = \"Hello\" #User input\n str.set(instr)\n return str\n\n#Variable declaration\nob=sample()\n\n#assign returned object to ob\nob=input()\n\n#Result\nob.show()\n", + "input": [ + "\n", + "\n", + "class sample:\n", + " __s=None\n", + " def show(self):\n", + " print self.__s\n", + " def set(self,str):\n", + " self.__s=str\n", + "\n", + "#Return an object of type sample\n", + "def input():\n", + " str=sample()\n", + " instr = \"Hello\" #User input\n", + " str.set(instr)\n", + " return str\n", + "\n", + "#Variable declaration\n", + "ob=sample()\n", + "\n", + "#assign returned object to ob\n", + "ob=input()\n", + "\n", + "#Result\n", + "ob.show()\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Hello\n" + "text": [ + "Hello\n" + ] } ], "prompt_number": 6 @@ -255,19 +644,56 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.12, Page Number: 289<h3>" + "source": [ + "<h3>Example 12.12, Page Number: 289<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Returning an object'''\n#The error shown in C++ doesnt occur here.\nclass sample:\n __s=None\n def __init__(self):\n self.__s=0\n def __del__(self): \n print \"Freeing p\"\n def show(self):\n print self.__s\n def set(self,str):\n self.__s=str\n \n#This function takes one object parameter\ndef input():\n str=sample()\n instr=\"Hello\" #User input\n str.set(instr)\n return str\n\n#Variable declaration\nob=sample()\n\n#assign returned object to ob\nob=input()\n\n#Result\nob.show()\n\n\n", + "input": [ + "\n", + "#\n", + "class sample:\n", + " __s=None\n", + " def __init__(self):\n", + " self.__s=0\n", + " def __del__(self): \n", + " print \"Freeing p\"\n", + " def show(self):\n", + " print self.__s\n", + " def set(self,str):\n", + " self.__s=str\n", + " \n", + "#This function takes one object parameter\n", + "def input():\n", + " str=sample()\n", + " instr=\"Hello\" #User input\n", + " str.set(instr)\n", + " return str\n", + "\n", + "#Variable declaration\n", + "ob=sample()\n", + "\n", + "#assign returned object to ob\n", + "ob=input()\n", + "\n", + "#Result\n", + "ob.show()\n", + "\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Freeing p\nFreeing p\nHello\n" + "text": [ + "Freeing p\n", + "Freeing p\n", + "Hello\n" + ] } ], "prompt_number": 8 @@ -275,19 +701,52 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.13, Page Number: 292<h3>" + "source": [ + "<h3>Example 12.13, Page Number: 292<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementing a copy constructor'''\n#Copy construcor doesnt work in this example, it works only when explicitly called\n\nclass myclass:\n __p=None\n def __init__(self,i):\n if isinstance(i,int):\n print \"Allocating p\"\n self.__p=i\n else:\n print \"Copy constructor called\"\n self.__p=i.getval()\n def __del__(self): \n print \"Freeing p\"\n def getval(self):\n return self.__p\n \n#This function takes one object parameter\ndef display(ob):\n print ob.getval()\n\n#Variable declaration\nob=myclass(10)\n\n#Result\ndisplay(ob)\n\n\n", + "input": [ + "\n", + "\n", + "class myclass:\n", + " __p=None\n", + " def __init__(self,i):\n", + " if isinstance(i,int):\n", + " print \"Allocating p\"\n", + " self.__p=i\n", + " else:\n", + " print \"Copy constructor called\"\n", + " self.__p=i.getval()\n", + " def __del__(self): \n", + " print \"Freeing p\"\n", + " def getval(self):\n", + " return self.__p\n", + " \n", + "#This function takes one object parameter\n", + "def display(ob):\n", + " print ob.getval()\n", + "\n", + "#Variable declaration\n", + "ob=myclass(10)\n", + "\n", + "#Result\n", + "display(ob)\n", + "\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Allocating p\n10\n" + "text": [ + "Allocating p\n", + "10\n" + ] } ], "prompt_number": 1 @@ -295,19 +754,50 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.14, Page Number: 294<h3>" + "source": [ + "<h3>Example 12.14, Page Number: 294<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementing a copy constructor'''\n\n\nclass myclass:\n __p=None\n def __init__(self,i):\n if isinstance(i,int):\n print \"Allocating p\"\n self.__p=i\n else:\n print \"Copy constructor called\"\n self.__p=i.getval()\n def __del__(self): \n print \"Freeing p\"\n def getval(self):\n return self.__p\n \n\n#Variable declaration\na=myclass(10) #calls normal constructor\nb=myclass(a) #calls copy constructor\n\n\n\n\n", + "input": [ + "\n", + "\n", + "\n", + "class myclass:\n", + " __p=None\n", + " def __init__(self,i):\n", + " if isinstance(i,int):\n", + " print \"Allocating p\"\n", + " self.__p=i\n", + " else:\n", + " print \"Copy constructor called\"\n", + " self.__p=i.getval()\n", + " def __del__(self): \n", + " print \"Freeing p\"\n", + " def getval(self):\n", + " return self.__p\n", + " \n", + "\n", + "#Variable declaration\n", + "a=myclass(10) #calls normal constructor\n", + "b=myclass(a) #calls copy constructor\n", + "\n", + "\n", + "\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Allocating p\nCopy constructor called\n" + "text": [ + "Allocating p\n", + "Copy constructor called\n" + ] } ], "prompt_number": 4 @@ -315,19 +805,45 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.15, Page Number: 295<h3>" + "source": [ + "<h3>Example 12.15, Page Number: 295<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementing a copy constructor'''\n\n\nclass myclass:\n def __init__(self,i=0):\n if isinstance(i,int):\n print \"Normal constructor\"\n else:\n print \"Copy constructor\"\n\n\n#Variable declaration\na=myclass() #calls normal constructor\n\nf=myclass()\na=myclass(f) #Invoke copyconstructor\n\n\n\n\n", + "input": [ + "\n", + "\n", + "class myclass:\n", + " def __init__(self,i=0):\n", + " if isinstance(i,int):\n", + " print \"Normal constructor\"\n", + " else:\n", + " print \"Copy constructor\"\n", + "\n", + "\n", + "#Variable declaration\n", + "a=myclass() #calls normal constructor\n", + "\n", + "f=myclass()\n", + "a=myclass(f) #Invoke copyconstructor\n", + "\n", + "\n", + "\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Normal constructor\nNormal constructor\nCopy constructor\n" + "text": [ + "Normal constructor\n", + "Normal constructor\n", + "Copy constructor\n" + ] } ], "prompt_number": 8 @@ -335,19 +851,41 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 12.16, Page Number: 297<h3>" + "source": [ + "<h3>Example 12.16, Page Number: 297<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementation of the this pointer'''\n#Here self works as this\n\nclass c1:\n def __init__(self):\n self.__i=None\n def load_i(self,val):\n self.__i=val\n def get_i(self):\n return self.__i\n \n#Variable declaration \no=c1()\n\no.load_i(100)\n\n#Result\nprint o.get_i()\n", + "input": [ + "\n", + "\n", + "class c1:\n", + " def __init__(self):\n", + " self.__i=None\n", + " def load_i(self,val):\n", + " self.__i=val\n", + " def get_i(self):\n", + " return self.__i\n", + " \n", + "#Variable declaration \n", + "o=c1()\n", + "\n", + "o.load_i(100)\n", + "\n", + "#Result\n", + "print o.get_i()\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "100\n" + "text": [ + "100\n" + ] } ], "prompt_number": 13 @@ -355,7 +893,7 @@ { "cell_type": "code", "collapsed": false, - "input": "", + "input": [], "language": "python", "metadata": {}, "outputs": [] |