diff options
author | debashisdeb | 2014-06-20 15:42:42 +0530 |
---|---|---|
committer | debashisdeb | 2014-06-20 15:42:42 +0530 |
commit | 83c1bfceb1b681b4bb7253b47491be2d8b2014a1 (patch) | |
tree | f54eab21dd3d725d64a495fcd47c00d37abed004 /C++_from_the_Ground/Chapter_13(1).ipynb | |
parent | a78126bbe4443e9526a64df9d8245c4af8843044 (diff) | |
download | Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.gz Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.bz2 Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.zip |
removing problem statements
Diffstat (limited to 'C++_from_the_Ground/Chapter_13(1).ipynb')
-rw-r--r-- | C++_from_the_Ground/Chapter_13(1).ipynb | 694 |
1 files changed, 655 insertions, 39 deletions
diff --git a/C++_from_the_Ground/Chapter_13(1).ipynb b/C++_from_the_Ground/Chapter_13(1).ipynb index c38dca6a..6d12a5a7 100644 --- a/C++_from_the_Ground/Chapter_13(1).ipynb +++ b/C++_from_the_Ground/Chapter_13(1).ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "Chapter 13" + "name": "", + "signature": "sha256:be0df45a07fa8844875025463a4211b5faab9834d4e1dd155b475b36ae6ea27e" }, "nbformat": 3, "nbformat_minor": 0, @@ -10,24 +11,85 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h1>Chapter 13: Operator Overloading<h1>" + "source": [ + "<h1>Chapter 13: Operator Overloading<h1>" + ] }, { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.1, Page Number: 300<h3>" + "source": [ + "<h3>Example 13.1, Page Number: 300<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Overloading operators using member functions'''\n\nclass three_d:\n def __init__(self,i=None,j=None,k=None):\n if i==None:\n self.x=self.y=self.z=0\n else:\n self.x=i\n self.y=j\n self.z=k\n #Overload +\n def __add__(self,op2):\n temp=three_d()\n temp.x=self.x + op2.x #These are integer additions\n temp.y=self.y + op2.y #and the + retains its original\n temp.z=self.z + op2.z #meaning relative to them.\n return temp\n #Overload assignment\n def __assign__(self,op2):\n self.x=op2.x #These are integer assignments\n self.y=op2.y #and the = retains its original \n self.z=op2.z #meaning relative to them\n return self\n #Show x,y,z coordinates\n def show(self):\n print self.x,\",\",self.y,\",\",self.z\n \n#Variable declaration\na=three_d(1,2,3)\nb=three_d(10,10,10)\nc=three_d()\n\na.show()\nb.show()\n\n#add a and b together\nc=a+b\nc.show()\n\n#add a,b and c together\nc=a+b+c\nc.show()\n\n#demonstrate multiple assignment\nc=b=a\nc.show()\nb.show()\n \n ", + "input": [ + "\n", + "\n", + "class three_d:\n", + " def __init__(self,i=None,j=None,k=None):\n", + " if i==None:\n", + " self.x=self.y=self.z=0\n", + " else:\n", + " self.x=i\n", + " self.y=j\n", + " self.z=k\n", + " #Overload +\n", + " def __add__(self,op2):\n", + " temp=three_d()\n", + " temp.x=self.x + op2.x #These are integer additions\n", + " temp.y=self.y + op2.y #and the + retains its original\n", + " temp.z=self.z + op2.z #meaning relative to them.\n", + " return temp\n", + " #Overload assignment\n", + " def __assign__(self,op2):\n", + " self.x=op2.x #These are integer assignments\n", + " self.y=op2.y #and the = retains its original \n", + " self.z=op2.z #meaning relative to them\n", + " return self\n", + " #Show x,y,z coordinates\n", + " def show(self):\n", + " print self.x,\",\",self.y,\",\",self.z\n", + " \n", + "#Variable declaration\n", + "a=three_d(1,2,3)\n", + "b=three_d(10,10,10)\n", + "c=three_d()\n", + "\n", + "a.show()\n", + "b.show()\n", + "\n", + "#add a and b together\n", + "c=a+b\n", + "c.show()\n", + "\n", + "#add a,b and c together\n", + "c=a+b+c\n", + "c.show()\n", + "\n", + "#demonstrate multiple assignment\n", + "c=b=a\n", + "c.show()\n", + "b.show()\n", + " \n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "1 , 2 , 3\n10 , 10 , 10\n11 , 12 , 13\n22 , 24 , 26\n1 , 2 , 3\n1 , 2 , 3\n" + "text": [ + "1 , 2 , 3\n", + "10 , 10 , 10\n", + "11 , 12 , 13\n", + "22 , 24 , 26\n", + "1 , 2 , 3\n", + "1 , 2 , 3\n" + ] } ], "prompt_number": 1 @@ -35,19 +97,86 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.2, Page Number: 303<h3>" + "source": [ + "<h3>Example 13.2, Page Number: 303<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementation of ++ operator in python'''\n\nclass three_d:\n def __init__(self,i=None,j=None,k=None):\n if i==None:\n self.x=self.y=self.z=0\n else:\n self.x=i\n self.y=j\n self.z=k\n #Overload +\n def __add__(self,op2):\n temp=three_d()\n temp.x=self.x + op2.x #These are integer additions\n temp.y=self.y + op2.y #and the + retains its original\n temp.z=self.z + op2.z #meaning relative to them.\n return temp\n #Overload assignment\n def __assign__(self,op2):\n self.x=op2.x #These are integer assignments\n self.y=op2.y #and the = retains its original \n self.z=op2.z #meaning relative to them\n return self\n #Overload the increment operator\n def __iadd__(self,op2):\n self.x+=op2\n self.y+=op2\n self.z+=op2\n return self\n #Show x,y,z coordinates\n def show(self):\n print self.x,\",\",self.y,\",\",self.z\n \na=three_d(1,2,3)\nb=three_d(10,10,10)\nc=three_d()\n\na.show()\nb.show()\n\n#add a and b together\nc=a+b\nc.show()\n\n#add a,b and c together\nc=a+b+c\nc.show()\n\n#demonstrate multiple assignment\nc=b=a\nc.show()\nb.show()\n \n#Increment c\nc+=1\nc.show()\n", + "input": [ + "\n", + "\n", + "class three_d:\n", + " def __init__(self,i=None,j=None,k=None):\n", + " if i==None:\n", + " self.x=self.y=self.z=0\n", + " else:\n", + " self.x=i\n", + " self.y=j\n", + " self.z=k\n", + " #Overload +\n", + " def __add__(self,op2):\n", + " temp=three_d()\n", + " temp.x=self.x + op2.x #These are integer additions\n", + " temp.y=self.y + op2.y #and the + retains its original\n", + " temp.z=self.z + op2.z #meaning relative to them.\n", + " return temp\n", + " #Overload assignment\n", + " def __assign__(self,op2):\n", + " self.x=op2.x #These are integer assignments\n", + " self.y=op2.y #and the = retains its original \n", + " self.z=op2.z #meaning relative to them\n", + " return self\n", + " #Overload the increment operator\n", + " def __iadd__(self,op2):\n", + " self.x+=op2\n", + " self.y+=op2\n", + " self.z+=op2\n", + " return self\n", + " #Show x,y,z coordinates\n", + " def show(self):\n", + " print self.x,\",\",self.y,\",\",self.z\n", + " \n", + "a=three_d(1,2,3)\n", + "b=three_d(10,10,10)\n", + "c=three_d()\n", + "\n", + "a.show()\n", + "b.show()\n", + "\n", + "#add a and b together\n", + "c=a+b\n", + "c.show()\n", + "\n", + "#add a,b and c together\n", + "c=a+b+c\n", + "c.show()\n", + "\n", + "#demonstrate multiple assignment\n", + "c=b=a\n", + "c.show()\n", + "b.show()\n", + " \n", + "#Increment c\n", + "c+=1\n", + "c.show()\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "1 , 2 , 3\n10 , 10 , 10\n11 , 12 , 13\n22 , 24 , 26\n1 , 2 , 3\n1 , 2 , 3\n2 , 3 , 4\n" + "text": [ + "1 , 2 , 3\n", + "10 , 10 , 10\n", + "11 , 12 , 13\n", + "22 , 24 , 26\n", + "1 , 2 , 3\n", + "1 , 2 , 3\n", + "2 , 3 , 4\n" + ] } ], "prompt_number": 2 @@ -55,19 +184,107 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.3, Page Number: 306<h3>" + "source": [ + "<h3>Example 13.3, Page Number: 306<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementation of prefix and postfix ++ operator in python'''\n\nclass three_d:\n def __init__(self,i=None,j=None,k=None):\n if i==None:\n self.x=self.y=self.z=0\n else:\n self.x=i\n self.y=j\n self.z=k\n #Overload +\n def __add__(self,op2):\n temp=three_d()\n temp.x=self.x + op2.x #These are integer additions\n temp.y=self.y + op2.y #and the + retains its original\n temp.z=self.z + op2.z #meaning relative to them.\n return temp\n #Overload assignment\n def __assign__(self,op2):\n self.x=op2.x #These are integer assignments\n self.y=op2.y #and the = retains its original \n self.z=op2.z #meaning relative to them\n return self\n #Overload the increment operator\n def __iadd__(self,op2):\n self.x+=op2\n self.y+=op2\n self.z+=op2\n return self\n #Show x,y,z coordinates\n def show(self):\n print self.x,\",\",self.y,\",\",self.z\n \na=three_d(1,2,3)\nb=three_d(10,10,10)\nc=three_d()\n\na.show()\nb.show()\n\n#add a and b together\nc=a+b\nc.show()\n\n#add a,b and c together\nc=a+b+c\nc.show()\n\n#demonstrate multiple assignment\nc=b=a\nc.show()\nb.show()\n \n#Increment c (prefix)\nc+=1\nc.show()\n\n#Increment c (postfix)\nc+=1\nc.show()\n\n#Implementing prefix\nc+=1\na=c\na.show()\nc.show()\n\n#Implementing postfix\na=c\na.show()\nc+=1\nc.show()", + "input": [ + "\n", + "\n", + "class three_d:\n", + " def __init__(self,i=None,j=None,k=None):\n", + " if i==None:\n", + " self.x=self.y=self.z=0\n", + " else:\n", + " self.x=i\n", + " self.y=j\n", + " self.z=k\n", + " #Overload +\n", + " def __add__(self,op2):\n", + " temp=three_d()\n", + " temp.x=self.x + op2.x #These are integer additions\n", + " temp.y=self.y + op2.y #and the + retains its original\n", + " temp.z=self.z + op2.z #meaning relative to them.\n", + " return temp\n", + " #Overload assignment\n", + " def __assign__(self,op2):\n", + " self.x=op2.x #These are integer assignments\n", + " self.y=op2.y #and the = retains its original \n", + " self.z=op2.z #meaning relative to them\n", + " return self\n", + " #Overload the increment operator\n", + " def __iadd__(self,op2):\n", + " self.x+=op2\n", + " self.y+=op2\n", + " self.z+=op2\n", + " return self\n", + " #Show x,y,z coordinates\n", + " def show(self):\n", + " print self.x,\",\",self.y,\",\",self.z\n", + " \n", + "a=three_d(1,2,3)\n", + "b=three_d(10,10,10)\n", + "c=three_d()\n", + "\n", + "a.show()\n", + "b.show()\n", + "\n", + "#add a and b together\n", + "c=a+b\n", + "c.show()\n", + "\n", + "#add a,b and c together\n", + "c=a+b+c\n", + "c.show()\n", + "\n", + "#demonstrate multiple assignment\n", + "c=b=a\n", + "c.show()\n", + "b.show()\n", + " \n", + "#Increment c (prefix)\n", + "c+=1\n", + "c.show()\n", + "\n", + "#Increment c (postfix)\n", + "c+=1\n", + "c.show()\n", + "\n", + "#Implementing prefix\n", + "c+=1\n", + "a=c\n", + "a.show()\n", + "c.show()\n", + "\n", + "#Implementing postfix\n", + "a=c\n", + "a.show()\n", + "c+=1\n", + "c.show()" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "1 , 2 , 3\n10 , 10 , 10\n11 , 12 , 13\n22 , 24 , 26\n1 , 2 , 3\n1 , 2 , 3\n2 , 3 , 4\n3 , 4 , 5\n4 , 5 , 6\n4 , 5 , 6\n4 , 5 , 6\n5 , 6 , 7\n" + "text": [ + "1 , 2 , 3\n", + "10 , 10 , 10\n", + "11 , 12 , 13\n", + "22 , 24 , 26\n", + "1 , 2 , 3\n", + "1 , 2 , 3\n", + "2 , 3 , 4\n", + "3 , 4 , 5\n", + "4 , 5 , 6\n", + "4 , 5 , 6\n", + "4 , 5 , 6\n", + "5 , 6 , 7\n" + ] } ], "prompt_number": 3 @@ -75,19 +292,79 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.4, Page Number: 310<h3>" + "source": [ + "<h3>Example 13.4, Page Number: 310<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Overload + using a friend'''\n\nclass three_d:\n def __init__(self,i=None,j=None,k=None):\n if i==None:\n self.x=self.y=self.z=0\n else:\n self.x=i\n self.y=j\n self.z=k\n #Overload +\n def __add__(self,op2):\n return add(self,op2)\n #Overload assignment\n def __assign__(self,op2):\n self.x=op2.x #These are integer assignments\n self.y=op2.y #and the = retains its original \n self.z=op2.z #meaning relative to them\n return self\n #Show x,y,z coordinates\n def show(self):\n print self.x,\",\",self.y,\",\",self.z\n \n#friending the funcion\ndef add(op1,op2):\n temp=three_d()\n temp.x=op1.x + op2.x #These are integer additions\n temp.y=op1.y + op2.y #and the + retains its original\n temp.z=op1.z + op2.z #meaning relative to them.\n return temp\n\na=three_d(1,2,3)\nb=three_d(10,10,10)\nc=three_d()\n\na.show()\nb.show()\n\n#add a and b together\nc=a+b\nc.show()\n\n#add a,b and c together\nc=a+b+c\nc.show()\n\n#demonstrate multiple assignment\nc=b=a\nc.show()\nb.show()\n", + "input": [ + "\n", + "\n", + "class three_d:\n", + " def __init__(self,i=None,j=None,k=None):\n", + " if i==None:\n", + " self.x=self.y=self.z=0\n", + " else:\n", + " self.x=i\n", + " self.y=j\n", + " self.z=k\n", + " #Overload +\n", + " def __add__(self,op2):\n", + " return add(self,op2)\n", + " #Overload assignment\n", + " def __assign__(self,op2):\n", + " self.x=op2.x #These are integer assignments\n", + " self.y=op2.y #and the = retains its original \n", + " self.z=op2.z #meaning relative to them\n", + " return self\n", + " #Show x,y,z coordinates\n", + " def show(self):\n", + " print self.x,\",\",self.y,\",\",self.z\n", + " \n", + "#friending the funcion\n", + "def add(op1,op2):\n", + " temp=three_d()\n", + " temp.x=op1.x + op2.x #These are integer additions\n", + " temp.y=op1.y + op2.y #and the + retains its original\n", + " temp.z=op1.z + op2.z #meaning relative to them.\n", + " return temp\n", + "\n", + "a=three_d(1,2,3)\n", + "b=three_d(10,10,10)\n", + "c=three_d()\n", + "\n", + "a.show()\n", + "b.show()\n", + "\n", + "#add a and b together\n", + "c=a+b\n", + "c.show()\n", + "\n", + "#add a,b and c together\n", + "c=a+b+c\n", + "c.show()\n", + "\n", + "#demonstrate multiple assignment\n", + "c=b=a\n", + "c.show()\n", + "b.show()\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "1 , 2 , 3\n10 , 10 , 10\n11 , 12 , 13\n22 , 24 , 26\n1 , 2 , 3\n1 , 2 , 3\n" + "text": [ + "1 , 2 , 3\n", + "10 , 10 , 10\n", + "11 , 12 , 13\n", + "22 , 24 , 26\n", + "1 , 2 , 3\n", + "1 , 2 , 3\n" + ] } ], "prompt_number": 4 @@ -95,19 +372,62 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.5, Page Number: 311<h3>" + "source": [ + "<h3>Example 13.5, Page Number: 311<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Operator overloading when object is on the right side of the operator'''\n\nclass CL:\n def __init__(self):\n self.count=0\n def __assign__(self,obj):\n self.count=obj.count\n return self\n def __add__(self,i): \n return add(self,i)\n def __radd__(self,i):\n return radd(self,i)\n\n#This handles ob + int\ndef add(ob,i):\n temp=CL()\n temp.count=ob.count+i\n return temp\n \n#This handles int + ob \ndef radd(ob,i):\n temp=CL()\n temp.count=i+ob.count\n return temp\n\n#Variable declaration\no=CL()\no.count = 10\n\n#Result\nprint o.count, #outputs 10\no=10+o\nprint o.count, #outputs 20\no=o+12\nprint o.count #outputs 32\n\n\n ", + "input": [ + "\n", + "\n", + "class CL:\n", + " def __init__(self):\n", + " self.count=0\n", + " def __assign__(self,obj):\n", + " self.count=obj.count\n", + " return self\n", + " def __add__(self,i): \n", + " return add(self,i)\n", + " def __radd__(self,i):\n", + " return radd(self,i)\n", + "\n", + "#This handles ob + int\n", + "def add(ob,i):\n", + " temp=CL()\n", + " temp.count=ob.count+i\n", + " return temp\n", + " \n", + "#This handles int + ob \n", + "def radd(ob,i):\n", + " temp=CL()\n", + " temp.count=i+ob.count\n", + " return temp\n", + "\n", + "#Variable declaration\n", + "o=CL()\n", + "o.count = 10\n", + "\n", + "#Result\n", + "print o.count, #outputs 10\n", + "o=10+o\n", + "print o.count, #outputs 20\n", + "o=o+12\n", + "print o.count #outputs 32\n", + "\n", + "\n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "10 20 32\n" + "text": [ + "10 20 32\n" + ] } ], "prompt_number": 1 @@ -115,19 +435,112 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.6, Page Number: 314<h3>" + "source": [ + "<h3>Example 13.6, Page Number: 314<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementation of prefix and postfix ++ as a friend function'''\n\nclass three_d:\n def __init__(self,i=None,j=None,k=None):\n if i==None:\n self.x=self.y=self.z=0\n else:\n self.x=i\n self.y=j\n self.z=k\n #Overload +\n def __add__(self,op2):\n return add(self,op2)\n #Overload assignment\n def __assign__(self,op2):\n self.x=op2.x #These are integer assignments\n self.y=op2.y #and the = retains its original \n self.z=op2.z #meaning relative to them\n return self\n #Overload the increment operator\n def __iadd__(self,op2):\n return iadd(self,op2)\n #Show x,y,z coordinates\n def show(self):\n print self.x,\",\",self.y,\",\",self.z\n\n#friending the funcion\ndef add(op1,op2):\n temp=three_d()\n temp.x=op1.x + op2.x #These are integer additions\n temp.y=op1.y + op2.y #and the + retains its original\n temp.z=op1.z + op2.z #meaning relative to them.\n return temp\ndef iadd(op1,op2):\n op1.x+=op2\n op1.y+=op2\n op1.z+=op2\n return op1\n \na=three_d(1,2,3)\nb=three_d(10,10,10)\nc=three_d()\n\na.show()\nb.show()\n\n#add a and b together\nc=a+b\nc.show()\n\n#add a,b and c together\nc=a+b+c\nc.show()\n\n#demonstrate multiple assignment\nc=b=a\nc.show()\nb.show()\n \n#Increment c (prefix)\nc+=1\nc.show()\n\n#Increment c (postfix)\nc+=1\nc.show()\n\n#Implementing prefix\nc+=1\na=c\na.show()\nc.show()\n\n#Implementing postfix\na=c\na.show()\nc+=1\nc.show()", + "input": [ + "\n", + "class three_d:\n", + " def __init__(self,i=None,j=None,k=None):\n", + " if i==None:\n", + " self.x=self.y=self.z=0\n", + " else:\n", + " self.x=i\n", + " self.y=j\n", + " self.z=k\n", + " #Overload +\n", + " def __add__(self,op2):\n", + " return add(self,op2)\n", + " #Overload assignment\n", + " def __assign__(self,op2):\n", + " self.x=op2.x #These are integer assignments\n", + " self.y=op2.y #and the = retains its original \n", + " self.z=op2.z #meaning relative to them\n", + " return self\n", + " #Overload the increment operator\n", + " def __iadd__(self,op2):\n", + " return iadd(self,op2)\n", + " #Show x,y,z coordinates\n", + " def show(self):\n", + " print self.x,\",\",self.y,\",\",self.z\n", + "\n", + "#friending the funcion\n", + "def add(op1,op2):\n", + " temp=three_d()\n", + " temp.x=op1.x + op2.x #These are integer additions\n", + " temp.y=op1.y + op2.y #and the + retains its original\n", + " temp.z=op1.z + op2.z #meaning relative to them.\n", + " return temp\n", + "def iadd(op1,op2):\n", + " op1.x+=op2\n", + " op1.y+=op2\n", + " op1.z+=op2\n", + " return op1\n", + " \n", + "a=three_d(1,2,3)\n", + "b=three_d(10,10,10)\n", + "c=three_d()\n", + "\n", + "a.show()\n", + "b.show()\n", + "\n", + "#add a and b together\n", + "c=a+b\n", + "c.show()\n", + "\n", + "#add a,b and c together\n", + "c=a+b+c\n", + "c.show()\n", + "\n", + "#demonstrate multiple assignment\n", + "c=b=a\n", + "c.show()\n", + "b.show()\n", + " \n", + "#Increment c (prefix)\n", + "c+=1\n", + "c.show()\n", + "\n", + "#Increment c (postfix)\n", + "c+=1\n", + "c.show()\n", + "\n", + "#Implementing prefix\n", + "c+=1\n", + "a=c\n", + "a.show()\n", + "c.show()\n", + "\n", + "#Implementing postfix\n", + "a=c\n", + "a.show()\n", + "c+=1\n", + "c.show()" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "1 , 2 , 3\n10 , 10 , 10\n11 , 12 , 13\n22 , 24 , 26\n1 , 2 , 3\n1 , 2 , 3\n2 , 3 , 4\n3 , 4 , 5\n4 , 5 , 6\n4 , 5 , 6\n4 , 5 , 6\n5 , 6 , 7\n" + "text": [ + "1 , 2 , 3\n", + "10 , 10 , 10\n", + "11 , 12 , 13\n", + "22 , 24 , 26\n", + "1 , 2 , 3\n", + "1 , 2 , 3\n", + "2 , 3 , 4\n", + "3 , 4 , 5\n", + "4 , 5 , 6\n", + "4 , 5 , 6\n", + "4 , 5 , 6\n", + "5 , 6 , 7\n" + ] } ], "prompt_number": 6 @@ -135,19 +548,61 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.7, Page Number: 318<h3>" + "source": [ + "<h3>Example 13.7, Page Number: 318<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "\n\nclass sample:\n def __init__(self,ob=0):\n if isinstance(ob,int):\n #Normal constructor\n self.__s=\"\"\n return\n else:\n #Copy constructor\n self.__s=obj._sample__s\n return\n def __del__(self):\n print \"Freeing s\"\n def show(self):\n print self.__s\n def set(self,str):\n self.__s=str\n def __assign__(self,ob): #Overload assignment\n self.s=ob._sample__s\n return self\n \ndef input():\n str=sample()\n instr=\"Hello\" #User input\n str.set(instr)\n return str\n\nob=sample()\n\n#assign returned object to ob\nob=input()\n\n#Result\nob.show()\n", + "input": [ + "\n", + "\n", + "class sample:\n", + " def __init__(self,ob=0):\n", + " if isinstance(ob,int):\n", + " #Normal constructor\n", + " self.__s=\"\"\n", + " return\n", + " else:\n", + " #Copy constructor\n", + " self.__s=obj._sample__s\n", + " return\n", + " def __del__(self):\n", + " print \"Freeing s\"\n", + " def show(self):\n", + " print self.__s\n", + " def set(self,str):\n", + " self.__s=str\n", + " def __assign__(self,ob): #Overload assignment\n", + " self.s=ob._sample__s\n", + " return self\n", + " \n", + "def input():\n", + " str=sample()\n", + " instr=\"Hello\" #User input\n", + " str.set(instr)\n", + " return str\n", + "\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": "Freeing s\nFreeing s\nHello\n" + "text": [ + "Freeing s\n", + "Freeing s\n", + "Hello\n" + ] } ], "prompt_number": 4 @@ -155,19 +610,41 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.8, Page Number: 321<h3>" + "source": [ + "<h3>Example 13.8, Page Number: 321<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Overload []'''\n#There is on implementation of overloading [], hence we use normal functions\n\n\nclass atype:\n def __init__(self):\n self.__a=[]\n for i in range(SIZE):\n self.__a.append(i)\n def a(self,i):\n return self.__a[i]\n \n#Variable declaration\nSIZE=3\nob=atype()\n\n#Result\nprint ob.a(2),\n\n", + "input": [ + "\n", + "\n", + "class atype:\n", + " def __init__(self):\n", + " self.__a=[]\n", + " for i in range(SIZE):\n", + " self.__a.append(i)\n", + " def a(self,i):\n", + " return self.__a[i]\n", + " \n", + "#Variable declaration\n", + "SIZE=3\n", + "ob=atype()\n", + "\n", + "#Result\n", + "print ob.a(2),\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "2\n" + "text": [ + "2\n" + ] } ], "prompt_number": 6 @@ -175,19 +652,46 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.9, Page Number: 322<h3>" + "source": [ + "<h3>Example 13.9, Page Number: 322<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Overload []'''\n\nclass atype:\n def __init__(self):\n self.__a=[]\n for i in range(SIZE):\n self.__a.append(i)\n def a(self,i,j=None):\n if j==None:\n return self.__a[i]\n else:\n self.__a[i]=j\n \n#Variable declaration\nSIZE=3 \nob=atype()\n\nprint ob.a(2), #displays 2\n\nob.a(2,25)\n\nprint ob.a(2) #now displays 25", + "input": [ + "\n", + "\n", + "class atype:\n", + " def __init__(self):\n", + " self.__a=[]\n", + " for i in range(SIZE):\n", + " self.__a.append(i)\n", + " def a(self,i,j=None):\n", + " if j==None:\n", + " return self.__a[i]\n", + " else:\n", + " self.__a[i]=j\n", + " \n", + "#Variable declaration\n", + "SIZE=3 \n", + "ob=atype()\n", + "\n", + "print ob.a(2), #displays 2\n", + "\n", + "ob.a(2,25)\n", + "\n", + "print ob.a(2) #now displays 25" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "2 25\n" + "text": [ + "2 25\n" + ] } ], "prompt_number": 7 @@ -195,19 +699,52 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.10, Page Number: 323<h3>" + "source": [ + "<h3>Example 13.10, Page Number: 323<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''A safe array example'''\n\nclass atype:\n def __init__(self):\n self.__a=[]\n for i in range(SIZE):\n self.__a.append(i)\n def a(self,i,j=None):\n if (i<0 or i>SIZE-1):\n print \"Index value of\",\n print i,\"is out of bounds.\"\n return\n if j==None:\n return self.__a[i]\n else:\n self.__a[i]=j\n \n#Variable declaration\nSIZE=3 \nob=atype()\n\nprint ob.a(2), #displays 2\n\nob.a(2,25)\n\nprint ob.a(2) #now displays 25\n\nob.a(44,3) #generates runtime error, 3 out of bounds", + "input": [ + "\n", + "class atype:\n", + " def __init__(self):\n", + " self.__a=[]\n", + " for i in range(SIZE):\n", + " self.__a.append(i)\n", + " def a(self,i,j=None):\n", + " if (i<0 or i>SIZE-1):\n", + " print \"Index value of\",\n", + " print i,\"is out of bounds.\"\n", + " return\n", + " if j==None:\n", + " return self.__a[i]\n", + " else:\n", + " self.__a[i]=j\n", + " \n", + "#Variable declaration\n", + "SIZE=3 \n", + "ob=atype()\n", + "\n", + "print ob.a(2), #displays 2\n", + "\n", + "ob.a(2,25)\n", + "\n", + "print ob.a(2) #now displays 25\n", + "\n", + "ob.a(44,3) #generates runtime error, 3 out of bounds" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "2 25\nIndex value of 44 is out of bounds.\n" + "text": [ + "2 25\n", + "Index value of 44 is out of bounds.\n" + ] } ], "prompt_number": 9 @@ -215,19 +752,56 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.11, Page Number: 324<h3>" + "source": [ + "<h3>Example 13.11, Page Number: 324<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Overload ()'''\n\nclass three_d:\n def __init__(self,i=None,j=None,k=None):\n if i==None:\n self.x=self.y=self.z=0 #3-D coordinates\n else:\n self.x=i\n self.y=j\n self.z=k\n #Show X,Y,Z coordinates\n def show(self):\n print self.x,\",\",self.y,\",\",self.z\n #Overload ()\n def a(self,a,b,c):\n temp = three_d()\n temp.x=self.x+a\n temp.y=self.y+b\n temp.z=self.z+c\n return temp\n \n#Variable declaration\nob1=three_d(1,2,3)\n\nob2=ob1.a(10,11,12) #invoke operator ()\n\n#Result\nprint \"ob1: \",\nob1.show()\nprint \"ob2: \",\nob2.show()", + "input": [ + "\n", + "\n", + "class three_d:\n", + " def __init__(self,i=None,j=None,k=None):\n", + " if i==None:\n", + " self.x=self.y=self.z=0 #3-D coordinates\n", + " else:\n", + " self.x=i\n", + " self.y=j\n", + " self.z=k\n", + " #Show X,Y,Z coordinates\n", + " def show(self):\n", + " print self.x,\",\",self.y,\",\",self.z\n", + " #Overload ()\n", + " def a(self,a,b,c):\n", + " temp = three_d()\n", + " temp.x=self.x+a\n", + " temp.y=self.y+b\n", + " temp.z=self.z+c\n", + " return temp\n", + " \n", + "#Variable declaration\n", + "ob1=three_d(1,2,3)\n", + "\n", + "ob2=ob1.a(10,11,12) #invoke operator ()\n", + "\n", + "#Result\n", + "print \"ob1: \",\n", + "ob1.show()\n", + "print \"ob2: \",\n", + "ob2.show()" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "ob1: 1 , 2 , 3\nob2: 11 , 13 , 15\n" + "text": [ + "ob1: 1 , 2 , 3\n", + "ob2: 11 , 13 , 15\n" + ] } ], "prompt_number": 10 @@ -235,19 +809,61 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 13.12, Page Number: 326<h3>" + "source": [ + "<h3>Example 13.12, Page Number: 326<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Expanding the string type'''\n\nclass str_type:\n def __init__(self,str=\"\"):\n self.__string=str\n #String concatenation\n def __add__(self,str):\n temp=str_type()\n if isinstance(str,str_type):\n temp.__string=self.__string+str.__string\n else:\n temp.__string=self.__string+str\n return temp\n #String copy\n def __assign__(self,str):\n if isinstance(str,str_type):\n self.__string=str.__string\n else:\n self.__string=str\n return self\n def show_str(self):\n print self.__string\n \na=str_type(\"Hello \")\nb=str_type(\"There\")\nc=a+b\nc.show_str()\n\na=str_type(\"to program in because\")\na.show_str()\n\nb=c=str_type(\"C++ is fun\")\n\nc=c+\" \"+a+\" \"+b\nc.show_str()\n\n", + "input": [ + "\n", + "class str_type:\n", + " def __init__(self,str=\"\"):\n", + " self.__string=str\n", + " #String concatenation\n", + " def __add__(self,str):\n", + " temp=str_type()\n", + " if isinstance(str,str_type):\n", + " temp.__string=self.__string+str.__string\n", + " else:\n", + " temp.__string=self.__string+str\n", + " return temp\n", + " #String copy\n", + " def __assign__(self,str):\n", + " if isinstance(str,str_type):\n", + " self.__string=str.__string\n", + " else:\n", + " self.__string=str\n", + " return self\n", + " def show_str(self):\n", + " print self.__string\n", + " \n", + "a=str_type(\"Hello \")\n", + "b=str_type(\"There\")\n", + "c=a+b\n", + "c.show_str()\n", + "\n", + "a=str_type(\"to program in because\")\n", + "a.show_str()\n", + "\n", + "b=c=str_type(\"C++ is fun\")\n", + "\n", + "c=c+\" \"+a+\" \"+b\n", + "c.show_str()\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Hello There\nto program in because\nC++ is fun to program in because C++ is fun\n" + "text": [ + "Hello There\n", + "to program in because\n", + "C++ is fun to program in because C++ is fun\n" + ] } ], "prompt_number": 12 @@ -255,7 +871,7 @@ { "cell_type": "code", "collapsed": false, - "input": "", + "input": [], "language": "python", "metadata": {}, "outputs": [] |