From 206d0358703aa05d5d7315900fe1d054c2817ddc Mon Sep 17 00:00:00 2001
From: Jovina Dsouza
Date: Wed, 18 Jun 2014 12:43:07 +0530
Subject: adding book
---
C++_from_the_Ground/Chapter_10(1).ipynb | 207 +++++
C++_from_the_Ground/Chapter_11(1).ipynb | 347 +++++++++
C++_from_the_Ground/Chapter_12(1).ipynb | 367 +++++++++
C++_from_the_Ground/Chapter_13(1).ipynb | 267 +++++++
C++_from_the_Ground/Chapter_14(1).ipynb | 320 ++++++++
C++_from_the_Ground/Chapter_15(1).ipynb | 147 ++++
C++_from_the_Ground/Chapter_16(1).ipynb | 267 +++++++
C++_from_the_Ground/Chapter_17(1).ipynb | 307 ++++++++
C++_from_the_Ground/Chapter_18(1).ipynb | 413 ++++++++++
C++_from_the_Ground/Chapter_19(1).ipynb | 247 ++++++
C++_from_the_Ground/Chapter_2(1).ipynb | 267 +++++++
C++_from_the_Ground/Chapter_20(2).ipynb | 959 ++++++++++++++++++++++++
C++_from_the_Ground/Chapter_21(1).ipynb | 427 +++++++++++
C++_from_the_Ground/Chapter_22(1).ipynb | 207 +++++
C++_from_the_Ground/Chapter_3(1).ipynb | 207 +++++
C++_from_the_Ground/Chapter_4(1).ipynb | 399 ++++++++++
C++_from_the_Ground/Chapter_5(1).ipynb | 668 +++++++++++++++++
C++_from_the_Ground/Chapter_6(1).ipynb | 287 +++++++
C++_from_the_Ground/Chapter_7(1).ipynb | 472 ++++++++++++
C++_from_the_Ground/Chapter_8(1).ipynb | 327 ++++++++
C++_from_the_Ground/Chapter_9(1).ipynb | 387 ++++++++++
C++_from_the_Ground/README.txt | 10 +
C++_from_the_Ground/screenshots/c++preproce.png | Bin 0 -> 35527 bytes
C++_from_the_Ground/screenshots/datatypes.png | Bin 0 -> 29796 bytes
C++_from_the_Ground/screenshots/inheritence.png | Bin 0 -> 55815 bytes
25 files changed, 7506 insertions(+)
create mode 100644 C++_from_the_Ground/Chapter_10(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_11(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_12(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_13(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_14(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_15(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_16(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_17(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_18(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_19(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_2(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_20(2).ipynb
create mode 100644 C++_from_the_Ground/Chapter_21(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_22(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_3(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_4(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_5(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_6(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_7(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_8(1).ipynb
create mode 100644 C++_from_the_Ground/Chapter_9(1).ipynb
create mode 100644 C++_from_the_Ground/README.txt
create mode 100644 C++_from_the_Ground/screenshots/c++preproce.png
create mode 100644 C++_from_the_Ground/screenshots/datatypes.png
create mode 100644 C++_from_the_Ground/screenshots/inheritence.png
(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
new file mode 100644
index 00000000..6196736b
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_10(1).ipynb
@@ -0,0 +1,207 @@
+{
+ "metadata": {
+ "name": "Chapter 10"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "
Chapter 10: Structures and Unions"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1000 X\n"
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "17:06:28.236000\n"
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Sat Sep 14 17:07:14 2013\n"
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "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"
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "union as chars: X Y\nunion as integer: 22872\n"
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/C++_from_the_Ground/Chapter_11(1).ipynb b/C++_from_the_Ground/Chapter_11(1).ipynb
new file mode 100644
index 00000000..b59e52c7
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_11(1).ipynb
@@ -0,0 +1,347 @@
+{
+ "metadata": {
+ "name": "Chapter 11"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Chapter 11: Introducing the Class"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Contents of queue a: 10 20\nContents of queue b: 19 1\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Queue initialized\nQueue destroyed\nQueue initialized\nQueue destroyed\n10 20 19 1 \n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 20\n0 0\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "4\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "\ufffd\n\u0003\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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(),' ',",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "-1 -2 -3 -4 \n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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()",
+ "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"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1\n1\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n20\n10\n"
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/C++_from_the_Ground/Chapter_12(1).ipynb b/C++_from_the_Ground/Chapter_12(1).ipynb
new file mode 100644
index 00000000..abad8d81
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_12(1).ipynb
@@ -0,0 +1,367 @@
+{
+ "metadata": {
+ "name": "Chapter 12"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Chapter 12: A Closer Look at Classes"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "7\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Screen Can Be Used.\nPop-up In Use.\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Screen Can Be Used.\nPop-up In Use.\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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"
+ },
+ {
+ "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"
+ },
+ {
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 100 10\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Constructing\nDestructing\n10\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Allocating p\n10\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Freeing p\nAllocating p\n10\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Hello\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Freeing p\nFreeing p\nHello\n"
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Allocating p\n10\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Allocating p\nCopy constructor called\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Normal constructor\nNormal constructor\nCopy constructor\n"
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "100\n"
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/C++_from_the_Ground/Chapter_13(1).ipynb b/C++_from_the_Ground/Chapter_13(1).ipynb
new file mode 100644
index 00000000..c38dca6a
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_13(1).ipynb
@@ -0,0 +1,267 @@
+{
+ "metadata": {
+ "name": "Chapter 13"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Chapter 13: Operator Overloading"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "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"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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()",
+ "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"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 20 32\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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()",
+ "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"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Freeing s\nFreeing s\nHello\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "2\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "2 25\n"
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "2 25\nIndex value of 44 is out of bounds.\n"
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "ob1: 1 , 2 , 3\nob2: 11 , 13 , 15\n"
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/C++_from_the_Ground/Chapter_14(1).ipynb b/C++_from_the_Ground/Chapter_14(1).ipynb
new file mode 100644
index 00000000..0a3df4c8
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_14(1).ipynb
@@ -0,0 +1,320 @@
+{
+ "metadata": {
+ "name": "Chapter 14"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Chapter 14: Inheritance"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "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"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1 2\n3\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "2 3\n6\n"
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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 ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "2 3\n6\n3 4\n12\n-1\n"
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "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()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 12\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 14.6, Page Number: 342"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''An example of multiple base classes'''\n\nclass base1:\n def __init__(self):\n self.x=None\n def showx(self):\n print self.x\n \nclass base2:\n def __init__(self):\n self.y=None\n def showy(self):\n print self.y\n \nclass derived(base1,base2):\n def set(self,i,j):\n self.x=i\n self.y=j\n \n#Variable declaration\nob = derived()\n\nob.set(10,20) #provided by derived\nob.showx() #from base1\nob.showy() #from base2\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n20\n"
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 14.7, Page Number: 343"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Constructors, Destructors and Inheritance'''\n\nclass base:\n def __init__(self):\n print \"Constructing base\"\n def __del__(self):\n print \"Destructing base\"\n\nclass derived(base):\n def __init__(self):\n base.__init__(self)\n print \"Constructing derived\"\n def __del__(self):\n print \"Destructing derived\"\n for b in self.__class__.__bases__:\n b.__del__(self)\n\n#Variable declaration\nob=derived()\n\n#Does nothing but construct and destruct ob",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Constructing base\nConstructing derived\nDestructing derived\nDestructing base\n"
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 14.8, Page Number: 344"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "#Inheritance 344 eg:14.8\n'''Constructors, Destructors and Inheritance'''\n\nclass base:\n def __init__(self):\n print \"Constructing base\"\n def __del__(self):\n print \"Destructing base\"\n\nclass derived1(base):\n def __init__(self):\n base.__init__(self)\n print \"Constructing derived1\"\n def __del__(self):\n print \"Destructing derived1\"\n super(derived1,self).__del__(self)\n\nclass derived2(derived1):\n def __init__(self):\n derived1.__init__(self)\n print \"Constructing derived2\"\n def __del__(self):\n print \"Destructing derived2\"\n super(self.__class__,self).__del__(self)\n \n#Variable declaration\nob=derived2()\n\n#Does nothing but construct and destruct ob",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 14.9, Page Number: 345"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Constructors and Destructors in Multiple Inheritance'''\n\nclass base1:\n def __init__(self):\n print \"Constructing base1\"\n def __del__(self):\n print \"Destructing base1\"\n \nclass base2:\n def __init__(self):\n print \"Constructing base2\"\n def __del__(self):\n print \"Destructing base2\"\n \nclass derived(base1,base2):\n def __init__(self):\n for b in self.__class__.__bases__:\n b.__init__(self)\n print \"Constructing derived\"\n def __del__(self):\n print \"Destructing derived\"\n for b in self.__class__.__bases__:\n b.__del__(self)\n \n#Variable declaration\nob = derived()\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Constructing base1\nConstructing base2\nConstructing derived\nDestructing derived\nDestructing base1\nDestructing base2\n"
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 14.10, Page Number: 347"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Passing parameters to parent constructors in inheritance'''\n\nclass base:\n def __init__(self,x):\n self._i=x\n print \"Constructing base\"\n def __del__(self):\n print \"Destructing base\"\n\nclass derived(base):\n def __init__(self,x,y):\n base.__init__(self,y)\n self.__j=x\n print \"Constructing derived\"\n def __del__(self):\n print \"Destructing derived\"\n for b in self.__class__.__bases__:\n b.__del__(self)\n def show(self):\n print self._i,self.__j\n\n#Variable declaration\nob=derived(3,4)\n\n#Result\nob.show() #shows 4 3",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Constructing base\nConstructing derived\nDestructing derived\nDestructing base\n4 3\n"
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 14.11, Page Number: 348"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Example that uses multiple base classes'''\n\nclass base1:\n def __init__(self,x):\n self._i=x\n print \"Constructing base1\"\n def __del__(self):\n print \"Destructing base1\"\n \nclass base2:\n def __init__(self,x):\n self._k=x\n print \"Constructing base2\"\n def __del__(self):\n print \"Destructing base2\"\n \nclass derived(base1,base2):\n def __init__(self,x,y,z):\n self.__j=x\n i=0\n for b in self.__class__.__bases__:\n if i==0:\n b.__init__(self,y)\n else :\n b.__init__(self,z)\n i+=1\n print \"Constructing derived\"\n def __del__(self):\n print \"Destructing derived\"\n for b in self.__class__.__bases__:\n b.__del__(self)\n def show(self):\n print self._i,self.__j,self._k\n \n#Variable declaration\nob = derived(3,4,5)\n\n#Result\nob.show()\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Constructing base1\nConstructing base2\nConstructing derived\nDestructing derived\nDestructing base1\nDestructing base2\n4 3 5\n"
+ }
+ ],
+ "prompt_number": 25
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 14.12, Page Number: 348"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Another example that uses multiple base classes'''\n\nclass base1:\n def __init__(self,x):\n self._i=x\n print \"Constructing base1\"\n def __del__(self):\n print \"Destructing base1\"\n \nclass base2:\n def __init__(self,x):\n self._k=x\n print \"Constructing base2\"\n def __del__(self):\n print \"Destructing base2\"\n \nclass derived(base1,base2):\n def __init__(self,x,y):\n i=0\n for b in self.__class__.__bases__:\n if i==0:\n b.__init__(self,x)\n else :\n b.__init__(self,y)\n i+=1\n print \"Constructing derived\"\n def __del__(self):\n print \"Destructing derived\"\n for b in self.__class__.__bases__:\n b.__del__(self)\n def show(self):\n print self._i,self._k\n \n#Variable declaration\nob = derived(3,4)\n\n#Result\nob.show()\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Constructing base1\nConstructing base2\nConstructing derived\nDestructing derived\nDestructing base1\nDestructing base2\n3 4\n"
+ }
+ ],
+ "prompt_number": 26
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 14.13, Page Number: 351"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Acessing member variables'''\n\nclass base:\n def __init__(self):\n self.__i=None\n self.j=self.k=None\n def seti(self,x):\n self.__i=x\n def geti(self):\n return self.__i\n\nclass derived(base):\n def __init__(self):\n self.a=None\n\n\n#Variable declaration\nob=derived()\n\nob._base__i=10 #Accessing private members of base class\nob.j=20 #legal because j and k are public variable in base\nob.k=30\n\nob.a=40 #legal because a is public in derived class\nob.seti(10)\n\n#Result\nprint ob.geti(),ob.j,ob.a",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 20 40\n"
+ }
+ ],
+ "prompt_number": 28
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 14.14, Page Number: 354"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Virtual base class'''\n#All classes in pyhton are effectively virtual, hence scope resolution is not needed\n\nclass base:\n def __init__(self):\n self.i=None\n\n#derived1 inherits base\nclass derived1(base):\n def __init__(self):\n self.__j=None\n \n#derived2 inherits base\nclass derived2(base):\n def __init__(self):\n self.__k=None\n\n#derived3 inherits from both derived1 and derived2\nclass derived3(derived1,derived2):\n def __init__(self):\n self.__sum=None\n \n#Variable declaration\nob=derived3()\n\nob.i=10\nob.j=20\nob.k=30\n\nob.sum=ob.i+ob.j+ob.k\n\n#Result\nprint ob.i,ob.j,ob.k,ob.sum",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 20 30 60\n"
+ }
+ ],
+ "prompt_number": 29
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 14.15, Page Number: 355"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Virtual base class'''\n#All classes in pyhton are effectively virtual, hence virtual keyword is not needed\n\nclass base:\n def __init__(self):\n self.i=None\n\n#derived1 inherits base\nclass derived1(base):\n def __init__(self):\n self.__j=None\n \n#derived2 inherits base\nclass derived2(base):\n def __init__(self):\n self.__k=None\n\n#derived3 inherits from both derived1 and derived2\nclass derived3(derived1,derived2):\n def __init__(self):\n self.__sum=None\n \n#Variable declaration\nob=derived3()\n\nob.i=10\nob.j=20\nob.k=30\n\nob.sum=ob.i+ob.j+ob.k\n\n#Result\nprint ob.i,ob.j,ob.k,ob.sum",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 20 30 60\n"
+ }
+ ],
+ "prompt_number": 30
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/C++_from_the_Ground/Chapter_15(1).ipynb b/C++_from_the_Ground/Chapter_15(1).ipynb
new file mode 100644
index 00000000..53e0df9a
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_15(1).ipynb
@@ -0,0 +1,147 @@
+{
+ "metadata": {
+ "name": "Chapter 15"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Chapter 15: Virtual Functions and Polymorphism"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 15.1, Page Number: 358"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Using base pointers on derived class objects'''\n\n\nclass B_class:\n def __init__(self):\n self.author=None\n def put_author(self,s):\n self.author=s\n def show_author(self):\n print self.author\n \nclass D_class(B_class):\n def __init__(self):\n self.title=None\n def put_title(self,num):\n self.title=num\n def show_title(self):\n print \"Title:\",self.title\n \n#Variable declaration\np=[B_class()] #acts as a pointer to B_class type\nB_ob=B_class()\n\ndp=[D_class()] #acts as a pointer to D_class type\nD_ob=D_class()\n\np[0]=B_ob #assigning p to object of base\n\n\n#Access B_class via pointer\np[0].put_author(\"Tom Clancy\")\n\n#Access D_class via base pointer\np[0]=D_ob\np[0].put_author(\"William Shakespeare\")\n\n#Show that each author went into proper object\nB_ob.show_author()\nD_ob.show_author()\nprint \"\\n\"\n\n#Since put_title() and show_title() are not part of the base class, \n#they are not accessible via the base pointer p and must be accessed \n#either directly, or, as shown here, through a pointer to the \n#derived type\ndp[0]=D_ob\ndp[0].put_title(\"The Tempest\")\np[0].show_author()\ndp[0].show_title()\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Tom Clancy\nWilliam Shakespeare\n\n\nWilliam Shakespeare\nTitle: The Tempest\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 15.2, Page Number: 361"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''A short example that uses virtual functions'''\n#All functions in python are effectively virtual, hence virtual keyword not required\n\n\nclass base:\n def who(self): #virtual function\n print \"Base\"\n\nclass first_d(base):\n def who(self): #redifine who() relative to first_d\n print \"First derivation\"\n \nclass second_d(base):\n def who(self): #redifine who() relative to second_d\n print \"Second derivation\"\n \n \n#Variable declaration\nbase_obj=base()\np=[base()]\nfirst_obj=first_d()\nsecond_obj=second_d()\n\np[0]=base_obj\np[0].who() #access base's who\n\np[0]=first_obj\np[0].who() #access first_d's who\n\np[0]=second_obj\np[0].who() #access second_d's who\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Base\nFirst derivation\nSecond derivation\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 15.3, Page Number: 363"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''An example that shows virtual functions are inherited'''\n\n\nclass base:\n def who(self): #virtual function\n print \"Base\"\n\nclass first_d(base):\n def who(self): #redifine who() relative to first_d\n print \"First derivation\"\n \nclass second_d(base):\n #who not defined\n pass\n \n \n#Variable declaration\nbase_obj=base()\np=[base()]\nfirst_obj=first_d()\nsecond_obj=second_d()\n\np[0]=base_obj\np[0].who() #access base's who\n\np[0]=first_obj\np[0].who() #access first_d's who\n\np[0]=second_obj\np[0].who() #access base's who because\n #second_d does not redefine it.\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Base\nFirst derivation\nBase\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 15.4, Page Number: 364"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Virtual function in hierarchical inheritance'''\n\n\nclass base:\n def who(self): #virtual function\n print \"Base\"\n\nclass first_d(base):\n def who(self): #redifine who() relative to first_d\n print \"First derivation\"\n \n#second_d now inherited first_d -- not base\nclass second_d(first_d):\n #who not defined\n pass\n \n \n#Variable declaration\nbase_obj=base()\np=[base()]\nfirst_obj=first_d()\nsecond_obj=second_d()\n\np[0]=base_obj\np[0].who() #access base's who\n\np[0]=first_obj\np[0].who() #access first_d's who\n\np[0]=second_obj\np[0].who() #access first_d's who because\n #second_d does not redefine it.\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Base\nFirst derivation\nFirst derivation\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 15.5, Page Number: 366"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''A simple application of virtual functions'''\n\nclass figure:\n def __init__(self):\n self._x=None\n self._y=None\n def set_dim(self,i,j):\n self._x=i\n self._y=j\n def show_area(self):\n print \"No area computation defined\",\n print \"for this class.\"\n \nclass triangle(figure):\n def show_area(self):\n print \"Triangle with height\",\n print self._x,\"and base\",self._y,\n print \"has an area of\",\n print self._x*0.5*self._y,\".\"\n \nclass rectangle(figure):\n def show_area(self):\n print \"Rectangle with dimensions\",\n print self._x,\"x\",self._y,\n print \"has an area of\",\n print self._x*self._y,\".\"\n \n#Variable declaration\np=[figure()] #pointer to base type\nt=triangle() #objects of derived type\nr=rectangle()\n\np[0]=t\np[0].set_dim(10.0,5.0)\np[0].show_area()\n\np[0]=r\np[0].set_dim(10.0,5.0)\np[0].show_area()\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Triangle with height 10.0 and base 5.0 has an area of 25.0 .\nRectangle with dimensions 10.0 x 5.0 has an area of 50.0 .\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 15.6, Page Number: 368"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''An expanded version of previous program'''\n\nclass figure:\n def __init__(self):\n self._x=None\n self._y=None\n def set_dim(self,i,j=0):\n self._x=i\n self._y=j\n def show_area(self):\n print \"No area computation defined\",\n print \"for this class.\"\n \nclass triangle(figure):\n def show_area(self):\n print \"Triangle with height\",\n print self._x,\"and base\",self._y,\n print \"has an area of\",\n print self._x*0.5*self._y,\".\"\n \nclass rectangle(figure):\n def show_area(self):\n print \"Rectangle with dimensions\",\n print self._x,\"x\",self._y,\n print \"has an area of\",\n print self._x*self._y,\".\"\n \nclass circle(figure):\n def show_area(self):\n print \"Circle with radius\",\n print self._x,\n print \"has an area of\",\n print 3.14*self._x*self._x,\".\"\n \n \n#Variable declaration\np=[figure()] #pointer to base type\nt=triangle() #objects of derived type\nr=rectangle()\nc=circle()\n\np[0]=t\np[0].set_dim(10.0,5.0)\np[0].show_area()\n\np[0]=r\np[0].set_dim(10.0,5.0)\np[0].show_area()\n\np[0]=c\np[0].set_dim(9.0)\np[0].show_area()\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Triangle with height 10.0 and base 5.0 has an area of 25.0 .\nRectangle with dimensions 10.0 x 5.0 has an area of 50.0 .\nCircle with radius 9.0 has an area of 254.34 .\n"
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/C++_from_the_Ground/Chapter_16(1).ipynb b/C++_from_the_Ground/Chapter_16(1).ipynb
new file mode 100644
index 00000000..afb4b4f1
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_16(1).ipynb
@@ -0,0 +1,267 @@
+{
+ "metadata": {
+ "name": "Chapter 16"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Chapter 16: Templates"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.1, Page Number: 376"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementation of templates'''\n\n#The concept of template in in-built in python,hence this is a function template\ndef swapargs(a,b):\n temp=a[0]\n a[0]=b[0]\n b[0]=temp\n\n#Variable declaration\ni=[10]\nj=[20]\nx=[10.1]\ny=[23.3]\na=['x']\nb=['z']\n\nprint \"Original i, j: \",i[0],j[0]\nprint \"Original x,y: \",x[0],y[0]\nprint \"Original a,b: \",a[0],b[0]\n\nswapargs(i,j) #swap integers\nswapargs(x,y) #swap floats\nswapargs(a,b) #swap chars\n\n#Result\nprint \"Swapped i, j: \",i[0],j[0]\nprint \"Swapped x,y: \",x[0],y[0]\nprint \"Swapped a,b: \",a[0],b[0]",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Original i, j: 10 20\nOriginal x,y: 10.1 23.3\nOriginal a,b: x z\nSwapped i, j: 20 10\nSwapped x,y: 23.3 10.1\nSwapped a,b: z x\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.2, Page Number: 378"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Template for two generic types'''\n\ndef myfunc(x,y):\n print x,y\n \nmyfunc(10,\"hi\")\nmyfunc(0.23,10L)",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 hi\n0.23 10\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.3, Page Number: 379"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Overloading generic functions'''\n\ndef swapargs(a,b):\n if isinstance(a[0],int): #integer version\n temp=a[0]\n a[0]=b[0]\n b[0]=temp\n print \"Inside swapargs int specialization.\"\n else: #generic version\n temp=a[0]\n a[0]=b[0]\n b[0]=temp\n print \"Inside template swapargs.\"\n\n#Variable declaration\ni=[10]\nj=[20]\nx=[10.1]\ny=[23.3]\na=['x']\nb=['z']\n\nprint \"Original i, j: \",i[0],j[0]\nprint \"Original x,y: \",x[0],y[0]\nprint \"Original a,b: \",a[0],b[0]\n\nswapargs(i,j) #calls explicitly overloaded swapargs()\nswapargs(x,y) #calls generic swapargs()\nswapargs(a,b) #calls generic swapargs()\n\n#Result\nprint \"Swapped i, j: \",i[0],j[0]\nprint \"Swapped x,y: \",x[0],y[0]\nprint \"Swapped a,b: \",a[0],b[0]",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Original i, j: 10 20\nOriginal x,y: 10.1 23.3\nOriginal a,b: x z\nInside swapargs int specialization.\nInside template swapargs.\nInside template swapargs.\nSwapped i, j: 20 10\nSwapped x,y: 23.3 10.1\nSwapped a,b: z x\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.4, Page Number: 381"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Overload a function template declaration'''\n\n#Function version of f() template\ndef f(a,b=None):\n if(b==None): #First version of f()\n print \"Inside f(X a)\"\n else: #Second version of f()\n print \"Inside f(X a, Y b)\"\n \nf(10) #calls f(X)\nf(10,20) #calls f(X,Y)",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Inside f(X a)\nInside f(X a, Y b)\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.5, Page Number: 382"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementing templates in python'''\n\n#Display data specified number of times.\ndef repeat(data,times):\n while times:\n print data\n times-=1\n \nrepeat(\"This is a test\",3)\nrepeat(100,5)\nrepeat(99.0/2, 4)\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "This is a test\nThis is a test\nThis is a test\n100\n100\n100\n100\n100\n49.5\n49.5\n49.5\n49.5\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.6, Page Number: 383"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''A generic version of myabs()'''\n\ndef myabs(val):\n if val<0:\n return -val\n else:\n return val\n\n#Result\nprint myabs(-10)\nprint myabs(-10.0)\nprint myabs(-10L)\nprint myabs(-10.0)\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n10.0\n10\n10.0\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.7, Page Number: 385"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementing a generic queue class'''\n\ndef SIZE():\n return 100\nclass queue:\n def __init__(self):\n self.q=[]\n self.sloc=self.rloc=0\n #Put an object into the queue\n def qput(self,i):\n if self.sloc==SIZE():\n print \"Queue is full.\"\n return\n self.sloc+=1\n self.q.append(i)\n #Get an object from the queue.\n def qget(self):\n if self.rloc==self.sloc:\n print \"Queue Underflow.\"\n return\n a=self.rloc\n self.rloc+=1\n return self.q[a]\n \n#Create two integer queues\na=queue()\nb=queue()\na.qput(10)\nb.qput(19)\na.qput(20)\nb.qput(1)\n\nprint a.qget(),\nprint a.qget(),\nprint b.qget(),\nprint b.qget()\n\n#Create two double queues\nc=queue()\nd=queue()\nc.qput(10.12)\nd.qput(19.99)\nc.qput(-20.0)\nd.qput(0.986)\n\nprint c.qget(),\nprint c.qget(),\nprint d.qget(),\nprint d.qget()\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 20 19 1\n10.12 -20.0 19.99 0.986\n"
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.8, Page Number: 387"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Two generic types in a class'''\n\nclass myclass:\n def __init__(self,a,b):\n self.__i=a\n self.__j=b\n def show(self):\n print self.__i,self.__j\n\nob1=myclass(10,0.23)\nob2=myclass('X',\"This is a test\")\n\n#Result\nob1.show() #Show int,double\nob2.show() #Show char.char*\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 0.23\nX This is a test\n"
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.9, Page Number: 388"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''A generic safe array example'''\n\n#Defining constant\ndef SIZE():\n return 10\n\nclass atype:\n def __init__(self):\n self.a=[]\n for i in range(SIZE()):\n self.a.append(i)\n #Implementing the [] overloading\n def op1(self,i,j):\n if (i<0 or i>=SIZE()):\n print \"\\nIndex value of \",\n print i,\" is out-of-bounds.\"\n return \n self.a[i]=j\n def op2(self,i):\n if (i<0 or i>SIZE()-1):\n print \"\\nIndex value of \",\n print i,\" is out-of-bounds.\"\n return\n return self.a[i]\n \n#Variable declaration\nintob=atype()\ndoubleob=atype()\n \nprint \"Integer array: \",\nfor i in range(SIZE()):\n intob.op1(i,i)\nfor i in range(SIZE()):\n print intob.op2(i),\n \nprint \"\"\n\nprint \"Double array: \",\nfor i in range(SIZE()):\n doubleob.op1(i,float(i)/3)\nfor i in range(SIZE()):\n print doubleob.op2(i),\n \nintob.op1(12,100)",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Integer array: 0 1 2 3 4 5 6 7 8 9 \nDouble array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \nIndex value of 12 is out-of-bounds.\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.10, Page Number: 389"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementing non-type template arguments'''\n\nclass atype:\n def __init__(self,size):\n self.a=[]\n self.size=size\n for i in range(size):\n self.a.append(i)\n #Implementing the [] overloading\n def op1(self,i,j):\n if (i<0 or i>self.size-1):\n print \"\\nIndex value of \",\n print i,\" is out-of-bounds.\"\n return\n self.a[i]=j\n def op2(self,i):\n if (i<0 or i>self.size-1):\n print \"\\nIndex value of \",\n print i,\" is out-of-bounds.\"\n return\n return self.a[i]\n \n#Variable declaration\nintob=atype(10)\ndoubleob=atype(15)\n \nprint \"Integer array: \",\nfor i in range(10):\n intob.op1(i,i)\nfor i in range(10):\n print intob.op2(i),\n \nprint \"\"\n\nprint \"Double array: \",\nfor i in range(15):\n doubleob.op1(i,float(i)/3)\nfor i in range(15):\n print doubleob.op2(i),\n \nintob.op1(12,100) #generates runtime error\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Integer array: 0 1 2 3 4 5 6 7 8 9 \nDouble array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 3.33333333333 3.66666666667 4.0 4.33333333333 4.66666666667 \nIndex value of 12 is out-of-bounds.\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.11, Page Number: 391"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''A generic safe array example with default arguments'''\n\nclass atype:\n def __init__(self,size=10):\n self.a=[]\n for i in range(size):\n self.a.append(i)\n #Implementing the [] overloading\n def op1(self,i,j):\n if (i<0 and i>SIZE()-1):\n print \"Index value of \"\n print i,\" is out-of-bounds.\"\n exit(1)\n self.a[i]=j\n def op2(self,i):\n if (i<0 and i>SIZE()-1):\n print \"Index value of \"\n print i,\" is out-of-bounds.\"\n exit()\n return self.a[i]\n \n#Variable declaration\nintob=atype(100)\ndoubleob=atype()\ndefarray=atype()\n \nprint \"Integer array: \",\nfor i in range(100):\n intob.op1(i,i)\nfor i in range(100):\n print intob.op2(i),\n \nprint \"\"\n\nprint \"Double array: \",\nfor i in range(10):\n doubleob.op1(i,float(i)/3)\nfor i in range(10):\n print doubleob.op2(i),\n \nprint \"\"\n\nprint \"Defarray array: \",\nfor i in range(10):\n doubleob.op1(i,i)\nfor i in range(10):\n print doubleob.op2(i),\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Integer array: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 \nDouble array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \nDefarray array: 0 1 2 3 4 5 6 7 8 9\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 16.12, Page Number: 393"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Demonstrate class specialization'''\n\nclass myclass:\n def __init__(self,a):\n if isinstance(a,int):\n print \"Inside myclassspecialization\"\n self.__x=a*a\n else:\n print \"Inside generic myclass\"\n self.__x=a\n def getx(self):\n return self.__x\n \nd=myclass(10.1)\nprint \"double: \",d.getx(),\"\\n\"\n\ni=myclass(5)\nprint \"int: \",i.getx()\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Inside generic myclass\ndouble: 10.1 \n\nInside myclassspecialization\nint: 25\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/C++_from_the_Ground/Chapter_17(1).ipynb b/C++_from_the_Ground/Chapter_17(1).ipynb
new file mode 100644
index 00000000..8694627c
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_17(1).ipynb
@@ -0,0 +1,307 @@
+{
+ "metadata": {
+ "name": "Chapter 17"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Chapter 17: Exception Handling"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.1, Page Number: 397"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''A simple exception handling example'''\n\nprint \"start\"\n\ntry: #start a try block\n print \"Inside try block\"\n raise Exception(99) #raise an error\n print \"This will not execute\"\nexcept Exception,i: #catch an error\n print \"Caught an exception -- value is:\",\n print i\n\nprint \"end\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "start\nInside try block\nCaught an exception -- value is: 99\nend\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.2, Page Number: 399"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''A simple exception handling example'''\n\nimport sys\n\n\n\ndef main():\n print \"start\"\n try: #start a try block\n print \"Inside try block\"\n raise Exception(99) #raise an error\n print \"This will not execute\"\n except Exception,i: #catch an error\n if isinstance(i,float):\n print \"Caught an exception -- value is:\",\n print i\n else:\n print \"Abnormal program termination\"\n return\n print \"end\"\n\n \nmain()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "start\nInside try block\nAbnormal program termination\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.3, Page Number: 400"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Throwing an exception from a function called \n from within a try block'''\n\ndef Xtest(test):\n print \"Inside Xtest, test is: \",test\n if(test):\n raise Exception(test)\n \nprint \"start\"\n\ntry: #start a try block\n print \"Inside try block\"\n Xtest(0)\n Xtest(1)\n Xtest(2)\nexcept Exception,i: #catch an error\n print \"Caught an exception -- value is:\",\n print i\n\nprint \"end\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "start\nInside try block\nInside Xtest, test is: 0\nInside Xtest, test is: 1\nCaught an exception -- value is: 1\nend\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.4, Page Number: 401"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''A try/except is reset each time a function is entered'''\n\ndef Xhandler(test):\n try:\n if(test):\n raise Exception(test)\n except Exception,i:\n print \"Caught One! Ex #:\",i\n \nprint \"start\"\n\nXhandler(1)\nXhandler(2)\nXhandler(0)\nXhandler(3)\n\nprint \"end\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "start\nCaught One! Ex #: 1\nCaught One! Ex #: 2\nCaught One! Ex #: 3\nend\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.5, Page Number: 401"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Use exception class'''\n\nclass MyException:\n def __init__(self,s):\n self.str_what=s\n \n#Variable declaration\na=None \nb=None\n\ntry:\n print \"Enter numerator and denominator:\"\n #User-input\n a=10 \n b=0\n if not(b):\n raise MyException(\"Cannot divide by zero!\")\n else:\n print \"Quotient is\",a/b\nexcept MyException as e: #catch an error\n print e.str_what\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter numerator and denominator:\nCannot divide by zero!\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.6, Page Number: 403"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Different types of exceptions being caught'''\n\nclass MyException:\n def __init__(self,s):\n self.x=s\n \ndef Xhandler(test):\n try:\n if(test):\n raise MyException(test)\n else:\n raise MyException(\"Value is zero\")\n except MyException as i:\n if isinstance(i.x,int):\n print \"Caught One! Ex #:\",i.x\n else:\n print \"Caught a string:\",\n print i.x\n \nprint \"start\"\n\nXhandler(1)\nXhandler(2)\nXhandler(0)\nXhandler(3)\n\nprint \"end\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "start\nCaught One! Ex #: 1\nCaught One! Ex #: 2\nCaught a string: Value is zero\nCaught One! Ex #: 3\nend\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.7, Page Number: 404"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Catching derived classes'''\n\nclass B:\n pass\n\nclass D(B):\n pass\n\nderived=D()\n\ntry:\n raise B()\nexcept B as b:\n print \"Caught a base class.\"\nexcept D as d:\n print \"This wont execute.\"\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Caught a base class.\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.8, Page Number: 405"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Example to catch all exceptions'''\n\ndef Xhandler(test):\n try:\n if test==0:\n raise Exception(test) #throw int\n if test==1:\n raise Exception('a') #throw char\n if test==2:\n raise Exception(123.23) #throw double\n except: #Catches all exceptions\n print \"Caught One!\"\n\nprint \"start\"\n\nXhandler(0)\nXhandler(1)\nXhandler(2)\n\nprint \"end\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "start\nCaught One!\nCaught One!\nCaught One!\nend\n"
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.9, Page Number: 405"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Example to catch all exceptions'''\n\nclass MyException:\n def __init__(self,s):\n self.x=s\n \ndef Xhandler(test):\n try:\n if test==0:\n raise MyException(test) #throw int\n if test==1:\n raise MyException('a') #throw char\n if test==2:\n raise MyException(123.23) #throw double\n except MyException as i:\n if isinstance(i.x,int): #catch an int exception\n print \"Caught\",i.x \n else: #catch all other exceptions\n print \"Caught One!\"\n \n\nprint \"start\"\n\nXhandler(0)\nXhandler(1)\nXhandler(2)\n\nprint \"end\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "start\nCaught 0\nCaught One!\nCaught One!\nend\n"
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.10, Page Number: 407"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Restricting function throw types'''\n\nclass MyException:\n def __init__(self,s):\n self.x=s\n \n#This function can only throw ints, chars and doubles\ndef Xhandler(test):\n if test==0:\n raise MyException(test) #throw int\n if test==1:\n raise MyException('a') #throw char\n if test==2:\n raise MyException(123.23) #throw double\n \n\nprint \"start\"\ntry:\n Xhandler(0)\nexcept MyException as i:\n if isinstance(i.x,int): #catch an int exception\n print \"Caught int\" \n elif isinstance(i.x,str): #catch a char exception\n print \"Caught char\"\n elif isinstance(i.x,float): #catch a float exception\n print \"Caught double\"\n\nprint \"end\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "start\nCaught int\nend\n"
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.11, Page Number: 408"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Example of \"rethrowing\" an exception'''\n\ndef Xhandler():\n try:\n raise Exception(\"hello\") #throw a char *\n except Exception,c: #catch a char *\n print \"Caugh char * inside Xhandler\"\n raise #rethrow char * out of function\n \nprint \"start\"\ntry:\n Xhandler()\nexcept Exception,c:\n print \"Caught char * inside main\"\n \nprint \"end\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "start\nCaugh char * inside Xhandler\nCaught char * inside main\nend\n"
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.12, Page Number: 410"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Handle exceptions due to allocation failure'''\n\nfrom ctypes import *\n\n#Variable declaration\np=[]\n\ntry:\n for i in range(32):\n p.append(c_int())\nexcept MemoryError,m:\n print \"Allocation failure.\"\n \nfor i in range(32):\n p[i]=i\n \nfor i in range(32):\n print p[i],\n \n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31\n"
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.13, Page Number: 410"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Checking allocation failure without raising an exception'''\n\nfrom ctypes import *\n\n#Variable declaration\np=[]\n\n\nfor i in range(32):\n p.append(c_int()) \nif not(p):\n print \"Allocation failure.\"\n \nfor i in range(32):\n p[i]=i\n \nfor i in range(32):\n print p[i],\n \n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31\n"
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 17.13, Page Number: 412"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Demonstrate overloaded new and delete'''\n\nclass three_d:\n def __init__(self,i=0,j=0,k=0): #3=D coordinates\n if(i==0 and j==0 and k==0):\n self.x=self.y=self.z=0\n print \"Constructing 0, 0, 0\"\n else:\n self.x=i\n self.y=j\n self.z=k\n print \"Constructing\",i,\",\",j,\",\",k\n def __del__(self):\n print \"Destructing\"\n #new overloaded relative to three_d.\n def __new__(typ, *args, **kwargs):\n obj = object.__new__(typ, *args, **kwargs)\n return obj\n def show(self):\n print self.x,\",\",\n print self.y,\",\",\n print self.z\n \np1=[]*3\np2=[]\n \ntry:\n print \"Allocating array of three_d objects.\"\n for i in range(3): #allocate array\n p1.append(three_d())\n print \"Allocating three_d object.\"\n p2.append(three_d(5,6,7)) #allocate object\nexcept MemoryError:\n print \"Allocation error\"\n \np1[2].show()\np2[0].show()\n\n\nfor i in xrange(2,-1,-1):\n del p1[i] #delete array\nprint \"Deleting array of thee_d objects.\"\n\ndel p2[0] #delete object\nprint \"Deleting three_d object.\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Allocating array of three_d objects.\nConstructing 0, 0, 0\nConstructing 0, 0, 0\nConstructing 0, 0, 0\nAllocating three_d object.\nConstructing 5 , 6 , 7\n0 , 0 , 0\n5 , 6 , 7\nDestructing\nDestructing\nDestructing\nDeleting array of thee_d objects.\nDestructing\nDeleting three_d object.\n"
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/C++_from_the_Ground/Chapter_18(1).ipynb b/C++_from_the_Ground/Chapter_18(1).ipynb
new file mode 100644
index 00000000..3ad3c39c
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_18(1).ipynb
@@ -0,0 +1,413 @@
+{
+ "metadata": {
+ "name": "Chapter 18"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Chapter 18: The C++ I/O System"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.1, Page Number: 421"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementation of overloading << for print in python'''\n\nclass three_d:\n def __init__(self,a,b,c): #3D coordinates\n self.x=a\n self.y=b\n self.z=c\n #Display x,y,z coordinates - three_d inserter.\n def __repr__(self):\n return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n\n#Variable declaration\na=three_d(1,2,3)\nb=three_d(3,4,5)\nc=three_d(5,6,7)\n\n#Result\nprint a,b,c",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1, 2, 3\n 3, 4, 5\n 5, 6, 7\n\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.2, Page Number: 423"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Using a friend function implement overloading of << in python'''\n\nclass thrnee_d:\n def __init__(self,a,b,c): #3D coordinates\n self.x=a\n self.y=b\n self.z=c\n #Display x,y,z coordinates - three_d inserter.\n __repr__=repr \n \n#Friend function \ndef repr():\n return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n\n#Variable declaration\na=three_d(1,2,3)\nb=three_d(3,4,5)\nc=three_d(5,6,7)\n\nprint a,b,c",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1, 2, 3\n 3, 4, 5\n 5, 6, 7\n\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.3, Page Number: 424"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementation of overloading << and >> in python'''\n\nclass three_d:\n def __init__(self,a,b,c): #3D coordinates\n self.x=a\n self.y=b\n self.z=c\n #Display x,y,z coordinates - three_d inserter.\n def __repr__(self):\n return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)\n\n#Variable declaration\na=three_d(1,2,3)\n\nprint a\n\n#User input\nprint \"Enter X,Y,Z values:\"\na=three_d(4,5,6) \nprint a",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1, 2, 3\nEnter X,Y,Z values:\n4, 5, 6\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.4, Page Number: 428"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementing some ios flags in python'''\n\n\nprint '{0:+d}'.format(123), #for ios::showpos\nif(123.23>0):\n i='{0:e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:e}'.format(123.23)\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "+123 +1.232300e+02\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.5, Page Number: 430"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementing some ios flags in python'''\n\nimport string\n\nprint '{0:+d}'.format(123), #for ios::showpos\nif(123.23>0):\n i='{0:e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:e}'.format(123.23)\n\n \nprint '{:10.2f}'.format(123.23), #2 digits left of decimal\nif(123.23>0):\n i='{0:.2e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:.2e}'.format(123.23)\n \n \nprint '{:#>10}'.format(str(123)), #for ios::fill\nif(123.23>0): \n i='{0:.2e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:.2e}'.format(123.23)\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "+123 +1.232300e+02\n 123.23 +1.23e+02\n#######123 +1.23e+02\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.6, Page Number: 432"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implement io manipulators in python'''\n\nprint '{0:.0e}'.format(1000.243) #for setprecision\nprint '{:>20}'.format(\"Hello There\") #to set width and right align\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1e+03\n Hello There\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.7, Page Number: 433"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementing some ios flags in python'''\n\nprint '{0:+d}'.format(123), #for ios::showpos\nif(123.23>0):\n i='{0:e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:e}'.format(123.23)\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "+123 +1.232300e+02\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.8, Page Number: 433"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Removing leading whitespaces from a string'''\n\n#User input\ns=\" Hello\"\n\n#Result\nprint s.lstrip() #lstrip removes leading spaces",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Hello\n"
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.9, Page Number: 434"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementing some ios flags in python'''\n\ndef setup(s):\n return '{:$<10}'.format(str(s))\n\n\n#Result\nprint 10,setup(10)\n\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 10$$$$$$$$\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.10, Page Number: 435"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Prompting the user to enter number in hexadecimal form'''\n\ndef prompt():\n print \"Enter number using hex format:\"\n hex=0x46\n return hex\n\n\n#Result\ni=prompt()\nprint i\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.11, Page Number: 438"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Program to write to a file'''\n\n#Open a file, creating one if it does not already exist\nout=open(\"test\",'w')\n\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n #Write to file\n out.write(\"10 123.23\\n\")\n out.write(\"This is a short text file.\")\n #Close the file\n out.close()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.12, Page Number: 438"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Program to read to a file'''\n\n#Open a file, creating one if it does not already exist\nIn=open(\"test\",'r')\n\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\nelse:\n #Read file\n i=In.read(2)\n ch=In.read(1)\n f=In.read(6)\n str=In.read()\n print i,f,ch\n print str\n #Close the file\n out.close()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 123.23 \n\nThis is a short text file.\n"
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.13, Page Number: 439"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Program to read to a file provided on the screen'''\n#Program cannot be provided here, so error is obtained\n\nimport sys\n \nif not(len(sys.argv)==2):\n print \"Usage: PR \\n\"\nelse:\n #Open a file\n In=open(sys.argv[1],'r')\n\n #In case file cannot open\n if(not(In)):\n print \"Cannot open file.\"\n else:\n #Read file\n ch=In.read()\n print ch\n In.close()\n \n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Usage: PR \n\n"
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.14, Page Number: 440"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Program to write to a file'''\n\nimport sys\n\np=\"hello there\"\n\nout=open(\"test\",'w')\n\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n #Write to file\n for i in range(len(p)):\n out.write(p[i])\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.15, Page Number: 441"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Program to read and write in a file'''\n\nimport sys\n\nn=[1,2,3,4,5]\n\n#Open a file 'test'\nout=open(\"test\",'w')\n\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n #Write to file\n for i in range(5):\n out.write(chr(n[i]))\n out.close()\n \nfor i in range(5): #clear array\n n[i]=0\n \n#Open the file\nIn=open(\"test\",'r')\n\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\nelse:\n #Read file\n for i in range(5):\n n[i]=ord(In.read(1))\n\n#Result, shows value from file\nfor i in range(5):\n print n[i],\n \nIn.close()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1 2 3 4 5\n"
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "Example 18.16, Page Number: 442