From 83c1bfceb1b681b4bb7253b47491be2d8b2014a1 Mon Sep 17 00:00:00 2001
From: debashisdeb
Date: Fri, 20 Jun 2014 15:42:42 +0530
Subject: removing problem statements
---
C++_from_the_Ground/Chapter_10(1).ipynb | 414 +++++++-
C++_from_the_Ground/Chapter_11(1).ipynb | 632 ++++++++++++-
C++_from_the_Ground/Chapter_12(1).ipynb | 648 +++++++++++--
C++_from_the_Ground/Chapter_13(1).ipynb | 694 +++++++++++++-
C++_from_the_Ground/Chapter_14(1).ipynb | 698 +++++++++++++-
C++_from_the_Ground/Chapter_15(1).ipynb | 322 ++++++-
C++_from_the_Ground/Chapter_16(1).ipynb | 487 +++++++++-
C++_from_the_Ground/Chapter_17(1).ipynb | 513 +++++++++-
C++_from_the_Ground/Chapter_18(1).ipynb | 600 ++++++++++--
C++_from_the_Ground/Chapter_19(1).ipynb | 551 ++++++++++-
C++_from_the_Ground/Chapter_2(1).ipynb | 243 ++++-
C++_from_the_Ground/Chapter_20(2).ipynb | 43 +-
C++_from_the_Ground/Chapter_21(1).ipynb | 902 ++++++++++++++++--
C++_from_the_Ground/Chapter_22(1).ipynb | 201 +++-
C++_from_the_Ground/Chapter_3(1).ipynb | 329 ++++++-
C++_from_the_Ground/Chapter_4(1).ipynb | 1063 +++++++++++++++++++--
C++_from_the_Ground/Chapter_5(1).ipynb | 731 ++++++++++++---
C++_from_the_Ground/Chapter_6(1).ipynb | 355 ++++++-
C++_from_the_Ground/Chapter_7(1).ipynb | 591 ++++++++++--
C++_from_the_Ground/Chapter_8(1).ipynb | 1553 ++++++++++++++++++++++++++++++-
C++_from_the_Ground/Chapter_9(1).ipynb | 547 +++++++++--
21 files changed, 11119 insertions(+), 998 deletions(-)
(limited to 'C++_from_the_Ground')
diff --git a/C++_from_the_Ground/Chapter_10(1).ipynb b/C++_from_the_Ground/Chapter_10(1).ipynb
index 6196736b..77fa05ce 100644
--- a/C++_from_the_Ground/Chapter_10(1).ipynb
+++ b/C++_from_the_Ground/Chapter_10(1).ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 10"
+ "name": "",
+ "signature": "sha256:369d1716e7a65538ad06381f5a1cdafc97ddfbad9ccd970d060ca5c894a6593b"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,24 +11,141 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "
Chapter 10: Structures and Unions"
+ "source": [
+ "Chapter 10: Structures and Unions"
+ ]
},
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 10.1, Page Number: 223"
+ "source": [
+ "Example 10.1, Page Number: 223"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''A simple inventory prohram that uses an array of structures/ '''\n'''Implementing structures in python'''\n\n\nclass inv_type:\n def __init__(self):\n self.item=None\n self.cost=0\n self.retail=0\n self.on_hand=0\n self.lead_time=0\n\n#Variable declaration\nsize=100 \ninvtry = []*size\ni=5 #User iput for menu selection\n\n#Initialize the array\ndef init_list():\n for t in range(size):\n invtry.append(inv_type())\n \n#get a menu selection\ndef menu():\n global i\n print \"(E)nter\"\n print \"(D)isplay\"\n print \"(U)pdate\"\n print \"(Q)uit\"\n print \"choose one: \"\n i-=1\n return i\n\n#enter items into the list\ndef enter():\n #find the first free structure\n for i in range(size):\n if not(invtry[i].item==None):\n break\n #i will be size if list is full\n if i==size:\n print \"List full.\"\n return\n input(i)\n \n#Input the information\ndef input(i):\n #Enter information; User input\n invtry[i].item=\"Gloves\"\n invtry[i].cost=10\n invtry[i].retail=25\n invtry[i].on_hand=50\n invtry[i].lead_time=10\n \n#Modify an existing item\ndef update():\n name=\"Gloves\" #User input\n for i in range(size):\n if not(name==invtry[i].item):\n break\n if i==size:\n print \"Item not found.\"\n return\n print \"Enter new information.\"\n input(i)\n \n#Display the list\ndef display():\n for t in range(size):\n if not(invtry[t].item==None):\n print invtry[t].item\n print \"Cost: $\",invtry[t].cost\n print \"Retail: $\",invtry[t].retail\n print \"On hand: \",invtry[t].on_hand\n print \"Resupply time: \",invtry[t].lead_time,\" days\"\n \n\ninit_list()\nwhile True:\n choice=menu()\n if choice==4:\n enter()\n elif choice==3:\n display()\n elif choice==2:\n update()\n elif choice==1:\n break",
+ "input": [
+ "\n",
+ "class inv_type:\n",
+ " def __init__(self):\n",
+ " self.item=None\n",
+ " self.cost=0\n",
+ " self.retail=0\n",
+ " self.on_hand=0\n",
+ " self.lead_time=0\n",
+ "\n",
+ "#Variable declaration\n",
+ "size=100 \n",
+ "invtry = []*size\n",
+ "i=5 #User iput for menu selection\n",
+ "\n",
+ "#Initialize the array\n",
+ "def init_list():\n",
+ " for t in range(size):\n",
+ " invtry.append(inv_type())\n",
+ " \n",
+ "#get a menu selection\n",
+ "def menu():\n",
+ " global i\n",
+ " print \"(E)nter\"\n",
+ " print \"(D)isplay\"\n",
+ " print \"(U)pdate\"\n",
+ " print \"(Q)uit\"\n",
+ " print \"choose one: \"\n",
+ " i-=1\n",
+ " return i\n",
+ "\n",
+ "#enter items into the list\n",
+ "def enter():\n",
+ " #find the first free structure\n",
+ " for i in range(size):\n",
+ " if not(invtry[i].item==None):\n",
+ " break\n",
+ " #i will be size if list is full\n",
+ " if i==size:\n",
+ " print \"List full.\"\n",
+ " return\n",
+ " input(i)\n",
+ " \n",
+ "#Input the information\n",
+ "def input(i):\n",
+ " #Enter information; User input\n",
+ " invtry[i].item=\"Gloves\"\n",
+ " invtry[i].cost=10\n",
+ " invtry[i].retail=25\n",
+ " invtry[i].on_hand=50\n",
+ " invtry[i].lead_time=10\n",
+ " \n",
+ "#Modify an existing item\n",
+ "def update():\n",
+ " name=\"Gloves\" #User input\n",
+ " for i in range(size):\n",
+ " if not(name==invtry[i].item):\n",
+ " break\n",
+ " if i==size:\n",
+ " print \"Item not found.\"\n",
+ " return\n",
+ " print \"Enter new information.\"\n",
+ " input(i)\n",
+ " \n",
+ "#Display the list\n",
+ "def display():\n",
+ " for t in range(size):\n",
+ " if not(invtry[t].item==None):\n",
+ " print invtry[t].item\n",
+ " print \"Cost: $\",invtry[t].cost\n",
+ " print \"Retail: $\",invtry[t].retail\n",
+ " print \"On hand: \",invtry[t].on_hand\n",
+ " print \"Resupply time: \",invtry[t].lead_time,\" days\"\n",
+ " \n",
+ "\n",
+ "init_list()\n",
+ "while True:\n",
+ " choice=menu()\n",
+ " if choice==4:\n",
+ " enter()\n",
+ " elif choice==3:\n",
+ " display()\n",
+ " elif choice==2:\n",
+ " update()\n",
+ " elif choice==1:\n",
+ " break"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "(E)nter\n(D)isplay\n(U)pdate\n(Q)uit\nchoose one: \n(E)nter\n(D)isplay\n(U)pdate\n(Q)uit\nchoose one: \nGloves\nCost: $ 10\nRetail: $ 25\nOn hand: 50\nResupply time: 10 days\n(E)nter\n(D)isplay\n(U)pdate\n(Q)uit\nchoose one: \nEnter new information.\n(E)nter\n(D)isplay\n(U)pdate\n(Q)uit\nchoose one: \n"
+ "text": [
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n",
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n",
+ "Gloves\n",
+ "Cost: $ 10\n",
+ "Retail: $ 25\n",
+ "On hand: 50\n",
+ "Resupply time: 10 days\n",
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n",
+ "Enter new information.\n",
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n"
+ ]
}
],
"prompt_number": 11
@@ -35,19 +153,43 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 10.2, Page Number: 226"
+ "source": [
+ "Example 10.2, Page Number: 226"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Pass a structure(class) to a function'''\n\n#Define a structure(class) type\nclass sample:\n a=None\n ch=None\n \ndef f1(parm):\n print parm.a,\" \",parm.ch\n\n#declare arg\narg=sample() \n\n#initialize arg\narg.a=1000\narg.ch='X'\n\n#call function\nf1(arg)\n ",
+ "input": [
+ "\n",
+ "\n",
+ "class sample:\n",
+ " a=None\n",
+ " ch=None\n",
+ " \n",
+ "def f1(parm):\n",
+ " print parm.a,\" \",parm.ch\n",
+ "\n",
+ "#declare arg\n",
+ "arg=sample() \n",
+ "\n",
+ "#initialize arg\n",
+ "arg.a=1000\n",
+ "arg.ch='X'\n",
+ "\n",
+ "#call function\n",
+ "f1(arg)\n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "1000 X\n"
+ "text": [
+ "1000 X\n"
+ ]
}
],
"prompt_number": 12
@@ -55,19 +197,52 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 10.3, Page Number: 227"
+ "source": [
+ "Example 10.3, Page Number: 227"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Demonstrate structure assignments.'''\n\nclass stype:\n a=None\n b=None\n\n#Variable declaration\nsvar1=stype()\nsvar2=stype()\n\nsvar1.a=svar1.b=10\nsvar2.a=svar2.b=20\n\nprint \"Structures before assignment.\"\nprint \"svar1: \",svar1.a,' ',svar1.b\nprint \"svar1: \",svar2.a,' ',svar2.b\n\nsvar2=svar1 #assign structures\n\n#Result\nprint \"\\nStructures before assignment.\"\nprint \"svar1: \",svar1.a,' ',svar1.b\nprint \"svar1: \",svar2.a,' ',svar2.b",
+ "input": [
+ "\n",
+ "class stype:\n",
+ " a=None\n",
+ " b=None\n",
+ "\n",
+ "#Variable declaration\n",
+ "svar1=stype()\n",
+ "svar2=stype()\n",
+ "\n",
+ "svar1.a=svar1.b=10\n",
+ "svar2.a=svar2.b=20\n",
+ "\n",
+ "print \"Structures before assignment.\"\n",
+ "print \"svar1: \",svar1.a,' ',svar1.b\n",
+ "print \"svar1: \",svar2.a,' ',svar2.b\n",
+ "\n",
+ "svar2=svar1 #assign structures\n",
+ "\n",
+ "#Result\n",
+ "print \"\\nStructures before assignment.\"\n",
+ "print \"svar1: \",svar1.a,' ',svar1.b\n",
+ "print \"svar1: \",svar2.a,' ',svar2.b"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Structures before assignment.\nsvar1: 10 10\nsvar1: 20 20\n\nStructures before assignment.\nsvar1: 10 10\nsvar1: 10 10\n"
+ "text": [
+ "Structures before assignment.\n",
+ "svar1: 10 10\n",
+ "svar1: 20 20\n",
+ "\n",
+ "Structures before assignment.\n",
+ "svar1: 10 10\n",
+ "svar1: 10 10\n"
+ ]
}
],
"prompt_number": 13
@@ -75,19 +250,32 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 10.4, Page Number: 230"
+ "source": [
+ "Example 10.4, Page Number: 230"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''This program displays the current system time.'''\n\nimport datetime\n\ndate=datetime.datetime.now()\n\n#Result\nprint date.time()",
+ "input": [
+ "\n",
+ "\n",
+ "import datetime\n",
+ "\n",
+ "date=datetime.datetime.now()\n",
+ "\n",
+ "#Result\n",
+ "print date.time()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "17:06:28.236000\n"
+ "text": [
+ "17:06:28.236000\n"
+ ]
}
],
"prompt_number": 14
@@ -95,19 +283,31 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 10.5, Page Number: 231"
+ "source": [
+ "Example 10.5, Page Number: 231"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''This program displays the current system time.'''\n\nimport datetime\n\ndate=datetime.datetime.now()\n\n#Result\nprint date.ctime()",
+ "input": [
+ "\n",
+ "import datetime\n",
+ "\n",
+ "date=datetime.datetime.now()\n",
+ "\n",
+ "#Result\n",
+ "print date.ctime()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Sat Sep 14 17:07:14 2013\n"
+ "text": [
+ "Sat Sep 14 17:07:14 2013\n"
+ ]
}
],
"prompt_number": 15
@@ -115,19 +315,55 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 10.6, Page Number: 232"
+ "source": [
+ "Example 10.6, Page Number: 232"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Demonstrate a reference to a structure'''\n\nclass mystruct:\n a=None\n b=None\n\n#Recieve and return by reference\ndef f(var):\n var[0].a=var[0].a*var[0].a\n var[0].b=var[0].b/var[0].b\n return var[0]\n \n#Variable declaration\nx=[]\nx.append(mystruct())\ny=mystruct()\n\n#Initializing\nx[0].a=10\nx[0].b=20\n\nprint \"Original x.a and x.b: \",x[0].a,' ',x[0].b\n\ny=f(x) #function call\n\n#Result\nprint \"Modified x.a and x.b: \",x[0].a,' ',x[0].b\nprint \"Modified y.a and y.b: \",y.a,' ',y.b\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "class mystruct:\n",
+ " a=None\n",
+ " b=None\n",
+ "\n",
+ "#Recieve and return by reference\n",
+ "def f(var):\n",
+ " var[0].a=var[0].a*var[0].a\n",
+ " var[0].b=var[0].b/var[0].b\n",
+ " return var[0]\n",
+ " \n",
+ "#Variable declaration\n",
+ "x=[]\n",
+ "x.append(mystruct())\n",
+ "y=mystruct()\n",
+ "\n",
+ "#Initializing\n",
+ "x[0].a=10\n",
+ "x[0].b=20\n",
+ "\n",
+ "print \"Original x.a and x.b: \",x[0].a,' ',x[0].b\n",
+ "\n",
+ "y=f(x) #function call\n",
+ "\n",
+ "#Result\n",
+ "print \"Modified x.a and x.b: \",x[0].a,' ',x[0].b\n",
+ "print \"Modified y.a and y.b: \",y.a,' ',y.b\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Original x.a and x.b: 10 20\nModified x.a and x.b: 100 1\nModified y.a and y.b: 100 1\n"
+ "text": [
+ "Original x.a and x.b: 10 20\n",
+ "Modified x.a and x.b: 100 1\n",
+ "Modified y.a and y.b: 100 1\n"
+ ]
}
],
"prompt_number": 16
@@ -135,19 +371,61 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 10.7, Page Number: 239"
+ "source": [
+ "Example 10.7, Page Number: 239"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Use a union to exchange the bytes within a short integer'''\n#Class used in place of union \n\nclass swap_bytes:\n ch=[0,0]\n\n#Exchange of bytes\ndef disp_binary(u):\n t=128\n while not(t==0):\n if u&t:\n print \"1 \",\n else:\n print \"0 \",\n t=t/2\n\n#Variable declaration\nsb=swap_bytes()\n\nsb.ch[0]=15\n\nprint \"Original bytes: \",\ndisp_binary(sb.ch[1])\ndisp_binary(sb.ch[0])\n\n#Exchange bytes\ntemp=sb.ch[0]\nsb.ch[0]=sb.ch[1]\nsb.ch[1]=temp\n\n#Result\nprint \"\\nExchanged bytes: \",\ndisp_binary(sb.ch[1])\ndisp_binary(sb.ch[0])\n\n\n ",
+ "input": [
+ "\n",
+ "\n",
+ "class swap_bytes:\n",
+ " ch=[0,0]\n",
+ "\n",
+ "#Exchange of bytes\n",
+ "def disp_binary(u):\n",
+ " t=128\n",
+ " while not(t==0):\n",
+ " if u&t:\n",
+ " print \"1 \",\n",
+ " else:\n",
+ " print \"0 \",\n",
+ " t=t/2\n",
+ "\n",
+ "#Variable declaration\n",
+ "sb=swap_bytes()\n",
+ "\n",
+ "sb.ch[0]=15\n",
+ "\n",
+ "print \"Original bytes: \",\n",
+ "disp_binary(sb.ch[1])\n",
+ "disp_binary(sb.ch[0])\n",
+ "\n",
+ "#Exchange bytes\n",
+ "temp=sb.ch[0]\n",
+ "sb.ch[0]=sb.ch[1]\n",
+ "sb.ch[1]=temp\n",
+ "\n",
+ "#Result\n",
+ "print \"\\nExchanged bytes: \",\n",
+ "disp_binary(sb.ch[1])\n",
+ "disp_binary(sb.ch[0])\n",
+ "\n",
+ "\n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Original bytes: 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 \nExchanged bytes: 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 \n"
+ "text": [
+ "Original bytes: 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 \n",
+ "Exchanged bytes: 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 \n"
+ ]
}
],
"prompt_number": 17
@@ -155,19 +433,66 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 10.8, Page Number: 240"
+ "source": [
+ "Example 10.8, Page Number: 240"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Display the ASCII code in binary for characters'''\n\n#Variable declaration\nch='a'\n\nwhile True:\n print \"\\n\",ch,\n print bin(ord(ch)) #Display the bit pattern for each character\n ch=chr(ord(ch)+1)\n if ch=='r':\n break",
+ "input": [
+ "\n",
+ "ch='a'\n",
+ "\n",
+ "while True:\n",
+ " print \"\\n\",ch,\n",
+ " print bin(ord(ch)) #Display the bit pattern for each character\n",
+ " ch=chr(ord(ch)+1)\n",
+ " if ch=='r':\n",
+ " break"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "\na 0b1100001\n\nb 0b1100010\n\nc 0b1100011\n\nd 0b1100100\n\ne 0b1100101\n\nf 0b1100110\n\ng 0b1100111\n\nh 0b1101000\n\ni 0b1101001\n\nj 0b1101010\n\nk 0b1101011\n\nl 0b1101100\n\nm 0b1101101\n\nn 0b1101110\n\no 0b1101111\n\np 0b1110000\n\nq 0b1110001\n"
+ "text": [
+ "\n",
+ "a 0b1100001\n",
+ "\n",
+ "b 0b1100010\n",
+ "\n",
+ "c 0b1100011\n",
+ "\n",
+ "d 0b1100100\n",
+ "\n",
+ "e 0b1100101\n",
+ "\n",
+ "f 0b1100110\n",
+ "\n",
+ "g 0b1100111\n",
+ "\n",
+ "h 0b1101000\n",
+ "\n",
+ "i 0b1101001\n",
+ "\n",
+ "j 0b1101010\n",
+ "\n",
+ "k 0b1101011\n",
+ "\n",
+ "l 0b1101100\n",
+ "\n",
+ "m 0b1101101\n",
+ "\n",
+ "n 0b1101110\n",
+ "\n",
+ "o 0b1101111\n",
+ "\n",
+ "p 0b1110000\n",
+ "\n",
+ "q 0b1110001\n"
+ ]
}
],
"prompt_number": 18
@@ -175,19 +500,48 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 10.9, Page Number: 242"
+ "source": [
+ "Example 10.9, Page Number: 242"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementation of an example of union in python'''\n\n#Variable declaration\nch=['X','Y']\nc=\"\"\ndef disp_bits(u):\n t=128\n global c\n while not(t==0):\n if u&t:\n c=c+\"1\"\n else:\n c=c+\"0\"\n t=t/2 \n return c\n\n#Result\nprint \"union as chars: \",ch[0],ch[1]\nprint \"union as integer: \",\nc= disp_bits(ord(ch[1]))\nc= disp_bits(ord(ch[0]))\nprint int(str(c),2)\n\n\n",
+ "input": [
+ "\n",
+ "#Variable declaration\n",
+ "ch=['X','Y']\n",
+ "c=\"\"\n",
+ "def disp_bits(u):\n",
+ " t=128\n",
+ " global c\n",
+ " while not(t==0):\n",
+ " if u&t:\n",
+ " c=c+\"1\"\n",
+ " else:\n",
+ " c=c+\"0\"\n",
+ " t=t/2 \n",
+ " return c\n",
+ "\n",
+ "#Result\n",
+ "print \"union as chars: \",ch[0],ch[1]\n",
+ "print \"union as integer: \",\n",
+ "c= disp_bits(ord(ch[1]))\n",
+ "c= disp_bits(ord(ch[0]))\n",
+ "print int(str(c),2)\n",
+ "\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "union as chars: X Y\nunion as integer: 22872\n"
+ "text": [
+ "union as chars: X Y\n",
+ "union as integer: 22872\n"
+ ]
}
],
"prompt_number": 19
@@ -195,7 +549,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/C++_from_the_Ground/Chapter_11(1).ipynb b/C++_from_the_Ground/Chapter_11(1).ipynb
index b59e52c7..fdd70bcf 100644
--- a/C++_from_the_Ground/Chapter_11(1).ipynb
+++ b/C++_from_the_Ground/Chapter_11(1).ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 11"
+ "name": "",
+ "signature": "sha256:5428ee61d548a6c3eefd868dac96cac83b29f85d9bfd7876e9904bdfe87b0fad"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,24 +11,78 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Chapter 11: Introducing the Class"
+ "source": [
+ "Chapter 11: Introducing the Class"
+ ]
},
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.1, Page Number: 249"
+ "source": [
+ "Example 11.1, Page Number: 249"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Illustration of a class in python'''\n\n#This creates the class queue\nclass queue:\n #Initialize the queue \n def __init__(self):\n self.__sloc=0\n self.__rloc=-1\n self.__q=[]\n \n #Put an integer into the queue\n def qput(self,i):\n if self.__sloc==100:\n print \"Queue is full.\"\n return\n self.__sloc+=1\n self.__q.append(i)\n \n #Get an integer from the queue\n def qget(self):\n if self.__rloc==self.__sloc:\n print \"Queue underflow\"\n return\n self.__rloc+=1\n return self.__q[self.__rloc] \n \n\n \n#Create two queue objects\nb=queue()\na=queue()\n\na.qput(10)\nb.qput(19)\n\na.qput(20)\nb.qput(1)\n\n#Result\nprint \"Contents of queue a: \",\nprint a.qget(),' ',a.qget()\n\nprint \"Contents of queue b: \",\nprint b.qget(),' ',b.qget()\n\n\n \n",
+ "input": [
+ "\n",
+ "\n",
+ "class queue:\n",
+ " #Initialize the queue \n",
+ " def __init__(self):\n",
+ " self.__sloc=0\n",
+ " self.__rloc=-1\n",
+ " self.__q=[]\n",
+ " \n",
+ " #Put an integer into the queue\n",
+ " def qput(self,i):\n",
+ " if self.__sloc==100:\n",
+ " print \"Queue is full.\"\n",
+ " return\n",
+ " self.__sloc+=1\n",
+ " self.__q.append(i)\n",
+ " \n",
+ " #Get an integer from the queue\n",
+ " def qget(self):\n",
+ " if self.__rloc==self.__sloc:\n",
+ " print \"Queue underflow\"\n",
+ " return\n",
+ " self.__rloc+=1\n",
+ " return self.__q[self.__rloc] \n",
+ " \n",
+ "\n",
+ " \n",
+ "#Create two queue objects\n",
+ "b=queue()\n",
+ "a=queue()\n",
+ "\n",
+ "a.qput(10)\n",
+ "b.qput(19)\n",
+ "\n",
+ "a.qput(20)\n",
+ "b.qput(1)\n",
+ "\n",
+ "#Result\n",
+ "print \"Contents of queue a: \",\n",
+ "print a.qget(),' ',a.qget()\n",
+ "\n",
+ "print \"Contents of queue b: \",\n",
+ "print b.qget(),' ',b.qget()\n",
+ "\n",
+ "\n",
+ " \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Contents of queue a: 10 20\nContents of queue b: 19 1\n"
+ "text": [
+ "Contents of queue a: 10 20\n",
+ "Contents of queue b: 19 1\n"
+ ]
}
],
"prompt_number": 3
@@ -35,19 +90,56 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.2, Page Number: 250"
+ "source": [
+ "Example 11.2, Page Number: 250"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Demonstrate class member access'''\n \nclass myclass:\n def __init__(self):\n self.__a=None #private member\n self.b=None #public member\n #public functions\n def setlab(self,i):\n self.__a=i #refer to a\n self.b=i*i #refer to b\n return\n def geta(self):\n return self.__a #refer to a\n def reset(self):\n #call setlab using self\n self.setlab(0) #the object is already known\n \n \nob=myclass()\nob.setlab(5) #set ob.a and ob.b\nprint \"ob after setlab(5): \",ob.geta(),' ',\nprint ob.b #can access b because it is public\n\nob.b=20 #can access b because it is public\nprint \"ob after ob.b=20: \",\nprint ob.geta(),' ',ob.b\n\nob.reset()\nprint \"ob after ob.reset(): \",ob.geta(),' ',ob.b\n\n",
+ "input": [
+ "\n",
+ " \n",
+ "class myclass:\n",
+ " def __init__(self):\n",
+ " self.__a=None #private member\n",
+ " self.b=None #public member\n",
+ " #public functions\n",
+ " def setlab(self,i):\n",
+ " self.__a=i #refer to a\n",
+ " self.b=i*i #refer to b\n",
+ " return\n",
+ " def geta(self):\n",
+ " return self.__a #refer to a\n",
+ " def reset(self):\n",
+ " #call setlab using self\n",
+ " self.setlab(0) #the object is already known\n",
+ " \n",
+ " \n",
+ "ob=myclass()\n",
+ "ob.setlab(5) #set ob.a and ob.b\n",
+ "print \"ob after setlab(5): \",ob.geta(),' ',\n",
+ "print ob.b #can access b because it is public\n",
+ "\n",
+ "ob.b=20 #can access b because it is public\n",
+ "print \"ob after ob.b=20: \",\n",
+ "print ob.geta(),' ',ob.b\n",
+ "\n",
+ "ob.reset()\n",
+ "print \"ob after ob.reset(): \",ob.geta(),' ',ob.b\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "ob after setlab(5): 5 25\nob after ob.b=20: 5 20\nob after ob.reset(): 0 0\n"
+ "text": [
+ "ob after setlab(5): 5 25\n",
+ "ob after ob.b=20: 5 20\n",
+ "ob after ob.reset(): 0 0\n"
+ ]
}
],
"prompt_number": 4
@@ -55,19 +147,74 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.3, Page Number: 254"
+ "source": [
+ "Example 11.3, Page Number: 254"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Demonstrate a constructor and a destructor'''\n#The destructors of python behave a little differently.\n\n#This creates the class queue\nclass queue:\n \n #Constructor\n def __init__(self): \n self.__q=[]\n self.__rloc=-1\n self.__sloc=0\n print \"Queue initialized\"\n \n #Destructor\n def __del__(self):\n print (\"Queue destroyed\")\n \n #Put an integer into the queue\n def qput(self,i):\n if self.__sloc == 100:\n print \"Queue is full\"\n return \n self.__sloc+=1\n self.__q.append(i)\n \n #Get an integer from the queue\n def qget(self):\n if self.__rloc==self.__sloc:\n print \"Queue underflow\"\n return\n self.__rloc+=1\n return self.__q[self.__rloc]\n \n#Create two queue objects\na=queue()\n\n\na.qput(10)\na.qput(20)\nb=queue()\nb.qput(19)\n\nb.qput(1)\n\n#Result\nprint a.qget(),' ',\nprint a.qget(),' ',\nprint b.qget(),' ',\nprint b.qget(),' '\n",
+ "input": [
+ "\n",
+ "class queue:\n",
+ " \n",
+ " #Constructor\n",
+ " def __init__(self): \n",
+ " self.__q=[]\n",
+ " self.__rloc=-1\n",
+ " self.__sloc=0\n",
+ " print \"Queue initialized\"\n",
+ " \n",
+ " #Destructor\n",
+ " def __del__(self):\n",
+ " print (\"Queue destroyed\")\n",
+ " \n",
+ " #Put an integer into the queue\n",
+ " def qput(self,i):\n",
+ " if self.__sloc == 100:\n",
+ " print \"Queue is full\"\n",
+ " return \n",
+ " self.__sloc+=1\n",
+ " self.__q.append(i)\n",
+ " \n",
+ " #Get an integer from the queue\n",
+ " def qget(self):\n",
+ " if self.__rloc==self.__sloc:\n",
+ " print \"Queue underflow\"\n",
+ " return\n",
+ " self.__rloc+=1\n",
+ " return self.__q[self.__rloc]\n",
+ " \n",
+ "#Create two queue objects\n",
+ "a=queue()\n",
+ "\n",
+ "\n",
+ "a.qput(10)\n",
+ "a.qput(20)\n",
+ "b=queue()\n",
+ "b.qput(19)\n",
+ "\n",
+ "b.qput(1)\n",
+ "\n",
+ "#Result\n",
+ "print a.qget(),' ',\n",
+ "print a.qget(),' ',\n",
+ "print b.qget(),' ',\n",
+ "print b.qget(),' '\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Queue initialized\nQueue destroyed\nQueue initialized\nQueue destroyed\n10 20 19 1 \n"
+ "text": [
+ "Queue initialized\n",
+ "Queue destroyed\n",
+ "Queue initialized\n",
+ "Queue destroyed\n",
+ "10 20 19 1 \n"
+ ]
}
],
"prompt_number": 2
@@ -75,19 +222,74 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.4, Page Number: 257"
+ "source": [
+ "Example 11.4, Page Number: 257"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Parameterized Constructors'''\n\n#This creates the class queue\nclass queue: \n \n #Constructor\n def __init__(self,i): \n self.__q=[]\n self.__rloc=-1\n self.__sloc=0\n self.__who=i\n print \"Queue \",self.__who,\" initialized.\"\n \n #Destructor\n def __del__():\n print \"Queue \",self.__who,\" destroyed\"\n \n #Put an integer into the queue\n def qput(self,i):\n if self.__sloc == 100:\n print \"Queue is full\"\n return \n self.__sloc+=1\n self.__q.append(i)\n \n #Get an integer from the queue\n def qget(self):\n if self.__rloc==self.__sloc:\n print \"Queue underflow\"\n return\n self.__rloc+=1\n return self.__q[self.__rloc]\n \na=queue(1)\nb=queue(2)\n\na.qput(10)\nb.qput(19)\n\na.qput(20)\nb.qput(1)\n\n#Result\nprint a.qget(),' ',\nprint a.qget(),' ',\nprint b.qget(),' ',\nprint b.qget(),' '\n",
+ "input": [
+ "\n",
+ "\n",
+ "class queue: \n",
+ " \n",
+ " #Constructor\n",
+ " def __init__(self,i): \n",
+ " self.__q=[]\n",
+ " self.__rloc=-1\n",
+ " self.__sloc=0\n",
+ " self.__who=i\n",
+ " print \"Queue \",self.__who,\" initialized.\"\n",
+ " \n",
+ " #Destructor\n",
+ " def __del__():\n",
+ " print \"Queue \",self.__who,\" destroyed\"\n",
+ " \n",
+ " #Put an integer into the queue\n",
+ " def qput(self,i):\n",
+ " if self.__sloc == 100:\n",
+ " print \"Queue is full\"\n",
+ " return \n",
+ " self.__sloc+=1\n",
+ " self.__q.append(i)\n",
+ " \n",
+ " #Get an integer from the queue\n",
+ " def qget(self):\n",
+ " if self.__rloc==self.__sloc:\n",
+ " print \"Queue underflow\"\n",
+ " return\n",
+ " self.__rloc+=1\n",
+ " return self.__q[self.__rloc]\n",
+ " \n",
+ "a=queue(1)\n",
+ "b=queue(2)\n",
+ "\n",
+ "a.qput(10)\n",
+ "b.qput(19)\n",
+ "\n",
+ "a.qput(20)\n",
+ "b.qput(1)\n",
+ "\n",
+ "#Result\n",
+ "print a.qget(),' ',\n",
+ "print a.qget(),' ',\n",
+ "print b.qget(),' ',\n",
+ "print b.qget(),' '\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Queue 1 initialized.\nQueue destroyed\nQueue 2 initialized.\nQueue destroyed\n10 20 19 1 \n"
+ "text": [
+ "Queue 1 initialized.\n",
+ "Queue destroyed\n",
+ "Queue 2 initialized.\n",
+ "Queue destroyed\n",
+ "10 20 19 1 \n"
+ ]
}
],
"prompt_number": 3
@@ -95,19 +297,44 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.5, Page Number: 258"
+ "source": [
+ "Example 11.5, Page Number: 258"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Passing more than a single arguments'''\nclass widget:\n \n #Pass two arguments to the constructor\n def __init__(self,a,b):\n self.__i=a\n self.__j=b\n \n def put_widget(self):\n print self.__i,\" \",self.__j\n\n#Initializing\nx=widget(10,20)\ny=widget(0,0)\n\nx.put_widget()\ny.put_widget()\n\n ",
+ "input": [
+ "\n",
+ "class widget:\n",
+ " \n",
+ " #Pass two arguments to the constructor\n",
+ " def __init__(self,a,b):\n",
+ " self.__i=a\n",
+ " self.__j=b\n",
+ " \n",
+ " def put_widget(self):\n",
+ " print self.__i,\" \",self.__j\n",
+ "\n",
+ "#Initializing\n",
+ "x=widget(10,20)\n",
+ "y=widget(0,0)\n",
+ "\n",
+ "x.put_widget()\n",
+ "y.put_widget()\n",
+ "\n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10 20\n0 0\n"
+ "text": [
+ "10 20\n",
+ "0 0\n"
+ ]
}
],
"prompt_number": 5
@@ -115,19 +342,40 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.6, Page Number: 259"
+ "source": [
+ "Example 11.6, Page Number: 259"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''An example of class'''\n\nclass myclass:\n \n #Constructor\n def __init__(self,x):\n self.a=x\n #To get the vale of a\n def get_a(self):\n return self.a\n\n#Initializing\nob=myclass(4)\n\n#Result\nprint ob.get_a()",
+ "input": [
+ "\n",
+ "\n",
+ "class myclass:\n",
+ " \n",
+ " #Constructor\n",
+ " def __init__(self,x):\n",
+ " self.a=x\n",
+ " #To get the vale of a\n",
+ " def get_a(self):\n",
+ " return self.a\n",
+ "\n",
+ "#Initializing\n",
+ "ob=myclass(4)\n",
+ "\n",
+ "#Result\n",
+ "print ob.get_a()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "4\n"
+ "text": [
+ "4\n"
+ ]
}
],
"prompt_number": 1
@@ -135,19 +383,44 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.7, Page Number: 260"
+ "source": [
+ "Example 11.7, Page Number: 260"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Using structures to create a class'''\n\nfrom ctypes import *\n\nclass c1(Structure):\n _fields_=[(\"__i\", c_int)] #private member\n def get_i(self): #public finctions\n return self.__i\n def put_i(self,j):\n self.__i=j\n\n#Variable declaration\ns=c1()\n\ns.put_i(10)\n\n#Result\nprint s.get_i()\n \n ",
+ "input": [
+ "\n",
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "class c1(Structure):\n",
+ " _fields_=[(\"__i\", c_int)] #private member\n",
+ " def get_i(self): #public finctions\n",
+ " return self.__i\n",
+ " def put_i(self,j):\n",
+ " self.__i=j\n",
+ "\n",
+ "#Variable declaration\n",
+ "s=c1()\n",
+ "\n",
+ "s.put_i(10)\n",
+ "\n",
+ "#Result\n",
+ "print s.get_i()\n",
+ " \n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
}
],
"prompt_number": 1
@@ -155,19 +428,42 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.8, Page Number: 261"
+ "source": [
+ "Example 11.8, Page Number: 261"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Now we use class instead'''\n\nclass c1:\n def __init__(self):\n self.__i=None #private member\n def get_i(self): #public finctions\n return self.__i\n def put_i(self,j):\n self.__i=j\n\n#Variable declaration\ns=c1()\n\ns.put_i(10)\n\n#Result\nprint s.get_i()\n \n ",
+ "input": [
+ "\n",
+ "class c1:\n",
+ " def __init__(self):\n",
+ " self.__i=None #private member\n",
+ " def get_i(self): #public finctions\n",
+ " return self.__i\n",
+ " def put_i(self,j):\n",
+ " self.__i=j\n",
+ "\n",
+ "#Variable declaration\n",
+ "s=c1()\n",
+ "\n",
+ "s.put_i(10)\n",
+ "\n",
+ "#Result\n",
+ "print s.get_i()\n",
+ " \n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
}
],
"prompt_number": 2
@@ -175,19 +471,47 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.9, Page Number: 263 "
+ "source": [
+ "Example 11.9, Page Number: 263 "
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Create a union-based class'''\n\nfrom ctypes import *\n\n#Creates a union\nclass u_type(Union):\n _fields_ = [(\"i\",c_short),\n (\"ch\", c_char*2)]\n #Constructor\n def __init__(self,a):\n self.i=a\n \n #Show the characters that comprise a short int.\n def showchars(self):\n print self.ch[0]\n print self.ch[1]\n \n \nu=u_type(1000)\n\n#Displays char of 1000\nu.showchars()",
+ "input": [
+ "\n",
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "#Creates a union\n",
+ "class u_type(Union):\n",
+ " _fields_ = [(\"i\",c_short),\n",
+ " (\"ch\", c_char*2)]\n",
+ " #Constructor\n",
+ " def __init__(self,a):\n",
+ " self.i=a\n",
+ " \n",
+ " #Show the characters that comprise a short int.\n",
+ " def showchars(self):\n",
+ " print self.ch[0]\n",
+ " print self.ch[1]\n",
+ " \n",
+ " \n",
+ "u=u_type(1000)\n",
+ "\n",
+ "#Displays char of 1000\n",
+ "u.showchars()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "\ufffd\n\u0003\n"
+ "text": [
+ "\ufffd\n",
+ "\u0003\n"
+ ]
}
],
"prompt_number": 2
@@ -195,19 +519,40 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.10, Page Number: 264 "
+ "source": [
+ "Example 11.10, Page Number: 264 "
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''A simple class program'''\n#There is no inline function in python\n\nclass c1:\n def __init__(self):\n self.__i=None\n def get_i(self):\n return self.i\n def put_i(self,j):\n self.i=j\n\n#Variable declaration\ns=c1()\n\ns.put_i(10)\n\n#Result\nprint s.get_i()",
+ "input": [
+ "\n",
+ "class c1:\n",
+ " def __init__(self):\n",
+ " self.__i=None\n",
+ " def get_i(self):\n",
+ " return self.i\n",
+ " def put_i(self,j):\n",
+ " self.i=j\n",
+ "\n",
+ "#Variable declaration\n",
+ "s=c1()\n",
+ "\n",
+ "s.put_i(10)\n",
+ "\n",
+ "#Result\n",
+ "print s.get_i()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
}
],
"prompt_number": 2
@@ -215,19 +560,41 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.11, Page Number: 265 "
+ "source": [
+ "Example 11.11, Page Number: 265 "
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''A simple class program'''\n#There is no inline functions in python\n\nclass c1:\n def __init__(self):\n self.__i=None\n def get_i(self):\n return self.i\n def put_i(self,j):\n self.i=j\n\n#Variable declaration\ns=c1()\n\ns.put_i(10)\n\n#Result\nprint s.get_i()",
+ "input": [
+ "\n",
+ "\n",
+ "class c1:\n",
+ " def __init__(self):\n",
+ " self.__i=None\n",
+ " def get_i(self):\n",
+ " return self.i\n",
+ " def put_i(self,j):\n",
+ " self.i=j\n",
+ "\n",
+ "#Variable declaration\n",
+ "s=c1()\n",
+ "\n",
+ "s.put_i(10)\n",
+ "\n",
+ "#Result\n",
+ "print s.get_i()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
}
],
"prompt_number": 3
@@ -235,19 +602,70 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.12, Page Number: 267 "
+ "source": [
+ "Example 11.12, Page Number: 267 "
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''An example of arrays of objects'''\n\nclass display:\n def __init__(self):\n width=None\n height=None\n res=None\n def set_dim(self,w,h):\n self.width=w\n self.height=h\n def get_dim(self):\n return self.width,self.height\n def set_res(self,r):\n self.res=r\n def get_res(self):\n return self.res\n \n#Variable decleration\nnames=[\"low\",\"medium\",\"high\"] \n(low,medium,high)=(0,1,2) #For enumeration type\nw=None\nh=None\ndisplay_mode=[]*3\n\nfor i in range(3):\n display_mode.append(display())\n\n#Initialize the array of objects using member functions\ndisplay_mode[0].set_res(low)\ndisplay_mode[0].set_dim(640,480)\n\ndisplay_mode[1].set_res(medium)\ndisplay_mode[1].set_dim(800,600)\n\ndisplay_mode[2].set_res(high)\ndisplay_mode[2].set_dim(1600,1200)\n\n#Result\nprint \"Available display modes: \"\nfor i in range(3):\n print names[display_mode[i].get_res()],\" : \",\n w,h=display_mode[i].get_dim()\n print w,\" by \",h",
+ "input": [
+ "\n",
+ "\n",
+ "class display:\n",
+ " def __init__(self):\n",
+ " width=None\n",
+ " height=None\n",
+ " res=None\n",
+ " def set_dim(self,w,h):\n",
+ " self.width=w\n",
+ " self.height=h\n",
+ " def get_dim(self):\n",
+ " return self.width,self.height\n",
+ " def set_res(self,r):\n",
+ " self.res=r\n",
+ " def get_res(self):\n",
+ " return self.res\n",
+ " \n",
+ "#Variable decleration\n",
+ "names=[\"low\",\"medium\",\"high\"] \n",
+ "(low,medium,high)=(0,1,2) #For enumeration type\n",
+ "w=None\n",
+ "h=None\n",
+ "display_mode=[]*3\n",
+ "\n",
+ "for i in range(3):\n",
+ " display_mode.append(display())\n",
+ "\n",
+ "#Initialize the array of objects using member functions\n",
+ "display_mode[0].set_res(low)\n",
+ "display_mode[0].set_dim(640,480)\n",
+ "\n",
+ "display_mode[1].set_res(medium)\n",
+ "display_mode[1].set_dim(800,600)\n",
+ "\n",
+ "display_mode[2].set_res(high)\n",
+ "display_mode[2].set_dim(1600,1200)\n",
+ "\n",
+ "#Result\n",
+ "print \"Available display modes: \"\n",
+ "for i in range(3):\n",
+ " print names[display_mode[i].get_res()],\" : \",\n",
+ " w,h=display_mode[i].get_dim()\n",
+ " print w,\" by \",h"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Available display modes: \nlow : 640 by 480\nmedium : 800 by 600\nhigh : 1600 by 1200\n"
+ "text": [
+ "Available display modes: \n",
+ "low : 640 by 480\n",
+ "medium : 800 by 600\n",
+ "high : 1600 by 1200\n"
+ ]
}
],
"prompt_number": 3
@@ -255,19 +673,39 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.13, Page Number: 268"
+ "source": [
+ "Example 11.13, Page Number: 268"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Initialize an array of objects'''\n\nclass samp:\n __a=None\n def __init__(self,n):\n self.__a=n\n def get_a(self):\n return self.__a\n\n#Initializing the list\nsampArray=[samp(-1),samp(-2),samp(-3),samp(-4)]\n\n#Display\nfor i in range(4):\n print sampArray[i].get_a(),' ',",
+ "input": [
+ "\n",
+ "\n",
+ "class samp:\n",
+ " __a=None\n",
+ " def __init__(self,n):\n",
+ " self.__a=n\n",
+ " def get_a(self):\n",
+ " return self.__a\n",
+ "\n",
+ "#Initializing the list\n",
+ "sampArray=[samp(-1),samp(-2),samp(-3),samp(-4)]\n",
+ "\n",
+ "#Display\n",
+ "for i in range(4):\n",
+ " print sampArray[i].get_a(),' ',"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "-1 -2 -3 -4 \n"
+ "text": [
+ "-1 -2 -3 -4 \n"
+ ]
}
],
"prompt_number": 4
@@ -275,19 +713,56 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.14, Page Number: 269"
+ "source": [
+ "Example 11.14, Page Number: 269"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Two dimensional array if objects'''\n\nclass samp:\n __a=None\n __b=None\n def __init__(self,n,m):\n self.__a=n\n self.__b=m\n def get_a(self):\n return self.__a\n def get_b(self):\n return self.__b\n \n#Initializing the list\nsampArray=[[samp(1,2),samp(3,4)],\n [samp(5,6),samp(7,8)],\n [samp(9,10),samp(11,12)],\n [samp(13,14),samp(15,16)]]\n\n#Display\nfor i in range(4):\n print sampArray[i][0].get_a(),' ',\n print sampArray[i][0].get_b()\n print sampArray[i][1].get_a(),' ',\n print sampArray[i][1].get_b()",
+ "input": [
+ "\n",
+ "\n",
+ "class samp:\n",
+ " __a=None\n",
+ " __b=None\n",
+ " def __init__(self,n,m):\n",
+ " self.__a=n\n",
+ " self.__b=m\n",
+ " def get_a(self):\n",
+ " return self.__a\n",
+ " def get_b(self):\n",
+ " return self.__b\n",
+ " \n",
+ "#Initializing the list\n",
+ "sampArray=[[samp(1,2),samp(3,4)],\n",
+ " [samp(5,6),samp(7,8)],\n",
+ " [samp(9,10),samp(11,12)],\n",
+ " [samp(13,14),samp(15,16)]]\n",
+ "\n",
+ "#Display\n",
+ "for i in range(4):\n",
+ " print sampArray[i][0].get_a(),' ',\n",
+ " print sampArray[i][0].get_b()\n",
+ " print sampArray[i][1].get_a(),' ',\n",
+ " print sampArray[i][1].get_b()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "1 2\n3 4\n5 6\n7 8\n9 10\n11 12\n13 14\n15 16\n"
+ "text": [
+ "1 2\n",
+ "3 4\n",
+ "5 6\n",
+ "7 8\n",
+ "9 10\n",
+ "11 12\n",
+ "13 14\n",
+ "15 16\n"
+ ]
}
],
"prompt_number": 5
@@ -295,19 +770,46 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.15, Page Number: 270"
+ "source": [
+ "Example 11.15, Page Number: 270"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''A simple example using an object pointer'''\n\nfrom ctypes import *\n\nclass P_example(Structure):\n __num=None\n def set_num(self,val):\n self.__num=val\n def show_num(self):\n print self.__num\n\n#Variable declaration\nob=P_example() #Declare an object to the structure\np=POINTER(P_example) #Declare a pointer to the structure\n\nob.set_num(1) #access ob directly\nob.show_num()\n\np=ob #assign p the address of ob\np.show_num() #access ob using pointer\n \n ",
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "class P_example(Structure):\n",
+ " __num=None\n",
+ " def set_num(self,val):\n",
+ " self.__num=val\n",
+ " def show_num(self):\n",
+ " print self.__num\n",
+ "\n",
+ "#Variable declaration\n",
+ "ob=P_example() #Declare an object to the structure\n",
+ "p=POINTER(P_example) #Declare a pointer to the structure\n",
+ "\n",
+ "ob.set_num(1) #access ob directly\n",
+ "ob.show_num()\n",
+ "\n",
+ "p=ob #assign p the address of ob\n",
+ "p.show_num() #access ob using pointer\n",
+ " \n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "1\n1\n"
+ "text": [
+ "1\n",
+ "1\n"
+ ]
}
],
"prompt_number": 6
@@ -315,19 +817,47 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 11.16, Page Number: 271"
+ "source": [
+ "Example 11.16, Page Number: 271"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Pointer to array of objects'''\n\nclass P_example(Structure):\n __num=None\n def set_num(self,val):\n self.__num=val\n def show_num(self):\n print self.__num\n \n#Variable declaration\nob=[P_example(),P_example()] #Declare an object to the structure\np=POINTER(P_example) #Declare a pointer to the structure\n\nob[0].set_num(10) #access objects directly\nob[1].set_num(20)\n\np=ob #obtain pointer to first element\np[0].show_num() #access ob using pointer\n\np[1].show_num()\n\np[0].show_num()\n",
+ "input": [
+ "\n",
+ "class P_example(Structure):\n",
+ " __num=None\n",
+ " def set_num(self,val):\n",
+ " self.__num=val\n",
+ " def show_num(self):\n",
+ " print self.__num\n",
+ " \n",
+ "#Variable declaration\n",
+ "ob=[P_example(),P_example()] #Declare an object to the structure\n",
+ "p=POINTER(P_example) #Declare a pointer to the structure\n",
+ "\n",
+ "ob[0].set_num(10) #access objects directly\n",
+ "ob[1].set_num(20)\n",
+ "\n",
+ "p=ob #obtain pointer to first element\n",
+ "p[0].show_num() #access ob using pointer\n",
+ "\n",
+ "p[1].show_num()\n",
+ "\n",
+ "p[0].show_num()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10\n20\n10\n"
+ "text": [
+ "10\n",
+ "20\n",
+ "10\n"
+ ]
}
],
"prompt_number": 7
@@ -335,7 +865,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
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": "Chapter 12: A Closer Look at Classes"
+ "source": [
+ "Chapter 12: A Closer Look at Classes"
+ ]
},
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 12.1, Page Number: 274"
+ "source": [
+ "Example 12.1, Page Number: 274"
+ ]
},
{
"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": "Example 12.2, Page Number: 275"
+ "source": [
+ "Example 12.2, Page Number: 275"
+ ]
},
{
"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": "Example 12.3, Page Number: 277"
+ "source": [
+ "Example 12.3, Page Number: 277"
+ ]
},
{
"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": "Example 12.4, Page Number: 278"
+ "source": [
+ "Example 12.4, Page Number: 278"
+ ]
},
{
"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)Example 12.5, Page Number: 280"
+ "source": [
+ "Example 12.5, Page Number: 280"
+ ]
},
{
"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)Example 12.6, Page Number: 282"
+ "source": [
+ "Example 12.6, Page Number: 282"
+ ]
},
{
"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": "Example 12.7, Page Number: 283"
+ "source": [
+ "Example 12.7, Page Number: 283"
+ ]
},
{
"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": "Example 12.8, Page Number: 284 "
+ "source": [
+ "Example 12.8, Page Number: 284 "
+ ]
},
{
"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": "Example 12.9, Page Number: 286"
+ "source": [
+ "Example 12.9, Page Number: 286"
+ ]
},
{
"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": "Example 12.10, Page Number: 287"
+ "source": [
+ "Example 12.10, Page Number: 287"
+ ]
},
{
"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": "Example 12.11, Page Number: 288"
+ "source": [
+ "Example 12.11, Page Number: 288"
+ ]
},
{
"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": "Example 12.12, Page Number: 289"
+ "source": [
+ "Example 12.12, Page Number: 289"
+ ]
},
{
"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": "Example 12.13, Page Number: 292"
+ "source": [
+ "Example 12.13, Page Number: 292"
+ ]
},
{
"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": "Example 12.14, Page Number: 294"
+ "source": [
+ "Example 12.14, Page Number: 294"
+ ]
},
{
"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": "Example 12.15, Page Number: 295"
+ "source": [
+ "Example 12.15, Page Number: 295"
+ ]
},
{
"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": "Example 12.16, Page Number: 297"
+ "source": [
+ "Example 12.16, Page Number: 297"
+ ]
},
{
"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": []
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": "Chapter 13: Operator Overloading"
+ "source": [
+ "Chapter 13: Operator Overloading"
+ ]
},
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 13.1, Page Number: 300"
+ "source": [
+ "Example 13.1, Page Number: 300"
+ ]
},
{
"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": "Example 13.2, Page Number: 303"
+ "source": [
+ "Example 13.2, Page Number: 303"
+ ]
},
{
"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": "Example 13.3, Page Number: 306"
+ "source": [
+ "Example 13.3, Page Number: 306"
+ ]
},
{
"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": "Example 13.4, Page Number: 310"
+ "source": [
+ "Example 13.4, Page Number: 310"
+ ]
},
{
"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": "Example 13.5, Page Number: 311"
+ "source": [
+ "Example 13.5, Page Number: 311"
+ ]
},
{
"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": "Example 13.6, Page Number: 314"
+ "source": [
+ "Example 13.6, Page Number: 314"
+ ]
},
{
"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": "Example 13.7, Page Number: 318"
+ "source": [
+ "Example 13.7, Page Number: 318"
+ ]
},
{
"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": "Example 13.8, Page Number: 321"
+ "source": [
+ "Example 13.8, Page Number: 321"
+ ]
},
{
"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": "Example 13.9, Page Number: 322"
+ "source": [
+ "Example 13.9, Page Number: 322"
+ ]
},
{
"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": "Example 13.10, Page Number: 323"
+ "source": [
+ "Example 13.10, Page Number: 323"
+ ]
},
{
"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": "Example 13.11, Page Number: 324"
+ "source": [
+ "Example 13.11, Page Number: 324"
+ ]
},
{
"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": "Example 13.12, Page Number: 326"
+ "source": [
+ "Example 13.12, Page Number: 326"
+ ]
},
{
"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": []
diff --git a/C++_from_the_Ground/Chapter_14(1).ipynb b/C++_from_the_Ground/Chapter_14(1).ipynb
index 0a3df4c8..2f8447ba 100644
--- a/C++_from_the_Ground/Chapter_14(1).ipynb
+++ b/C++_from_the_Ground/Chapter_14(1).ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 14"
+ "name": "",
+ "signature": "sha256:d3cc78f10810f320519cd1c8becefb4790578385b5c2b528dc88273651f18c12"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,24 +11,112 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Chapter 14: Inheritance"
+ "source": [
+ "Chapter 14: Inheritance"
+ ]
},
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 14.1, Page Number: 333"
+ "source": [
+ "Example 14.1, Page Number: 333"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Demonstrate inheritance'''\n\n#Define base class for vehicles\nclass road_vehicle:\n def __init__(self):\n self.__wheels=None\n self.__passengers=None\n def set_wheels(self,num):\n self.__wheels=num\n def get_wheels(self):\n return self.__wheels\n def set_pass(self,num):\n self.__passengers=num\n def get_pass(self):\n return self.__passengers\n\n#Define a truck\nclass truck(road_vehicle):\n def __init__(self):\n self.__cargo=None\n def set_cargo(self,size):\n self.__cargo=size\n def get_cargo(self):\n return self.__cargo\n def show(self):\n print \"wheels: \",self.get_wheels()\n print \"passengers: \",self.get_pass()\n print \"cargo capacity in cubic feet: \",self.__cargo\n \n#Define an enum type\n(car,van,wagon)=(1,2,3)\ntype=[\"car\",\"van\",\"wagon\"]\n \n#Define an automobile\nclass automobile(road_vehicle):\n def __init__(self):\n self.car_type=None\n def set_type(self,t):\n self.car_type=t\n def get_type(self):\n return self.car_type\n def show(self):\n print \"wheels: \",self.get_wheels()\n print \"passengers: \",self.get_pass()\n print \"type: \",\n if self.get_type()==1:\n print \"car\"\n elif self.get_type()==2:\n print \"van\"\n elif self.get_type()==3:\n print \"wagon\"\n \n#Variable declaration\nt1=truck()\nt2=truck()\nc=automobile()\n\nt1.set_wheels(18)\nt1.set_pass(2)\nt1.set_cargo(3200)\n\nt2.set_wheels(6)\nt2.set_pass(3)\nt2.set_cargo(1200)\n\nt1.show()\nt2.show()\n\nc.set_wheels(4)\nc.set_pass(6)\nc.set_type(van)\n\nc.show() \n ",
+ "input": [
+ "\n",
+ "\n",
+ "class road_vehicle:\n",
+ " def __init__(self):\n",
+ " self.__wheels=None\n",
+ " self.__passengers=None\n",
+ " def set_wheels(self,num):\n",
+ " self.__wheels=num\n",
+ " def get_wheels(self):\n",
+ " return self.__wheels\n",
+ " def set_pass(self,num):\n",
+ " self.__passengers=num\n",
+ " def get_pass(self):\n",
+ " return self.__passengers\n",
+ "\n",
+ "#Define a truck\n",
+ "class truck(road_vehicle):\n",
+ " def __init__(self):\n",
+ " self.__cargo=None\n",
+ " def set_cargo(self,size):\n",
+ " self.__cargo=size\n",
+ " def get_cargo(self):\n",
+ " return self.__cargo\n",
+ " def show(self):\n",
+ " print \"wheels: \",self.get_wheels()\n",
+ " print \"passengers: \",self.get_pass()\n",
+ " print \"cargo capacity in cubic feet: \",self.__cargo\n",
+ " \n",
+ "#Define an enum type\n",
+ "(car,van,wagon)=(1,2,3)\n",
+ "type=[\"car\",\"van\",\"wagon\"]\n",
+ " \n",
+ "#Define an automobile\n",
+ "class automobile(road_vehicle):\n",
+ " def __init__(self):\n",
+ " self.car_type=None\n",
+ " def set_type(self,t):\n",
+ " self.car_type=t\n",
+ " def get_type(self):\n",
+ " return self.car_type\n",
+ " def show(self):\n",
+ " print \"wheels: \",self.get_wheels()\n",
+ " print \"passengers: \",self.get_pass()\n",
+ " print \"type: \",\n",
+ " if self.get_type()==1:\n",
+ " print \"car\"\n",
+ " elif self.get_type()==2:\n",
+ " print \"van\"\n",
+ " elif self.get_type()==3:\n",
+ " print \"wagon\"\n",
+ " \n",
+ "#Variable declaration\n",
+ "t1=truck()\n",
+ "t2=truck()\n",
+ "c=automobile()\n",
+ "\n",
+ "t1.set_wheels(18)\n",
+ "t1.set_pass(2)\n",
+ "t1.set_cargo(3200)\n",
+ "\n",
+ "t2.set_wheels(6)\n",
+ "t2.set_pass(3)\n",
+ "t2.set_cargo(1200)\n",
+ "\n",
+ "t1.show()\n",
+ "t2.show()\n",
+ "\n",
+ "c.set_wheels(4)\n",
+ "c.set_pass(6)\n",
+ "c.set_type(van)\n",
+ "\n",
+ "c.show() \n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "wheels: 18\npassengers: 2\ncargo capacity in cubic feet: 3200\nwheels: 6\npassengers: 3\ncargo capacity in cubic feet: 1200\nwheels: 4\npassengers: 6\ntype: van\n"
+ "text": [
+ "wheels: 18\n",
+ "passengers: 2\n",
+ "cargo capacity in cubic feet: 3200\n",
+ "wheels: 6\n",
+ "passengers: 3\n",
+ "cargo capacity in cubic feet: 1200\n",
+ "wheels: 4\n",
+ "passengers: 6\n",
+ "type: van\n"
+ ]
}
],
"prompt_number": 1
@@ -35,19 +124,50 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 14.2, Page Number: 335"
+ "source": [
+ "Example 14.2, Page Number: 335"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Base class access control'''\n\nclass base:\n def __init__(self):\n self.__i=self.__j=None\n def set(self,a,b):\n self.__i=a\n self.__j=b\n def show(self):\n print self.__i,self.__j\n \nclass derived(base):\n def __init__(self,x):\n self.__k=x\n def showk(self):\n print self.__k\n \n#Variable declaration\nob = derived(3)\n\nob.set(1,2) #access member of base\nob.show() #access member of base\n\nob.showk() #uses member of derived class\n ",
+ "input": [
+ "\n",
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " self.__i=self.__j=None\n",
+ " def set(self,a,b):\n",
+ " self.__i=a\n",
+ " self.__j=b\n",
+ " def show(self):\n",
+ " print self.__i,self.__j\n",
+ " \n",
+ "class derived(base):\n",
+ " def __init__(self,x):\n",
+ " self.__k=x\n",
+ " def showk(self):\n",
+ " print self.__k\n",
+ " \n",
+ "#Variable declaration\n",
+ "ob = derived(3)\n",
+ "\n",
+ "ob.set(1,2) #access member of base\n",
+ "ob.show() #access member of base\n",
+ "\n",
+ "ob.showk() #uses member of derived class\n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "1 2\n3\n"
+ "text": [
+ "1 2\n",
+ "3\n"
+ ]
}
],
"prompt_number": 1
@@ -55,19 +175,52 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 14.3, Page Number: 337"
+ "source": [
+ "Example 14.3, Page Number: 337"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Implementing protected members'''\n\nclass base:\n def __init__(self):\n self.__i=self.__j=None #These act as protected members\n def set(self,a,b):\n self.__i=a\n self.__j=b\n def show(self):\n print self.__i,self.__j\n \nclass derived(base):\n def __init__(self):\n self.__k=None\n def setk(self):\n self.__k=self._base__i*self._base__j #accessing private variables in derived class\n def showk(self):\n print self.__k\n \n#Variable declaration\nob = derived()\n\nob.set(2,3) #OK, known to be derived\nob.show() #OK, known to be derived\n\nob.setk()\nob.showk() #uses member of derived class\n ",
+ "input": [
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " self.__i=self.__j=None #These act as protected members\n",
+ " def set(self,a,b):\n",
+ " self.__i=a\n",
+ " self.__j=b\n",
+ " def show(self):\n",
+ " print self.__i,self.__j\n",
+ " \n",
+ "class derived(base):\n",
+ " def __init__(self):\n",
+ " self.__k=None\n",
+ " def setk(self):\n",
+ " self.__k=self._base__i*self._base__j #accessing private variables in derived class\n",
+ " def showk(self):\n",
+ " print self.__k\n",
+ " \n",
+ "#Variable declaration\n",
+ "ob = derived()\n",
+ "\n",
+ "ob.set(2,3) #OK, known to be derived\n",
+ "ob.show() #OK, known to be derived\n",
+ "\n",
+ "ob.setk()\n",
+ "ob.showk() #uses member of derived class\n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "2 3\n6\n"
+ "text": [
+ "2 3\n",
+ "6\n"
+ ]
}
],
"prompt_number": 10
@@ -75,19 +228,73 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 14.4, Page Number: 338"
+ "source": [
+ "Example 14.4, Page Number: 338"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Hierarchical Inheritance in python'''\n\nclass base:\n def __init__(self):\n self.__i=None\n self.__j=None\n def set(self,a,b):\n self.__i=a\n self.__j=b\n def show(self):\n print self.__i,self.__j\n \nclass derived1(base):\n def __init__(self):\n self.__k=None\n def setk(self):\n self.__k=self._base__i*self._base__j\n def showk(self):\n print self.__k\n\nclass derived2(derived1):\n def __init__(self):\n self.__m=None\n def setm(self):\n self.__m=self._base__i-self._base__j\n def showm(self):\n print self.__m\n \n \n#Variable declaration\nob1 = derived1()\nob2 = derived2()\n\nob1.set(2,3) #access member of base\nob1.show() #access member of base\nob1.setk() #uses member of derived1 class\nob1.showk() #uses member of derived1 class\n\nob2.set(3,4) #access member of base\nob2.show() #access member of base\nob2.setk() #access member of derived1 class\nob2.setm() #access member of derived2 class\nob2.showk() #uses member of derived1 class\nob2.showm() #uses member of derived1 class\n ",
+ "input": [
+ "\n",
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " self.__i=None\n",
+ " self.__j=None\n",
+ " def set(self,a,b):\n",
+ " self.__i=a\n",
+ " self.__j=b\n",
+ " def show(self):\n",
+ " print self.__i,self.__j\n",
+ " \n",
+ "class derived1(base):\n",
+ " def __init__(self):\n",
+ " self.__k=None\n",
+ " def setk(self):\n",
+ " self.__k=self._base__i*self._base__j\n",
+ " def showk(self):\n",
+ " print self.__k\n",
+ "\n",
+ "class derived2(derived1):\n",
+ " def __init__(self):\n",
+ " self.__m=None\n",
+ " def setm(self):\n",
+ " self.__m=self._base__i-self._base__j\n",
+ " def showm(self):\n",
+ " print self.__m\n",
+ " \n",
+ " \n",
+ "#Variable declaration\n",
+ "ob1 = derived1()\n",
+ "ob2 = derived2()\n",
+ "\n",
+ "ob1.set(2,3) #access member of base\n",
+ "ob1.show() #access member of base\n",
+ "ob1.setk() #uses member of derived1 class\n",
+ "ob1.showk() #uses member of derived1 class\n",
+ "\n",
+ "ob2.set(3,4) #access member of base\n",
+ "ob2.show() #access member of base\n",
+ "ob2.setk() #access member of derived1 class\n",
+ "ob2.setm() #access member of derived2 class\n",
+ "ob2.showk() #uses member of derived1 class\n",
+ "ob2.showm() #uses member of derived1 class\n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "2 3\n6\n3 4\n12\n-1\n"
+ "text": [
+ "2 3\n",
+ "6\n",
+ "3 4\n",
+ "12\n",
+ "-1\n"
+ ]
}
],
"prompt_number": 11
@@ -95,19 +302,53 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 14.5, Page Number: 341"
+ "source": [
+ "Example 14.5, Page Number: 341"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Demonstrate inheriting a protected base class'''\n\nclass base:\n def __init__(self):\n self.__i=None\n self._j=None\n self.k=None\n def seti(self,a):\n self.__i=a\n def geti(self):\n return i\n \nclass derived(base):\n def setj(self,a):\n self._j=a\n def setk(self,a):\n self.k=a\n def getj(self):\n return self._j\n def getk(self):\n return self.k\n \n#Variable declaration \nob=derived()\n\nob.setk(10)\nprint ob.getk(),\nob.setj(12)\nprint ob.getj()",
+ "input": [
+ "\n",
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " self.__i=None\n",
+ " self._j=None\n",
+ " self.k=None\n",
+ " def seti(self,a):\n",
+ " self.__i=a\n",
+ " def geti(self):\n",
+ " return i\n",
+ " \n",
+ "class derived(base):\n",
+ " def setj(self,a):\n",
+ " self._j=a\n",
+ " def setk(self,a):\n",
+ " self.k=a\n",
+ " def getj(self):\n",
+ " return self._j\n",
+ " def getk(self):\n",
+ " return self.k\n",
+ " \n",
+ "#Variable declaration \n",
+ "ob=derived()\n",
+ "\n",
+ "ob.setk(10)\n",
+ "print ob.getk(),\n",
+ "ob.setj(12)\n",
+ "print ob.getj()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10 12\n"
+ "text": [
+ "10 12\n"
+ ]
}
],
"prompt_number": 1
@@ -115,19 +356,50 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Example 14.6, Page Number: 342