summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_16(1).ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C++_from_the_Ground/Chapter_16(1).ipynb')
-rw-r--r--C++_from_the_Ground/Chapter_16(1).ipynb487
1 files changed, 448 insertions, 39 deletions
diff --git a/C++_from_the_Ground/Chapter_16(1).ipynb b/C++_from_the_Ground/Chapter_16(1).ipynb
index afb4b4f1..941febba 100644
--- a/C++_from_the_Ground/Chapter_16(1).ipynb
+++ b/C++_from_the_Ground/Chapter_16(1).ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 16"
+ "name": "",
+ "signature": "sha256:bbf24dca4298d6f4bb5217612c0027ad0473157ec0c530ae82f2e0944bb06ddf"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,24 +11,62 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h1>Chapter 16: Templates<h1>"
+ "source": [
+ "<h1>Chapter 16: Templates<h1>"
+ ]
},
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.1, Page Number: 376<h3>"
+ "source": [
+ "<h3>Example 16.1, Page Number: 376<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementation of templates'''\n\n#The concept of template in in-built in python,hence this is a function template\ndef swapargs(a,b):\n temp=a[0]\n a[0]=b[0]\n b[0]=temp\n\n#Variable declaration\ni=[10]\nj=[20]\nx=[10.1]\ny=[23.3]\na=['x']\nb=['z']\n\nprint \"Original i, j: \",i[0],j[0]\nprint \"Original x,y: \",x[0],y[0]\nprint \"Original a,b: \",a[0],b[0]\n\nswapargs(i,j) #swap integers\nswapargs(x,y) #swap floats\nswapargs(a,b) #swap chars\n\n#Result\nprint \"Swapped i, j: \",i[0],j[0]\nprint \"Swapped x,y: \",x[0],y[0]\nprint \"Swapped a,b: \",a[0],b[0]",
+ "input": [
+ "\n",
+ "def swapargs(a,b):\n",
+ " temp=a[0]\n",
+ " a[0]=b[0]\n",
+ " b[0]=temp\n",
+ "\n",
+ "#Variable declaration\n",
+ "i=[10]\n",
+ "j=[20]\n",
+ "x=[10.1]\n",
+ "y=[23.3]\n",
+ "a=['x']\n",
+ "b=['z']\n",
+ "\n",
+ "print \"Original i, j: \",i[0],j[0]\n",
+ "print \"Original x,y: \",x[0],y[0]\n",
+ "print \"Original a,b: \",a[0],b[0]\n",
+ "\n",
+ "swapargs(i,j) #swap integers\n",
+ "swapargs(x,y) #swap floats\n",
+ "swapargs(a,b) #swap chars\n",
+ "\n",
+ "#Result\n",
+ "print \"Swapped i, j: \",i[0],j[0]\n",
+ "print \"Swapped x,y: \",x[0],y[0]\n",
+ "print \"Swapped a,b: \",a[0],b[0]"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Original i, j: 10 20\nOriginal x,y: 10.1 23.3\nOriginal a,b: x z\nSwapped i, j: 20 10\nSwapped x,y: 23.3 10.1\nSwapped a,b: z x\n"
+ "text": [
+ "Original i, j: 10 20\n",
+ "Original x,y: 10.1 23.3\n",
+ "Original a,b: x z\n",
+ "Swapped i, j: 20 10\n",
+ "Swapped x,y: 23.3 10.1\n",
+ "Swapped a,b: z x\n"
+ ]
}
],
"prompt_number": 1
@@ -35,19 +74,31 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.2, Page Number: 378<h3>"
+ "source": [
+ "<h3>Example 16.2, Page Number: 378<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Template for two generic types'''\n\ndef myfunc(x,y):\n print x,y\n \nmyfunc(10,\"hi\")\nmyfunc(0.23,10L)",
+ "input": [
+ "\n",
+ "def myfunc(x,y):\n",
+ " print x,y\n",
+ " \n",
+ "myfunc(10,\"hi\")\n",
+ "myfunc(0.23,10L)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10 hi\n0.23 10\n"
+ "text": [
+ "10 hi\n",
+ "0.23 10\n"
+ ]
}
],
"prompt_number": 3
@@ -55,19 +106,65 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.3, Page Number: 379<h3>"
+ "source": [
+ "<h3>Example 16.3, Page Number: 379<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Overloading generic functions'''\n\ndef swapargs(a,b):\n if isinstance(a[0],int): #integer version\n temp=a[0]\n a[0]=b[0]\n b[0]=temp\n print \"Inside swapargs int specialization.\"\n else: #generic version\n temp=a[0]\n a[0]=b[0]\n b[0]=temp\n print \"Inside template swapargs.\"\n\n#Variable declaration\ni=[10]\nj=[20]\nx=[10.1]\ny=[23.3]\na=['x']\nb=['z']\n\nprint \"Original i, j: \",i[0],j[0]\nprint \"Original x,y: \",x[0],y[0]\nprint \"Original a,b: \",a[0],b[0]\n\nswapargs(i,j) #calls explicitly overloaded swapargs()\nswapargs(x,y) #calls generic swapargs()\nswapargs(a,b) #calls generic swapargs()\n\n#Result\nprint \"Swapped i, j: \",i[0],j[0]\nprint \"Swapped x,y: \",x[0],y[0]\nprint \"Swapped a,b: \",a[0],b[0]",
+ "input": [
+ "\n",
+ "def swapargs(a,b):\n",
+ " if isinstance(a[0],int): #integer version\n",
+ " temp=a[0]\n",
+ " a[0]=b[0]\n",
+ " b[0]=temp\n",
+ " print \"Inside swapargs int specialization.\"\n",
+ " else: #generic version\n",
+ " temp=a[0]\n",
+ " a[0]=b[0]\n",
+ " b[0]=temp\n",
+ " print \"Inside template swapargs.\"\n",
+ "\n",
+ "#Variable declaration\n",
+ "i=[10]\n",
+ "j=[20]\n",
+ "x=[10.1]\n",
+ "y=[23.3]\n",
+ "a=['x']\n",
+ "b=['z']\n",
+ "\n",
+ "print \"Original i, j: \",i[0],j[0]\n",
+ "print \"Original x,y: \",x[0],y[0]\n",
+ "print \"Original a,b: \",a[0],b[0]\n",
+ "\n",
+ "swapargs(i,j) #calls explicitly overloaded swapargs()\n",
+ "swapargs(x,y) #calls generic swapargs()\n",
+ "swapargs(a,b) #calls generic swapargs()\n",
+ "\n",
+ "#Result\n",
+ "print \"Swapped i, j: \",i[0],j[0]\n",
+ "print \"Swapped x,y: \",x[0],y[0]\n",
+ "print \"Swapped a,b: \",a[0],b[0]"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Original i, j: 10 20\nOriginal x,y: 10.1 23.3\nOriginal a,b: x z\nInside swapargs int specialization.\nInside template swapargs.\nInside template swapargs.\nSwapped i, j: 20 10\nSwapped x,y: 23.3 10.1\nSwapped a,b: z x\n"
+ "text": [
+ "Original i, j: 10 20\n",
+ "Original x,y: 10.1 23.3\n",
+ "Original a,b: x z\n",
+ "Inside swapargs int specialization.\n",
+ "Inside template swapargs.\n",
+ "Inside template swapargs.\n",
+ "Swapped i, j: 20 10\n",
+ "Swapped x,y: 23.3 10.1\n",
+ "Swapped a,b: z x\n"
+ ]
}
],
"prompt_number": 2
@@ -75,19 +172,34 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.4, Page Number: 381<h3>"
+ "source": [
+ "<h3>Example 16.4, Page Number: 381<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Overload a function template declaration'''\n\n#Function version of f() template\ndef f(a,b=None):\n if(b==None): #First version of f()\n print \"Inside f(X a)\"\n else: #Second version of f()\n print \"Inside f(X a, Y b)\"\n \nf(10) #calls f(X)\nf(10,20) #calls f(X,Y)",
+ "input": [
+ "\n",
+ "def f(a,b=None):\n",
+ " if(b==None): #First version of f()\n",
+ " print \"Inside f(X a)\"\n",
+ " else: #Second version of f()\n",
+ " print \"Inside f(X a, Y b)\"\n",
+ " \n",
+ "f(10) #calls f(X)\n",
+ "f(10,20) #calls f(X,Y)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Inside f(X a)\nInside f(X a, Y b)\n"
+ "text": [
+ "Inside f(X a)\n",
+ "Inside f(X a, Y b)\n"
+ ]
}
],
"prompt_number": 4
@@ -95,19 +207,45 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.5, Page Number: 382<h3>"
+ "source": [
+ "<h3>Example 16.5, Page Number: 382<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementing templates in python'''\n\n#Display data specified number of times.\ndef repeat(data,times):\n while times:\n print data\n times-=1\n \nrepeat(\"This is a test\",3)\nrepeat(100,5)\nrepeat(99.0/2, 4)\n",
+ "input": [
+ "\n",
+ "#Display data specified number of times.\n",
+ "def repeat(data,times):\n",
+ " while times:\n",
+ " print data\n",
+ " times-=1\n",
+ " \n",
+ "repeat(\"This is a test\",3)\n",
+ "repeat(100,5)\n",
+ "repeat(99.0/2, 4)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "This is a test\nThis is a test\nThis is a test\n100\n100\n100\n100\n100\n49.5\n49.5\n49.5\n49.5\n"
+ "text": [
+ "This is a test\n",
+ "This is a test\n",
+ "This is a test\n",
+ "100\n",
+ "100\n",
+ "100\n",
+ "100\n",
+ "100\n",
+ "49.5\n",
+ "49.5\n",
+ "49.5\n",
+ "49.5\n"
+ ]
}
],
"prompt_number": 5
@@ -115,19 +253,39 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.6, Page Number: 383<h3>"
+ "source": [
+ "<h3>Example 16.6, Page Number: 383<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''A generic version of myabs()'''\n\ndef myabs(val):\n if val<0:\n return -val\n else:\n return val\n\n#Result\nprint myabs(-10)\nprint myabs(-10.0)\nprint myabs(-10L)\nprint myabs(-10.0)\n",
+ "input": [
+ "\n",
+ "def myabs(val):\n",
+ " if val<0:\n",
+ " return -val\n",
+ " else:\n",
+ " return val\n",
+ "\n",
+ "#Result\n",
+ "print myabs(-10)\n",
+ "print myabs(-10.0)\n",
+ "print myabs(-10L)\n",
+ "print myabs(-10.0)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10\n10.0\n10\n10.0\n"
+ "text": [
+ "10\n",
+ "10.0\n",
+ "10\n",
+ "10.0\n"
+ ]
}
],
"prompt_number": 6
@@ -135,19 +293,73 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.7, Page Number: 385<h3>"
+ "source": [
+ "<h3>Example 16.7, Page Number: 385<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementing a generic queue class'''\n\ndef SIZE():\n return 100\nclass queue:\n def __init__(self):\n self.q=[]\n self.sloc=self.rloc=0\n #Put an object into the queue\n def qput(self,i):\n if self.sloc==SIZE():\n print \"Queue is full.\"\n return\n self.sloc+=1\n self.q.append(i)\n #Get an object from the queue.\n def qget(self):\n if self.rloc==self.sloc:\n print \"Queue Underflow.\"\n return\n a=self.rloc\n self.rloc+=1\n return self.q[a]\n \n#Create two integer queues\na=queue()\nb=queue()\na.qput(10)\nb.qput(19)\na.qput(20)\nb.qput(1)\n\nprint a.qget(),\nprint a.qget(),\nprint b.qget(),\nprint b.qget()\n\n#Create two double queues\nc=queue()\nd=queue()\nc.qput(10.12)\nd.qput(19.99)\nc.qput(-20.0)\nd.qput(0.986)\n\nprint c.qget(),\nprint c.qget(),\nprint d.qget(),\nprint d.qget()\n",
+ "input": [
+ "\n",
+ "def SIZE():\n",
+ " return 100\n",
+ "class queue:\n",
+ " def __init__(self):\n",
+ " self.q=[]\n",
+ " self.sloc=self.rloc=0\n",
+ " #Put an object into the queue\n",
+ " def qput(self,i):\n",
+ " if self.sloc==SIZE():\n",
+ " print \"Queue is full.\"\n",
+ " return\n",
+ " self.sloc+=1\n",
+ " self.q.append(i)\n",
+ " #Get an object from the queue.\n",
+ " def qget(self):\n",
+ " if self.rloc==self.sloc:\n",
+ " print \"Queue Underflow.\"\n",
+ " return\n",
+ " a=self.rloc\n",
+ " self.rloc+=1\n",
+ " return self.q[a]\n",
+ " \n",
+ "#Create two integer queues\n",
+ "a=queue()\n",
+ "b=queue()\n",
+ "a.qput(10)\n",
+ "b.qput(19)\n",
+ "a.qput(20)\n",
+ "b.qput(1)\n",
+ "\n",
+ "print a.qget(),\n",
+ "print a.qget(),\n",
+ "print b.qget(),\n",
+ "print b.qget()\n",
+ "\n",
+ "#Create two double queues\n",
+ "c=queue()\n",
+ "d=queue()\n",
+ "c.qput(10.12)\n",
+ "d.qput(19.99)\n",
+ "c.qput(-20.0)\n",
+ "d.qput(0.986)\n",
+ "\n",
+ "print c.qget(),\n",
+ "print c.qget(),\n",
+ "print d.qget(),\n",
+ "print d.qget()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10 20 19 1\n10.12 -20.0 19.99 0.986\n"
+ "text": [
+ "10 20 19 1\n",
+ "10.12 -20.0 19.99 0.986\n"
+ ]
}
],
"prompt_number": 7
@@ -155,19 +367,39 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.8, Page Number: 387<h3>"
+ "source": [
+ "<h3>Example 16.8, Page Number: 387<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Two generic types in a class'''\n\nclass myclass:\n def __init__(self,a,b):\n self.__i=a\n self.__j=b\n def show(self):\n print self.__i,self.__j\n\nob1=myclass(10,0.23)\nob2=myclass('X',\"This is a test\")\n\n#Result\nob1.show() #Show int,double\nob2.show() #Show char.char*\n",
+ "input": [
+ "\n",
+ "class myclass:\n",
+ " def __init__(self,a,b):\n",
+ " self.__i=a\n",
+ " self.__j=b\n",
+ " def show(self):\n",
+ " print self.__i,self.__j\n",
+ "\n",
+ "ob1=myclass(10,0.23)\n",
+ "ob2=myclass('X',\"This is a test\")\n",
+ "\n",
+ "#Result\n",
+ "ob1.show() #Show int,double\n",
+ "ob2.show() #Show char.char*\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10 0.23\nX This is a test\n"
+ "text": [
+ "10 0.23\n",
+ "X This is a test\n"
+ ]
}
],
"prompt_number": 8
@@ -175,19 +407,69 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.9, Page Number: 388<h3>"
+ "source": [
+ "<h3>Example 16.9, Page Number: 388<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''A generic safe array example'''\n\n#Defining constant\ndef SIZE():\n return 10\n\nclass atype:\n def __init__(self):\n self.a=[]\n for i in range(SIZE()):\n self.a.append(i)\n #Implementing the [] overloading\n def op1(self,i,j):\n if (i<0 or i>=SIZE()):\n print \"\\nIndex value of \",\n print i,\" is out-of-bounds.\"\n return \n self.a[i]=j\n def op2(self,i):\n if (i<0 or i>SIZE()-1):\n print \"\\nIndex value of \",\n print i,\" is out-of-bounds.\"\n return\n return self.a[i]\n \n#Variable declaration\nintob=atype()\ndoubleob=atype()\n \nprint \"Integer array: \",\nfor i in range(SIZE()):\n intob.op1(i,i)\nfor i in range(SIZE()):\n print intob.op2(i),\n \nprint \"\"\n\nprint \"Double array: \",\nfor i in range(SIZE()):\n doubleob.op1(i,float(i)/3)\nfor i in range(SIZE()):\n print doubleob.op2(i),\n \nintob.op1(12,100)",
+ "input": [
+ "\n",
+ "#Defining constant\n",
+ "def SIZE():\n",
+ " return 10\n",
+ "\n",
+ "class atype:\n",
+ " def __init__(self):\n",
+ " self.a=[]\n",
+ " for i in range(SIZE()):\n",
+ " self.a.append(i)\n",
+ " #Implementing the [] overloading\n",
+ " def op1(self,i,j):\n",
+ " if (i<0 or i>=SIZE()):\n",
+ " print \"\\nIndex value of \",\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " return \n",
+ " self.a[i]=j\n",
+ " def op2(self,i):\n",
+ " if (i<0 or i>SIZE()-1):\n",
+ " print \"\\nIndex value of \",\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " return\n",
+ " return self.a[i]\n",
+ " \n",
+ "#Variable declaration\n",
+ "intob=atype()\n",
+ "doubleob=atype()\n",
+ " \n",
+ "print \"Integer array: \",\n",
+ "for i in range(SIZE()):\n",
+ " intob.op1(i,i)\n",
+ "for i in range(SIZE()):\n",
+ " print intob.op2(i),\n",
+ " \n",
+ "print \"\"\n",
+ "\n",
+ "print \"Double array: \",\n",
+ "for i in range(SIZE()):\n",
+ " doubleob.op1(i,float(i)/3)\n",
+ "for i in range(SIZE()):\n",
+ " print doubleob.op2(i),\n",
+ " \n",
+ "intob.op1(12,100)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Integer array: 0 1 2 3 4 5 6 7 8 9 \nDouble array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \nIndex value of 12 is out-of-bounds.\n"
+ "text": [
+ "Integer array: 0 1 2 3 4 5 6 7 8 9 \n",
+ "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \n",
+ "Index value of 12 is out-of-bounds.\n"
+ ]
}
],
"prompt_number": 1
@@ -195,19 +477,66 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.10, Page Number: 389<h3>"
+ "source": [
+ "<h3>Example 16.10, Page Number: 389<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementing non-type template arguments'''\n\nclass atype:\n def __init__(self,size):\n self.a=[]\n self.size=size\n for i in range(size):\n self.a.append(i)\n #Implementing the [] overloading\n def op1(self,i,j):\n if (i<0 or i>self.size-1):\n print \"\\nIndex value of \",\n print i,\" is out-of-bounds.\"\n return\n self.a[i]=j\n def op2(self,i):\n if (i<0 or i>self.size-1):\n print \"\\nIndex value of \",\n print i,\" is out-of-bounds.\"\n return\n return self.a[i]\n \n#Variable declaration\nintob=atype(10)\ndoubleob=atype(15)\n \nprint \"Integer array: \",\nfor i in range(10):\n intob.op1(i,i)\nfor i in range(10):\n print intob.op2(i),\n \nprint \"\"\n\nprint \"Double array: \",\nfor i in range(15):\n doubleob.op1(i,float(i)/3)\nfor i in range(15):\n print doubleob.op2(i),\n \nintob.op1(12,100) #generates runtime error\n",
+ "input": [
+ "\n",
+ "class atype:\n",
+ " def __init__(self,size):\n",
+ " self.a=[]\n",
+ " self.size=size\n",
+ " for i in range(size):\n",
+ " self.a.append(i)\n",
+ " #Implementing the [] overloading\n",
+ " def op1(self,i,j):\n",
+ " if (i<0 or i>self.size-1):\n",
+ " print \"\\nIndex value of \",\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " return\n",
+ " self.a[i]=j\n",
+ " def op2(self,i):\n",
+ " if (i<0 or i>self.size-1):\n",
+ " print \"\\nIndex value of \",\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " return\n",
+ " return self.a[i]\n",
+ " \n",
+ "#Variable declaration\n",
+ "intob=atype(10)\n",
+ "doubleob=atype(15)\n",
+ " \n",
+ "print \"Integer array: \",\n",
+ "for i in range(10):\n",
+ " intob.op1(i,i)\n",
+ "for i in range(10):\n",
+ " print intob.op2(i),\n",
+ " \n",
+ "print \"\"\n",
+ "\n",
+ "print \"Double array: \",\n",
+ "for i in range(15):\n",
+ " doubleob.op1(i,float(i)/3)\n",
+ "for i in range(15):\n",
+ " print doubleob.op2(i),\n",
+ " \n",
+ "intob.op1(12,100) #generates runtime error\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Integer array: 0 1 2 3 4 5 6 7 8 9 \nDouble array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 3.33333333333 3.66666666667 4.0 4.33333333333 4.66666666667 \nIndex value of 12 is out-of-bounds.\n"
+ "text": [
+ "Integer array: 0 1 2 3 4 5 6 7 8 9 \n",
+ "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 3.33333333333 3.66666666667 4.0 4.33333333333 4.66666666667 \n",
+ "Index value of 12 is out-of-bounds.\n"
+ ]
}
],
"prompt_number": 2
@@ -215,19 +544,73 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.11, Page Number: 391<h3>"
+ "source": [
+ "<h3>Example 16.11, Page Number: 391<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''A generic safe array example with default arguments'''\n\nclass atype:\n def __init__(self,size=10):\n self.a=[]\n for i in range(size):\n self.a.append(i)\n #Implementing the [] overloading\n def op1(self,i,j):\n if (i<0 and i>SIZE()-1):\n print \"Index value of \"\n print i,\" is out-of-bounds.\"\n exit(1)\n self.a[i]=j\n def op2(self,i):\n if (i<0 and i>SIZE()-1):\n print \"Index value of \"\n print i,\" is out-of-bounds.\"\n exit()\n return self.a[i]\n \n#Variable declaration\nintob=atype(100)\ndoubleob=atype()\ndefarray=atype()\n \nprint \"Integer array: \",\nfor i in range(100):\n intob.op1(i,i)\nfor i in range(100):\n print intob.op2(i),\n \nprint \"\"\n\nprint \"Double array: \",\nfor i in range(10):\n doubleob.op1(i,float(i)/3)\nfor i in range(10):\n print doubleob.op2(i),\n \nprint \"\"\n\nprint \"Defarray array: \",\nfor i in range(10):\n doubleob.op1(i,i)\nfor i in range(10):\n print doubleob.op2(i),\n\n",
+ "input": [
+ "\n",
+ "class atype:\n",
+ " def __init__(self,size=10):\n",
+ " self.a=[]\n",
+ " for i in range(size):\n",
+ " self.a.append(i)\n",
+ " #Implementing the [] overloading\n",
+ " def op1(self,i,j):\n",
+ " if (i<0 and i>SIZE()-1):\n",
+ " print \"Index value of \"\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " exit(1)\n",
+ " self.a[i]=j\n",
+ " def op2(self,i):\n",
+ " if (i<0 and i>SIZE()-1):\n",
+ " print \"Index value of \"\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " exit()\n",
+ " return self.a[i]\n",
+ " \n",
+ "#Variable declaration\n",
+ "intob=atype(100)\n",
+ "doubleob=atype()\n",
+ "defarray=atype()\n",
+ " \n",
+ "print \"Integer array: \",\n",
+ "for i in range(100):\n",
+ " intob.op1(i,i)\n",
+ "for i in range(100):\n",
+ " print intob.op2(i),\n",
+ " \n",
+ "print \"\"\n",
+ "\n",
+ "print \"Double array: \",\n",
+ "for i in range(10):\n",
+ " doubleob.op1(i,float(i)/3)\n",
+ "for i in range(10):\n",
+ " print doubleob.op2(i),\n",
+ " \n",
+ "print \"\"\n",
+ "\n",
+ "print \"Defarray array: \",\n",
+ "for i in range(10):\n",
+ " doubleob.op1(i,i)\n",
+ "for i in range(10):\n",
+ " print doubleob.op2(i),\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Integer array: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 \nDouble array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \nDefarray array: 0 1 2 3 4 5 6 7 8 9\n"
+ "text": [
+ "Integer array: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 \n",
+ "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \n",
+ "Defarray array: 0 1 2 3 4 5 6 7 8 9\n"
+ ]
}
],
"prompt_number": 3
@@ -235,19 +618,45 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 16.12, Page Number: 393<h3>"
+ "source": [
+ "<h3>Example 16.12, Page Number: 393<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Demonstrate class specialization'''\n\nclass myclass:\n def __init__(self,a):\n if isinstance(a,int):\n print \"Inside myclass<int>specialization\"\n self.__x=a*a\n else:\n print \"Inside generic myclass\"\n self.__x=a\n def getx(self):\n return self.__x\n \nd=myclass(10.1)\nprint \"double: \",d.getx(),\"\\n\"\n\ni=myclass(5)\nprint \"int: \",i.getx()\n",
+ "input": [
+ "\n",
+ "class myclass:\n",
+ " def __init__(self,a):\n",
+ " if isinstance(a,int):\n",
+ " print \"Inside myclass<int>specialization\"\n",
+ " self.__x=a*a\n",
+ " else:\n",
+ " print \"Inside generic myclass\"\n",
+ " self.__x=a\n",
+ " def getx(self):\n",
+ " return self.__x\n",
+ " \n",
+ "d=myclass(10.1)\n",
+ "print \"double: \",d.getx(),\"\\n\"\n",
+ "\n",
+ "i=myclass(5)\n",
+ "print \"int: \",i.getx()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Inside generic myclass\ndouble: 10.1 \n\nInside myclass<int>specialization\nint: 25\n"
+ "text": [
+ "Inside generic myclass\n",
+ "double: 10.1 \n",
+ "\n",
+ "Inside myclass<int>specialization\n",
+ "int: 25\n"
+ ]
}
],
"prompt_number": 4
@@ -255,7 +664,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []