summaryrefslogtreecommitdiff
path: root/sample_notebooks/SrutiGoyal/Chapter_11-_Object_Initialization_and_Clean-Up.ipynb
diff options
context:
space:
mode:
authorhardythe12015-04-07 15:58:05 +0530
committerhardythe12015-04-07 15:58:05 +0530
commitc7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131 (patch)
tree725a7d43dc1687edf95bc36d39bebc3000f1de8f /sample_notebooks/SrutiGoyal/Chapter_11-_Object_Initialization_and_Clean-Up.ipynb
parent62aa228e2519ac7b7f1aef53001f2f2e988a6eb1 (diff)
downloadPython-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.tar.gz
Python-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.tar.bz2
Python-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.zip
added books
Diffstat (limited to 'sample_notebooks/SrutiGoyal/Chapter_11-_Object_Initialization_and_Clean-Up.ipynb')
-rwxr-xr-xsample_notebooks/SrutiGoyal/Chapter_11-_Object_Initialization_and_Clean-Up.ipynb1779
1 files changed, 1779 insertions, 0 deletions
diff --git a/sample_notebooks/SrutiGoyal/Chapter_11-_Object_Initialization_and_Clean-Up.ipynb b/sample_notebooks/SrutiGoyal/Chapter_11-_Object_Initialization_and_Clean-Up.ipynb
new file mode 100755
index 00000000..d3bdda01
--- /dev/null
+++ b/sample_notebooks/SrutiGoyal/Chapter_11-_Object_Initialization_and_Clean-Up.ipynb
@@ -0,0 +1,1779 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:64b755d597a2016634dadbbc81a598a60446119cc89f4f94fd9140b5bc077b77"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 11: Object Initialization and clean up"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example- bag.cpp, Page-392"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "MAX_ITEMS=25\n",
+ "def show(self):\n",
+ " for i in range(self.ItemCount):\n",
+ " print self._Bag__contents[i],\n",
+ "class Bag:\n",
+ " __contents=[int]*MAX_ITEMS\n",
+ " __ItemCount=int\n",
+ " def SetEmpty(self):\n",
+ " self.ItemCount=0\n",
+ " def put(self,item):\n",
+ " self._Bag__contents[self.ItemCount]=item\n",
+ " self.ItemCount+=1\n",
+ " show=show\n",
+ "bag=Bag() #object of class Bag\n",
+ "bag.SetEmpty() #initialize the object\n",
+ "while 1:\n",
+ " item=int(raw_input(\"\\nEnter Item Number to be put into the bag <0-no item>: \"))\n",
+ " if item==0:\n",
+ " break\n",
+ " bag.put(item)\n",
+ " print \"Items in bag:\",\n",
+ " bag.show()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Items in bag: 1"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Items in bag: 1 3"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Items in bag: 1 3 2"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Items in bag: 1 3 2 4"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example- newbag.cpp, Page-395"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "MAX_ITEMS=25 #size of array contents\n",
+ "def show(self):\n",
+ " for i in range(self.ItemCount):\n",
+ " print self._Bag__contents[i],\n",
+ "class Bag:\n",
+ " __contents=[int]*MAX_ITEMS #int 1D array\n",
+ " __ItemCount=int\n",
+ " def __init__(self): #Constructor\n",
+ " self.ItemCount=0\n",
+ " def put(self,item): #member function defined inside the class\n",
+ " self._Bag__contents[self.ItemCount]=item\n",
+ " self.ItemCount+=1\n",
+ " show=show #member function defined outside the class\n",
+ "bag=Bag() #object of class Bag\n",
+ "while 1:\n",
+ " item=int(raw_input(\"\\nEnter Item Number to be put into the bag <0-no item>: \"))\n",
+ " if item==0:\n",
+ " break\n",
+ " bag.put(item)\n",
+ " print \"Items in bag:\",\n",
+ " bag.show()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Items in bag: 1"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Items in bag: 1 3"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Items in bag: 1 3 2"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Items in bag: 1 3 2 4"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example-test1.cpp, Page-396"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def __init__(self):\n",
+ " print \"Constructor of class test called\"\n",
+ "class Test:\n",
+ " __init__=__init__ #Constructor\n",
+ "G=Test()\n",
+ "def func():\n",
+ " L=Test()\n",
+ " print \"Here's function func()\"\n",
+ "X=Test()\n",
+ "print \"main() function\"\n",
+ "func()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Constructor of class test called\n",
+ "Constructor of class test called\n",
+ "main() function\n",
+ "Constructor of class test called\n",
+ "Here's function func()\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example- giftbag.cpp, Page- 398"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "MAX_ITEMS=25\n",
+ "def show(self):\n",
+ " if self.ItemCount:\n",
+ " for i in range(self.ItemCount):\n",
+ " print self._Bag__contents[i],\n",
+ " else:\n",
+ " print \"Nil\"\n",
+ "class Bag:\n",
+ " __contents=[int]*MAX_ITEMS\n",
+ " __ItemCount=int\n",
+ " def __init__(self, item=None): #parameterized constructor: Python does not support overloading of functions\n",
+ " if isinstance(item, int):\n",
+ " self._Bag__contents[0]=item\n",
+ " self.ItemCount=1\n",
+ " else:\n",
+ " self.ItemCount=0\n",
+ " def put(self,item):\n",
+ " self._Bag__contents[self.ItemCount]=item\n",
+ " self.ItemCount+=1\n",
+ " show=show\n",
+ "bag1=Bag()\n",
+ "bag2=Bag(4) #object created using the parameterized constructor\n",
+ "print \"Gifted bag1 initially has:\",\n",
+ "bag1.show()\n",
+ "print \"Gifted bag2 initially has:\",\n",
+ "bag2.show()\n",
+ "while 1:\n",
+ " item=int(raw_input(\"\\nEnter Item Number to be put into the bag <0-no item>: \"))\n",
+ " if item==0:\n",
+ " break\n",
+ " bag2.put(item)\n",
+ " print \"Items in bag2:\",\n",
+ " bag2.show()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Gifted bag1 initially has: Nil\n",
+ "Gifted bag2 initially has: 4"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Items in bag2: 4 1"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Items in bag2: 4 1 2"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Items in bag2: 4 1 2 3"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter Item Number to be put into the bag <0-no item>: 0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example-test.cpp, Page-400 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def __init__(self):\n",
+ " print \"Constructor of class Test called\"\n",
+ "def __del__(self):\n",
+ " print \"Destructor of class Test called\"\n",
+ "class Test:\n",
+ " __init__=__init__ #Constructor\n",
+ " __del__=__del__ #Destructor\n",
+ "x=Test()\n",
+ "print \"Terminating main\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Constructor of class Test called\n",
+ "Destructor of class Test called\n",
+ "Terminating main\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example-count.cpp, Page-401"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "nobjects=0\n",
+ "nobj_alive=0\n",
+ "class MyClass:\n",
+ " def __init__(self):\n",
+ " global nobjects #using the global nobjects\n",
+ " global nobj_alive #using the global nobj_alive\n",
+ " nobjects+=1\n",
+ " nobj_alive+=1\n",
+ " def __del__(self):\n",
+ " global nobj_alive #using the global nobjects\n",
+ " nobj_alive-=1\n",
+ " def show(self):\n",
+ " global nobjects\n",
+ " global nobj_alive\n",
+ " print \"Total number of objects created: \", nobjects\n",
+ " print \"Number of objects currently alive: \", nobj_alive\n",
+ "obj1=MyClass()\n",
+ "obj1.show()\n",
+ "def func():\n",
+ " obj1=MyClass()\n",
+ " obj2=MyClass()\n",
+ " obj2.show()\n",
+ " del obj1\n",
+ " del obj2\n",
+ "func()\n",
+ "obj1.show()\n",
+ "obj2=MyClass()\n",
+ "obj3=MyClass()\n",
+ "obj2.show()\n",
+ "del obj1\n",
+ "del obj2\n",
+ "del obj3"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Total number of objects created: 1\n",
+ "Number of objects currently alive: 1\n",
+ "Total number of objects created: 3\n",
+ "Number of objects currently alive: 3\n",
+ "Total number of objects created: 3\n",
+ "Number of objects currently alive: 1\n",
+ "Total number of objects created: 5\n",
+ "Number of objects currently alive: 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Example-account.cpp, Page- 403"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def MoneyTransfer(self, acc , amount):\n",
+ " self._AccClass__balance=self._AccClass__balance-amount\n",
+ " acc._AccClass__balance=acc._AccClass__balance + amount\n",
+ "class AccClass:\n",
+ " __accno=int\n",
+ " __balance=float\n",
+ " def __init__(self, an=None, bal=0.0):\n",
+ " if isinstance(an, int):\n",
+ " self.accno=an\n",
+ " self.__balance=bal\n",
+ " else:\n",
+ " self.accno=raw_input(\"Enter account number for acc1 object: \")\n",
+ " self.__balance=float(raw_input(\"Enter the balance: \"))\n",
+ " def display(self):\n",
+ " print \"Acoount number is: \", self.accno\n",
+ " print \"Balance is: \", self.__balance\n",
+ " MoneyTransfer=MoneyTransfer\n",
+ "acc1=AccClass()\n",
+ "acc2=AccClass(10)\n",
+ "acc3=AccClass(20, 750.5)\n",
+ "print \"Acoount information...\"\n",
+ "acc1.display()\n",
+ "acc2.display()\n",
+ "acc3.display()\n",
+ "trans_money=float(raw_input(\"How much money is to be transferred from acc3 to acc1: \"))\n",
+ "acc3.MoneyTransfer(acc1, trans_money)\n",
+ "print \"Updated information about accounts...\"\n",
+ "acc1.display()\n",
+ "acc2.display()\n",
+ "acc3.display()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter account number for acc1 object: 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the balance: 100\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Acoount information...\n",
+ "Acoount number is: 1\n",
+ "Balance is: 100.0\n",
+ "Acoount number is: 10\n",
+ "Balance is: 0.0\n",
+ "Acoount number is: 20\n",
+ "Balance is: 750.5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How much money is to be transferred from acc3 to acc1: 200\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Updated information about accounts...\n",
+ "Acoount number is: 1\n",
+ "Balance is: 300.0\n",
+ "Acoount number is: 10\n",
+ "Balance is: 0.0\n",
+ "Acoount number is: 20\n",
+ "Balance is: 550.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example-test2.cpp. Page- 405"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def __init__(self, NameIn=None):\n",
+ " if isinstance(NameIn, str):\n",
+ " self.name=NameIn\n",
+ " print \"Test Object \", NameIn, \" created\"\n",
+ " else:\n",
+ " self.name=\"unnamed\"\n",
+ " print \"Test object 'unnamed' created\"\n",
+ "def __del__(self):\n",
+ " print \"Test Object \", self.name, \" destroyed\"\n",
+ " del self.name\n",
+ "class Test:\n",
+ " __name=[str]\n",
+ " __init__=__init__\n",
+ " __del__=__del__\n",
+ "g=Test(\"global\")\n",
+ "def func():\n",
+ " l=Test(\"func\")\n",
+ " print \"here's function func()\"\n",
+ "x=Test(\"main\")\n",
+ "func()\n",
+ "print \"main() function - termination\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Test Object global created\n",
+ "Test Object global destroyed\n",
+ "Test Object main created\n",
+ "Test Object main destroyed\n",
+ "Test Object func created\n",
+ "here's function func()\n",
+ "Test Object func destroyed\n",
+ "main() function - termination\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example-complex1.cpp, Page- 407"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import math\n",
+ "def add (self, c2):\n",
+ " temp=Complex()\n",
+ " temp._Complex__real=self._Complex__real+c2._Complex__real\n",
+ " temp._Complex__imag=self._Complex__imag+c2._Complex__imag\n",
+ " return temp\n",
+ "class Complex:\n",
+ " __real=float\n",
+ " __imag=float\n",
+ " def __init__(self, real_in=None, imag_in=0.0):\n",
+ " if isinstance(real_in, float):\n",
+ " self.__real=real_in\n",
+ " self.__imag=imag_in\n",
+ " else:\n",
+ " self.__real=self.__imag=0.0\n",
+ " def show(self, msg):\n",
+ " print msg, \n",
+ " print self.__real,\n",
+ " if self.__imag<0:\n",
+ " print \"-i\",\n",
+ " else:\n",
+ " print \"+i\",\n",
+ " print math.fabs(self.__imag) #print absolute value\n",
+ " add=add\n",
+ "c1=Complex(1.5,2.0)\n",
+ "c2=Complex(2.2)\n",
+ "c3=Complex()\n",
+ "c1.show(\"c1=\")\n",
+ "c2.show(\"c2=\")\n",
+ "c3=c1.add(c2)\n",
+ "c3.show(\"c3=c1.add(c2):\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "c1= 1.5 +i 2.0\n",
+ "c2= 2.2 +i 0.0\n",
+ "c3=c1.add(c2): 3.7 +i 2.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example- noname.cpp, Page- 410"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class nameless:\n",
+ " __a=int\n",
+ " def __init__(self):\n",
+ " print \"Constructor\"\n",
+ " def __del__(self):\n",
+ " print \"Destructor\"\n",
+ "nameless() #nameless object created\n",
+ "n1=nameless()\n",
+ "n2=nameless()\n",
+ "print \"Program terminates\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Constructor\n",
+ "Destructor\n",
+ "Constructor\n",
+ "Destructor\n",
+ "Constructor\n",
+ "Destructor\n",
+ "Program terminates\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example-name.cpp, Page-411"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def show(self, msg):\n",
+ " print msg\n",
+ " print \"First Name: \", self._name__first\n",
+ " if self._name__middle[0]:\n",
+ " print \"Middle Name: \", self._name__middle\n",
+ " if self._name__last[0]:\n",
+ " print \"Last Name: \", self._name__last\n",
+ "class name:\n",
+ " __first=[None]*15\n",
+ " __middle=[None]*15\n",
+ " __last=[None]*15\n",
+ " def __init__(self, FirstName=None, MiddleName=None, LastName=None):\n",
+ " if isinstance(LastName, str):\n",
+ " self.__last=LastName\n",
+ " self.__middle=MiddleName\n",
+ " self.__first=FirstName\n",
+ " elif isinstance(MiddleName, str):\n",
+ " self.__middle=MiddleName\n",
+ " self.__first=FirstName\n",
+ " elif isinstance(FirstName, str):\n",
+ " self.__first=FirstName\n",
+ " else:\n",
+ " self.__last='\\0' #initialized to NULL\n",
+ " self.__middle='\\0'\n",
+ " self.__first='\\0'\n",
+ " show=show\n",
+ "n1=name()\n",
+ "n2=name()\n",
+ "n3=name()\n",
+ "n1=name(\"Rajkumar\")\n",
+ "n2=name(\"Savithri\", \"S\")\n",
+ "n3=name(\"Veugopal\", \"K\", \"R\")\n",
+ "n1.show(\"First prson details...\")\n",
+ "n2.show(\"Second prson details...\")\n",
+ "n3.show(\"Third prson details...\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "First prson details...\n",
+ "First Name: Rajkumar\n",
+ "Second prson details...\n",
+ "First Name: Savithri\n",
+ "Middle Name: S\n",
+ "Third prson details...\n",
+ "First Name: Veugopal\n",
+ "Middle Name: K\n",
+ "Last Name: R\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example-vector1.cpp, Page-413"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def read(self):\n",
+ " for i in range(self._vector__sz):\n",
+ " print \"Enter vector [\", i, \"]? \",\n",
+ " self._vector__v[i]=int(raw_input())\n",
+ "def show_sum(self):\n",
+ " Sum=0\n",
+ " for i in range(self._vector__sz):\n",
+ " Sum+=self._vector__v[i]\n",
+ " print \"Vector sum= \", Sum\n",
+ "class vector:\n",
+ " __v=[int] #array of type integer\n",
+ " __sz=int\n",
+ " def __init__(self, size):\n",
+ " self.__sz= size\n",
+ " self.__v=[int]*size #dynamically allocating size to integer array\n",
+ " def __del__(self):\n",
+ " del self.__v\n",
+ " read=read\n",
+ " show_sum=show_sum\n",
+ "count = int\n",
+ "count=int(raw_input(\"How many elements are there in the vector: \"))\n",
+ "v1= vector(count)\n",
+ "v1.read()\n",
+ "v1.show_sum()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many elements are there in the vector: 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter vector [ 0 ]? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter vector [ 1 ]? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter vector [ 2 ]? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter vector [ 3 ]? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter vector [ 4 ]? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Vector sum= 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example-vector2.cpp, Page-415"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def show(self):\n",
+ " for i in range(self._vector__size):\n",
+ " print self.elem(i), \", \",\n",
+ "class vector:\n",
+ " __v=[int]\n",
+ " __size=int\n",
+ " def __init__(self, vector_size):\n",
+ " if isinstance(vector_size, int):\n",
+ " self.__size= vector_size\n",
+ " self.__v=[int]*vector_size\n",
+ " else:\n",
+ " print \"Copy construcor invoked\"\n",
+ " self.__size=vector_size.__size\n",
+ " self.__v=[int]*vector_size.__size\n",
+ " for i in range(vector_size.__size):\n",
+ " self.__v[i]=vector_size.__v[i]\n",
+ " def elem(self,i):\n",
+ " if i>=self.__size:\n",
+ " print \"Error: Out of Range\"\n",
+ " return -1\n",
+ " return self.__v[i]\n",
+ " def __del__(self):\n",
+ " del self.__v\n",
+ " show=show\n",
+ "v1=vector(5)\n",
+ "v2=vector(5)\n",
+ "for i in range(5):\n",
+ " if v2.elem(i)!=-1:\n",
+ " v2._vector__v[i]=i+1\n",
+ "v1=v2\n",
+ "v3=vector(v2)\n",
+ "print \"Vector v1: \",\n",
+ "v1.show()\n",
+ "print \"\\nvector v2: \",\n",
+ "v2.show()\n",
+ "print \"\\nvector v3: \",\n",
+ "v3.show()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Copy construcor invoked\n",
+ "Vector v1: 1 , 2 , 3 , 4 , 5 , \n",
+ "vector v2: 1 , 2 , 3 , 4 , 5 , \n",
+ "vector v3: 1 , 2 , 3 , 4 , 5 , \n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example-matrix.cpp, Page-418"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "TRUE=1\n",
+ "FALSE=0\n",
+ "def __del__(self):\n",
+ " for i in range(self._matrix__MaxRow):\n",
+ " del self._matrix__p[i]\n",
+ " del self._matrix__p\n",
+ "def add(self, a, b):\n",
+ " self._matrix__MaxRow=a._matrix__MaxRow\n",
+ " self._matrix__MaxCol=a._matrix__MaxCol\n",
+ " if (a._matrix__MaxRow!=b._matrix__MaxRow)|(a._matrix__MaxCol!=b._matrix__MaxCol):\n",
+ " print \"Error: invalid matrix order for addition\"\n",
+ " return\n",
+ " for i in range(self._matrix__MaxRow):\n",
+ " for j in range(self._matrix__MaxCol):\n",
+ " self._matrix__p[i][j]=a._matrix__p[i][j]+b._matrix__p[i][j]\n",
+ "def sub(self, a, b):\n",
+ " self._matrix__MaxRow=a._matrix__MaxRow\n",
+ " self._matrix__MaxCol=a._matrix__MaxCol\n",
+ " if (a._matrix__MaxRow!=b._matrix__MaxRow)|(a._matrix__MaxCol!=b._matrix__MaxCol):\n",
+ " print \"Error: invalid matrix order for subtraction\"\n",
+ " return\n",
+ " for i in range(self._matrix__MaxRow):\n",
+ " for j in range(self._matrix__MaxCol):\n",
+ " self._matrix__p[i][j]=a._matrix__p[i][j]-b._matrix__p[i][j]\n",
+ "def mul(self, a, b):\n",
+ " self._matrix__MaxRow=a._matrix__MaxRow\n",
+ " self._matrix__MaxCol=a._matrix__MaxCol\n",
+ " if (a._matrix__MaxCol!=b._matrix__MaxRow):\n",
+ " print \"Error: invalid matrix order for multiplication\"\n",
+ " return\n",
+ " for i in range(a._matrix__MaxRow):\n",
+ " for j in range(b._matrix__MaxCol):\n",
+ " self._matrix__p[i][j]=0\n",
+ " for k in range(a._matrix__MaxCol):\n",
+ " self._matrix__p[i][j]+=a._matrix__p[i][j]*b._matrix__p[i][j]\n",
+ "def eql(self, b):\n",
+ " for i in range(self._matrix__MaxRow):\n",
+ " for j in range(self._matrix__MaxCol):\n",
+ " if self._matrix__p[i][i]!=b._matrix__p[i][j]:\n",
+ " return 0\n",
+ " return 1\n",
+ "def read(self):\n",
+ " for i in range(self._matrix__MaxRow):\n",
+ " for j in range(self._matrix__MaxCol):\n",
+ " print \"Matrix[\", i, \",\",j,\"] =? \",\n",
+ " self._matrix__p[i][j]=int(raw_input())\n",
+ "def show(self):\n",
+ " for i in range(self._matrix__MaxRow):\n",
+ " for j in range(self._matrix__MaxCol):\n",
+ " print self._matrix__p[i][j], \" \",\n",
+ " print \"\"\n",
+ "class matrix:\n",
+ " __MaxRow=int\n",
+ " __MaxCol=int\n",
+ " __p=[int]\n",
+ " def __init__(self, row=0, col=0):\n",
+ " self.__MaxRow=row\n",
+ " self.__MaxCol=col\n",
+ " if row>0:\n",
+ " self.__p=[[int]*self.__MaxCol]*self.__MaxRow\n",
+ " __del__=__del__\n",
+ " read=read\n",
+ " show=show\n",
+ " add=add\n",
+ " sub=sub\n",
+ " mul=mul\n",
+ " eql=eql\n",
+ "print \"Enter Matrix A details...\"\n",
+ "m=int(raw_input(\"How many rows? \"))\n",
+ "n=int(raw_input(\"How many columns? \"))\n",
+ "a=matrix(m,n)\n",
+ "a.read()\n",
+ "print \"Enter Matrix B details...\"\n",
+ "p=int(raw_input(\"How many rows? \"))\n",
+ "q=int(raw_input(\"How many columns? \"))\n",
+ "b=matrix(p,q)\n",
+ "b.read()\n",
+ "print \"Matrix A is...\"\n",
+ "a.show()\n",
+ "print \"Matrix B is...\"\n",
+ "b.show()\n",
+ "c=matrix(m,n)\n",
+ "c.add(a,b)\n",
+ "print \"C=A+B...\"\n",
+ "c.show()\n",
+ "d=matrix(m,n)\n",
+ "d.sub(a,b)\n",
+ "print \"D=A-B...\"\n",
+ "d.show()\n",
+ "e=matrix(m,q)\n",
+ "e.mul(a,b)\n",
+ "print \"E=A*B...\"\n",
+ "e.show()\n",
+ "print \"(Is matrix A equal to matrix B)? \",\n",
+ "if(a.eql(b)):\n",
+ " print \"Yes\"\n",
+ "else:\n",
+ " print \"No\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Matrix A details...\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many rows? 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many columns? 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Matrix[ 0 , 0 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 0 , 1 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 0 , 2 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 1 , 0 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 1 , 1 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 1 , 2 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 2 , 0 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 2 , 1 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 2 , 2 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter Matrix B details...\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many rows? 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many columns? 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Matrix[ 0 , 0 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 0 , 1 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 0 , 2 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 1 , 0 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 1 , 1 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 1 , 2 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 2 , 0 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 2 , 1 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix[ 2 , 2 ] =? "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Matrix A is...\n",
+ "2 2 2 \n",
+ "2 2 2 \n",
+ "2 2 2 \n",
+ "Matrix B is...\n",
+ "1 1 1 \n",
+ "1 1 1 \n",
+ "1 1 1 \n",
+ "C=A+B...\n",
+ "3 3 3 \n",
+ "3 3 3 \n",
+ "3 3 3 \n",
+ "D=A-B...\n",
+ "1 1 1 \n",
+ "1 1 1 \n",
+ "1 1 1 \n",
+ "E=A*B...\n",
+ "6 6 6 \n",
+ "6 6 6 \n",
+ "6 6 6 \n",
+ "(Is matrix A equal to matrix B)? No\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example-person.cpp, Page-423"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def __init__(self, NameIn, AddressIn, PhoneIn):\n",
+ " self._Person__name=NameIn\n",
+ " self._Person__address=AddressIn\n",
+ " self._Person__phone=PhoneIn\n",
+ "#inline\n",
+ "def __del__(self):\n",
+ " del self._Person__name\n",
+ " del self._Person__address\n",
+ " del self._Person__phone\n",
+ "def getname(self):\n",
+ " return self._Person__name\n",
+ "def getaddress(self):\n",
+ " return self._Person__address\n",
+ "def getphone(self):\n",
+ " return self._Person__phone\n",
+ "def changename(self, NameIn):\n",
+ " if(self._Person__name):\n",
+ " del self._Person__name\n",
+ " self._Person__name=NameIn\n",
+ "class Person:\n",
+ " __name=[str]\n",
+ " __address=[str]\n",
+ " __phone=[str]\n",
+ " __init__=__init__\n",
+ " __del__=__del__\n",
+ " getname=getname\n",
+ " getaddress=getaddress\n",
+ " getphone=getphone\n",
+ " changename=changename\n",
+ "def printperson(p):\n",
+ " if(p.getname()):\n",
+ " print \"Name: \", p.getname()\n",
+ " if(p.getaddress()):\n",
+ " print \"Address: \", p.getaddress()\n",
+ " if(p.getphone()):\n",
+ " print \"Phone: \", p.getphone()\n",
+ "me=Person(\"Rajkumar\", \"E-mail: raj@cdabc.erne.in\", \"91-080-5584271\")\n",
+ "printperson(me)\n",
+ "you=Person(\"XYZ\", \"-not sure-\", \"-not sure-\")\n",
+ "print \"You XYZ by default...\"\n",
+ "printperson(you)\n",
+ "you.changename(\"ABC\")\n",
+ "print \"You changed XYZ to ABC...\"\n",
+ "printperson(you)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name: Rajkumar\n",
+ "Address: E-mail: raj@cdabc.erne.in\n",
+ "Phone: 91-080-5584271\n",
+ "You XYZ by default...\n",
+ "Name: XYZ\n",
+ "Address: -not sure-\n",
+ "Phone: -not sure-\n",
+ "You changed XYZ to ABC...\n",
+ "Name: ABC\n",
+ "Address: -not sure-\n",
+ "Phone: -not sure-\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example-graph.cpp, Page-425"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def __init__(self):\n",
+ " if(self._Graphics__nobjects[0]==False):\n",
+ " self._Graphics__setgraphicsmode()\n",
+ " self._Graphics__nobjects[0]+=1\n",
+ "def __del__(self):\n",
+ " self._Graphics__nobjects[0]-=1\n",
+ " if(self._Graphics__nobjects[0]==False):\n",
+ " self._Graphics__settextmode()\n",
+ "class Graphics:\n",
+ " __nobjects=[0]\n",
+ " def __setgraphicsmode(self):\n",
+ " pass\n",
+ " def __settextmode(self):\n",
+ " pass\n",
+ " __init__=__init__\n",
+ " __del__=__del__\n",
+ " def getcount(self):\n",
+ " return self.__nobjects[0]\n",
+ "def my_func():\n",
+ " obj=Graphics()\n",
+ " print \"No. of Graphics' objects while in my_func=\", obj.getcount()\n",
+ "obj1=Graphics()\n",
+ "print \"No. of Graphics' objects before in my_func=\", obj1.getcount()\n",
+ "my_func()\n",
+ "print \"No. of Graphics' objects after in my_func=\", obj1.getcount()\n",
+ "obj2=Graphics()\n",
+ "obj3=Graphics()\n",
+ "obj4=Graphics()\n",
+ "print \"Value of static member nobjects after all 3 more objects...\"\n",
+ "print \"In obj1= \", obj1.getcount()\n",
+ "print \"In obj2= \", obj2.getcount()\n",
+ "print \"In obj3= \", obj3.getcount()\n",
+ "print \"In obj4= \", obj4.getcount()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "No. of Graphics' objects before in my_func= 1\n",
+ "No. of Graphics' objects while in my_func= 2\n",
+ "No. of Graphics' objects after in my_func= 1\n",
+ "Value of static member nobjects after all 3 more objects...\n",
+ "In obj1= 4\n",
+ "In obj2= 4\n",
+ "In obj3= 4\n",
+ "In obj4= 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example Page-428"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def distance(self, a, b):\n",
+ " self.x=a.x-b.x\n",
+ " self.y=a.y-b.y\n",
+ "def display(self):\n",
+ " print \"x= \",self.x\n",
+ " print \"y= \", self.y\n",
+ "class point:\n",
+ " __x=int\n",
+ " __y=int\n",
+ " def __init__(self, a=None, b=None):\n",
+ " if isinstance(a, int):\n",
+ " self.x=a\n",
+ " self.y=b\n",
+ " else:\n",
+ " self.x=self.y=0\n",
+ " def __del__(self):\n",
+ " pass\n",
+ " distance=distance\n",
+ " display=display\n",
+ "p1=point(40,18)\n",
+ "p2=point(12,9)\n",
+ "p3=point()\n",
+ "p3.distance(p1,p2)\n",
+ "print \"Coordinates of P1: \"\n",
+ "p1.display()\n",
+ "print \"Coordinates of P2: \"\n",
+ "p2.display()\n",
+ "print \"distance between P1 and P2: \"\n",
+ "p3.display()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Coordinates of P1: \n",
+ "x= 40\n",
+ "y= 18\n",
+ "Coordinates of P2: \n",
+ "x= 12\n",
+ "y= 9\n",
+ "distance between P1 and P2: \n",
+ "x= 28\n",
+ "y= 9\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example Page-430"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def display(self):\n",
+ " print \"a =\", self.a,\n",
+ " print \"b =\", self.b\n",
+ "class data:\n",
+ " __a=int\n",
+ " __b=float\n",
+ " def __init__(self, x=None, y=None):\n",
+ " if isinstance(x, int):\n",
+ " self.a=x\n",
+ " self.b=y\n",
+ " elif isinstance(x, data):\n",
+ " self.a=x.a\n",
+ " self.b=x.b\n",
+ " else:\n",
+ " self.a=0\n",
+ " self.b=0\n",
+ " display=display\n",
+ "d1=data()\n",
+ "d2=data(12,9.9)\n",
+ "d3=data(d2)\n",
+ "print \"For default constructor: \"\n",
+ "d1.display()\n",
+ "print\"For parameterized constructor: \"\n",
+ "d2.display()\n",
+ "print \"For Copy Constructor: \"\n",
+ "d3.display()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "For default constructor: \n",
+ "a = 0 b = 0\n",
+ "For parameterized constructor: \n",
+ "a = 12 b = 9.9\n",
+ "For Copy Constructor: \n",
+ "a = 12 b = 9.9\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file