From 84eaa5fbfb3090e912ebe12de1906b7c0bdde908 Mon Sep 17 00:00:00 2001
From: Jovina Dsouza
Date: Tue, 8 Jul 2014 17:19:20 +0530
Subject: adding book
---
C++_from_the_Ground/Chapter_10.ipynb | 560 +++++++++++
C++_from_the_Ground/Chapter_11.ipynb | 877 +++++++++++++++++
C++_from_the_Ground/Chapter_12.ipynb | 904 +++++++++++++++++
C++_from_the_Ground/Chapter_13.ipynb | 883 +++++++++++++++++
C++_from_the_Ground/Chapter_14.ipynb | 924 ++++++++++++++++++
C++_from_the_Ground/Chapter_15.ipynb | 427 ++++++++
C++_from_the_Ground/Chapter_16.ipynb | 674 +++++++++++++
C++_from_the_Ground/Chapter_17.ipynb | 730 ++++++++++++++
C++_from_the_Ground/Chapter_18.ipynb | 881 +++++++++++++++++
C++_from_the_Ground/Chapter_19.ipynb | 725 ++++++++++++++
C++_from_the_Ground/Chapter_2.ipynb | 416 ++++++++
C++_from_the_Ground/Chapter_20.ipynb | 937 ++++++++++++++++++
C++_from_the_Ground/Chapter_21.ipynb | 1187 ++++++++++++++++++++++
C++_from_the_Ground/Chapter_22.ipynb | 347 +++++++
C++_from_the_Ground/Chapter_3.ipynb | 473 +++++++++
C++_from_the_Ground/Chapter_4.ipynb | 1340 +++++++++++++++++++++++++
C++_from_the_Ground/Chapter_5.ipynb | 1191 +++++++++++++++++++++++
C++_from_the_Ground/Chapter_6.ipynb | 558 +++++++++++
C++_from_the_Ground/Chapter_7.ipynb | 908 +++++++++++++++++
C++_from_the_Ground/Chapter_8.ipynb | 1783 ++++++++++++++++++++++++++++++++++
C++_from_the_Ground/Chapter_9.ipynb | 813 ++++++++++++++++
21 files changed, 17538 insertions(+)
create mode 100644 C++_from_the_Ground/Chapter_10.ipynb
create mode 100644 C++_from_the_Ground/Chapter_11.ipynb
create mode 100644 C++_from_the_Ground/Chapter_12.ipynb
create mode 100644 C++_from_the_Ground/Chapter_13.ipynb
create mode 100644 C++_from_the_Ground/Chapter_14.ipynb
create mode 100644 C++_from_the_Ground/Chapter_15.ipynb
create mode 100644 C++_from_the_Ground/Chapter_16.ipynb
create mode 100644 C++_from_the_Ground/Chapter_17.ipynb
create mode 100644 C++_from_the_Ground/Chapter_18.ipynb
create mode 100644 C++_from_the_Ground/Chapter_19.ipynb
create mode 100644 C++_from_the_Ground/Chapter_2.ipynb
create mode 100644 C++_from_the_Ground/Chapter_20.ipynb
create mode 100644 C++_from_the_Ground/Chapter_21.ipynb
create mode 100644 C++_from_the_Ground/Chapter_22.ipynb
create mode 100644 C++_from_the_Ground/Chapter_3.ipynb
create mode 100644 C++_from_the_Ground/Chapter_4.ipynb
create mode 100644 C++_from_the_Ground/Chapter_5.ipynb
create mode 100644 C++_from_the_Ground/Chapter_6.ipynb
create mode 100644 C++_from_the_Ground/Chapter_7.ipynb
create mode 100644 C++_from_the_Ground/Chapter_8.ipynb
create mode 100644 C++_from_the_Ground/Chapter_9.ipynb
diff --git a/C++_from_the_Ground/Chapter_10.ipynb b/C++_from_the_Ground/Chapter_10.ipynb
new file mode 100644
index 00000000..b19c3a4c
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_10.ipynb
@@ -0,0 +1,560 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:73c04e2c14bfab695e4acf07dc7752334b6372d624570a4e051fd91aeeb761f7"
+ },
+ "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": [
+ "\n",
+ "class inv_type:\n",
+ " def __init__(self):\n",
+ " self.item=None\n",
+ " self.cost=0\n",
+ " self.retail=0\n",
+ " self.on_hand=0\n",
+ " self.lead_time=0\n",
+ "\n",
+ "#Variable declaration\n",
+ "size=100 \n",
+ "invtry = []*size\n",
+ "i=5 #User iput for menu selection\n",
+ "\n",
+ "#Initialize the array\n",
+ "def init_list():\n",
+ " for t in range(size):\n",
+ " invtry.append(inv_type())\n",
+ " \n",
+ "#get a menu selection\n",
+ "def menu():\n",
+ " global i\n",
+ " print \"(E)nter\"\n",
+ " print \"(D)isplay\"\n",
+ " print \"(U)pdate\"\n",
+ " print \"(Q)uit\"\n",
+ " print \"choose one: \"\n",
+ " i-=1\n",
+ " return i\n",
+ "\n",
+ "#enter items into the list\n",
+ "def enter():\n",
+ " #find the first free structure\n",
+ " for i in range(size):\n",
+ " if not(invtry[i].item==None):\n",
+ " break\n",
+ " #i will be size if list is full\n",
+ " if i==size:\n",
+ " print \"List full.\"\n",
+ " return\n",
+ " input(i)\n",
+ " \n",
+ "#Input the information\n",
+ "def input(i):\n",
+ " #Enter information; User input\n",
+ " invtry[i].item=\"Gloves\"\n",
+ " invtry[i].cost=10\n",
+ " invtry[i].retail=25\n",
+ " invtry[i].on_hand=50\n",
+ " invtry[i].lead_time=10\n",
+ " \n",
+ "#Modify an existing item\n",
+ "def update():\n",
+ " name=\"Gloves\" #User input\n",
+ " for i in range(size):\n",
+ " if not(name==invtry[i].item):\n",
+ " break\n",
+ " if i==size:\n",
+ " print \"Item not found.\"\n",
+ " return\n",
+ " print \"Enter new information.\"\n",
+ " input(i)\n",
+ " \n",
+ "#Display the list\n",
+ "def display():\n",
+ " for t in range(size):\n",
+ " if not(invtry[t].item==None):\n",
+ " print invtry[t].item\n",
+ " print \"Cost: $\",invtry[t].cost\n",
+ " print \"Retail: $\",invtry[t].retail\n",
+ " print \"On hand: \",invtry[t].on_hand\n",
+ " print \"Resupply time: \",invtry[t].lead_time,\" days\"\n",
+ " \n",
+ "\n",
+ "init_list()\n",
+ "while True:\n",
+ " choice=menu()\n",
+ " if choice==4:\n",
+ " enter()\n",
+ " elif choice==3:\n",
+ " display()\n",
+ " elif choice==2:\n",
+ " update()\n",
+ " elif choice==1:\n",
+ " break"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n",
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n",
+ "Gloves\n",
+ "Cost: $ 10\n",
+ "Retail: $ 25\n",
+ "On hand: 50\n",
+ "Resupply time: 10 days\n",
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n",
+ "Enter new information.\n",
+ "(E)nter\n",
+ "(D)isplay\n",
+ "(U)pdate\n",
+ "(Q)uit\n",
+ "choose one: \n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.2, Page Number: 226"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class sample:\n",
+ " a=None\n",
+ " ch=None\n",
+ " \n",
+ "def f1(parm):\n",
+ " print parm.a,\" \",parm.ch\n",
+ "\n",
+ "#declare arg\n",
+ "arg=sample() \n",
+ "\n",
+ "#initialize arg\n",
+ "arg.a=1000\n",
+ "arg.ch='X'\n",
+ "\n",
+ "#call function\n",
+ "f1(arg)\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1000 X\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.3, Page Number: 227"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class stype:\n",
+ " a=None\n",
+ " b=None\n",
+ "\n",
+ "#Variable declaration\n",
+ "svar1=stype()\n",
+ "svar2=stype()\n",
+ "\n",
+ "svar1.a=svar1.b=10\n",
+ "svar2.a=svar2.b=20\n",
+ "\n",
+ "print \"Structures before assignment.\"\n",
+ "print \"svar1: \",svar1.a,' ',svar1.b\n",
+ "print \"svar1: \",svar2.a,' ',svar2.b\n",
+ "\n",
+ "svar2=svar1 #assign structures\n",
+ "\n",
+ "#Result\n",
+ "print \"\\nStructures before assignment.\"\n",
+ "print \"svar1: \",svar1.a,' ',svar1.b\n",
+ "print \"svar1: \",svar2.a,' ',svar2.b"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Structures before assignment.\n",
+ "svar1: 10 10\n",
+ "svar1: 20 20\n",
+ "\n",
+ "Structures before assignment.\n",
+ "svar1: 10 10\n",
+ "svar1: 10 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.4, Page Number: 230"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "import datetime\n",
+ "\n",
+ "date=datetime.datetime.now()\n",
+ "\n",
+ "#Result\n",
+ "print date.time()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "17:06:28.236000\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.5, Page Number: 231"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import datetime\n",
+ "\n",
+ "date=datetime.datetime.now()\n",
+ "\n",
+ "#Result\n",
+ "print date.ctime()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sat Sep 14 17:07:14 2013\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.6, Page Number: 232"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class mystruct:\n",
+ " a=None\n",
+ " b=None\n",
+ "\n",
+ "def f(var):\n",
+ " var[0].a=var[0].a*var[0].a\n",
+ " var[0].b=var[0].b/var[0].b\n",
+ " return var[0]\n",
+ " \n",
+ "#Variable declaration\n",
+ "x=[]\n",
+ "x.append(mystruct())\n",
+ "y=mystruct()\n",
+ "\n",
+ "#Initializing\n",
+ "x[0].a=10\n",
+ "x[0].b=20\n",
+ "\n",
+ "print \"Original x.a and x.b: \",x[0].a,' ',x[0].b\n",
+ "\n",
+ "y=f(x) #function call\n",
+ "\n",
+ "#Result\n",
+ "print \"Modified x.a and x.b: \",x[0].a,' ',x[0].b\n",
+ "print \"Modified y.a and y.b: \",y.a,' ',y.b\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Original x.a and x.b: 10 20\n",
+ "Modified x.a and x.b: 100 1\n",
+ "Modified 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": [
+ "\n",
+ "\n",
+ "class swap_bytes:\n",
+ " ch=[0,0]\n",
+ "\n",
+ "#Exchange of bytes\n",
+ "def disp_binary(u):\n",
+ " t=128\n",
+ " while not(t==0):\n",
+ " if u&t:\n",
+ " print \"1 \",\n",
+ " else:\n",
+ " print \"0 \",\n",
+ " t=t/2\n",
+ "\n",
+ "#Variable declaration\n",
+ "sb=swap_bytes()\n",
+ "\n",
+ "sb.ch[0]=15\n",
+ "\n",
+ "print \"Original bytes: \",\n",
+ "disp_binary(sb.ch[1])\n",
+ "disp_binary(sb.ch[0])\n",
+ "\n",
+ "#Exchange bytes\n",
+ "temp=sb.ch[0]\n",
+ "sb.ch[0]=sb.ch[1]\n",
+ "sb.ch[1]=temp\n",
+ "\n",
+ "#Result\n",
+ "print \"\\nExchanged bytes: \",\n",
+ "disp_binary(sb.ch[1])\n",
+ "disp_binary(sb.ch[0])\n",
+ "\n",
+ "\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Original bytes: 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 \n",
+ "Exchanged 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": [
+ "\n",
+ "ch='a'\n",
+ "\n",
+ "while True:\n",
+ " print \"\\n\",ch,\n",
+ " print bin(ord(ch)) #Display the bit pattern for each character\n",
+ " ch=chr(ord(ch)+1)\n",
+ " if ch=='r':\n",
+ " break"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "a 0b1100001\n",
+ "\n",
+ "b 0b1100010\n",
+ "\n",
+ "c 0b1100011\n",
+ "\n",
+ "d 0b1100100\n",
+ "\n",
+ "e 0b1100101\n",
+ "\n",
+ "f 0b1100110\n",
+ "\n",
+ "g 0b1100111\n",
+ "\n",
+ "h 0b1101000\n",
+ "\n",
+ "i 0b1101001\n",
+ "\n",
+ "j 0b1101010\n",
+ "\n",
+ "k 0b1101011\n",
+ "\n",
+ "l 0b1101100\n",
+ "\n",
+ "m 0b1101101\n",
+ "\n",
+ "n 0b1101110\n",
+ "\n",
+ "o 0b1101111\n",
+ "\n",
+ "p 0b1110000\n",
+ "\n",
+ "q 0b1110001\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.9, Page Number: 242"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "#Variable declaration\n",
+ "ch=['X','Y']\n",
+ "c=\"\"\n",
+ "def disp_bits(u):\n",
+ " t=128\n",
+ " global c\n",
+ " while not(t==0):\n",
+ " if u&t:\n",
+ " c=c+\"1\"\n",
+ " else:\n",
+ " c=c+\"0\"\n",
+ " t=t/2 \n",
+ " return c\n",
+ "\n",
+ "#Result\n",
+ "print \"union as chars: \",ch[0],ch[1]\n",
+ "print \"union as integer: \",\n",
+ "c= disp_bits(ord(ch[1]))\n",
+ "c= disp_bits(ord(ch[0]))\n",
+ "print int(str(c),2)\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "union as chars: X Y\n",
+ "union 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.ipynb b/C++_from_the_Ground/Chapter_11.ipynb
new file mode 100644
index 00000000..fdd70bcf
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_11.ipynb
@@ -0,0 +1,877 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:5428ee61d548a6c3eefd868dac96cac83b29f85d9bfd7876e9904bdfe87b0fad"
+ },
+ "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": [
+ "\n",
+ "\n",
+ "class queue:\n",
+ " #Initialize the queue \n",
+ " def __init__(self):\n",
+ " self.__sloc=0\n",
+ " self.__rloc=-1\n",
+ " self.__q=[]\n",
+ " \n",
+ " #Put an integer into the queue\n",
+ " def qput(self,i):\n",
+ " if self.__sloc==100:\n",
+ " print \"Queue is full.\"\n",
+ " return\n",
+ " self.__sloc+=1\n",
+ " self.__q.append(i)\n",
+ " \n",
+ " #Get an integer from the queue\n",
+ " def qget(self):\n",
+ " if self.__rloc==self.__sloc:\n",
+ " print \"Queue underflow\"\n",
+ " return\n",
+ " self.__rloc+=1\n",
+ " return self.__q[self.__rloc] \n",
+ " \n",
+ "\n",
+ " \n",
+ "#Create two queue objects\n",
+ "b=queue()\n",
+ "a=queue()\n",
+ "\n",
+ "a.qput(10)\n",
+ "b.qput(19)\n",
+ "\n",
+ "a.qput(20)\n",
+ "b.qput(1)\n",
+ "\n",
+ "#Result\n",
+ "print \"Contents of queue a: \",\n",
+ "print a.qget(),' ',a.qget()\n",
+ "\n",
+ "print \"Contents of queue b: \",\n",
+ "print b.qget(),' ',b.qget()\n",
+ "\n",
+ "\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Contents of queue a: 10 20\n",
+ "Contents 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": [
+ "\n",
+ " \n",
+ "class myclass:\n",
+ " def __init__(self):\n",
+ " self.__a=None #private member\n",
+ " self.b=None #public member\n",
+ " #public functions\n",
+ " def setlab(self,i):\n",
+ " self.__a=i #refer to a\n",
+ " self.b=i*i #refer to b\n",
+ " return\n",
+ " def geta(self):\n",
+ " return self.__a #refer to a\n",
+ " def reset(self):\n",
+ " #call setlab using self\n",
+ " self.setlab(0) #the object is already known\n",
+ " \n",
+ " \n",
+ "ob=myclass()\n",
+ "ob.setlab(5) #set ob.a and ob.b\n",
+ "print \"ob after setlab(5): \",ob.geta(),' ',\n",
+ "print ob.b #can access b because it is public\n",
+ "\n",
+ "ob.b=20 #can access b because it is public\n",
+ "print \"ob after ob.b=20: \",\n",
+ "print ob.geta(),' ',ob.b\n",
+ "\n",
+ "ob.reset()\n",
+ "print \"ob after ob.reset(): \",ob.geta(),' ',ob.b\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "ob after setlab(5): 5 25\n",
+ "ob after ob.b=20: 5 20\n",
+ "ob 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": [
+ "\n",
+ "class queue:\n",
+ " \n",
+ " #Constructor\n",
+ " def __init__(self): \n",
+ " self.__q=[]\n",
+ " self.__rloc=-1\n",
+ " self.__sloc=0\n",
+ " print \"Queue initialized\"\n",
+ " \n",
+ " #Destructor\n",
+ " def __del__(self):\n",
+ " print (\"Queue destroyed\")\n",
+ " \n",
+ " #Put an integer into the queue\n",
+ " def qput(self,i):\n",
+ " if self.__sloc == 100:\n",
+ " print \"Queue is full\"\n",
+ " return \n",
+ " self.__sloc+=1\n",
+ " self.__q.append(i)\n",
+ " \n",
+ " #Get an integer from the queue\n",
+ " def qget(self):\n",
+ " if self.__rloc==self.__sloc:\n",
+ " print \"Queue underflow\"\n",
+ " return\n",
+ " self.__rloc+=1\n",
+ " return self.__q[self.__rloc]\n",
+ " \n",
+ "#Create two queue objects\n",
+ "a=queue()\n",
+ "\n",
+ "\n",
+ "a.qput(10)\n",
+ "a.qput(20)\n",
+ "b=queue()\n",
+ "b.qput(19)\n",
+ "\n",
+ "b.qput(1)\n",
+ "\n",
+ "#Result\n",
+ "print a.qget(),' ',\n",
+ "print a.qget(),' ',\n",
+ "print b.qget(),' ',\n",
+ "print b.qget(),' '\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Queue initialized\n",
+ "Queue destroyed\n",
+ "Queue initialized\n",
+ "Queue destroyed\n",
+ "10 20 19 1 \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.4, Page Number: 257"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class queue: \n",
+ " \n",
+ " #Constructor\n",
+ " def __init__(self,i): \n",
+ " self.__q=[]\n",
+ " self.__rloc=-1\n",
+ " self.__sloc=0\n",
+ " self.__who=i\n",
+ " print \"Queue \",self.__who,\" initialized.\"\n",
+ " \n",
+ " #Destructor\n",
+ " def __del__():\n",
+ " print \"Queue \",self.__who,\" destroyed\"\n",
+ " \n",
+ " #Put an integer into the queue\n",
+ " def qput(self,i):\n",
+ " if self.__sloc == 100:\n",
+ " print \"Queue is full\"\n",
+ " return \n",
+ " self.__sloc+=1\n",
+ " self.__q.append(i)\n",
+ " \n",
+ " #Get an integer from the queue\n",
+ " def qget(self):\n",
+ " if self.__rloc==self.__sloc:\n",
+ " print \"Queue underflow\"\n",
+ " return\n",
+ " self.__rloc+=1\n",
+ " return self.__q[self.__rloc]\n",
+ " \n",
+ "a=queue(1)\n",
+ "b=queue(2)\n",
+ "\n",
+ "a.qput(10)\n",
+ "b.qput(19)\n",
+ "\n",
+ "a.qput(20)\n",
+ "b.qput(1)\n",
+ "\n",
+ "#Result\n",
+ "print a.qget(),' ',\n",
+ "print a.qget(),' ',\n",
+ "print b.qget(),' ',\n",
+ "print b.qget(),' '\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Queue 1 initialized.\n",
+ "Queue destroyed\n",
+ "Queue 2 initialized.\n",
+ "Queue destroyed\n",
+ "10 20 19 1 \n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.5, Page Number: 258"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class widget:\n",
+ " \n",
+ " #Pass two arguments to the constructor\n",
+ " def __init__(self,a,b):\n",
+ " self.__i=a\n",
+ " self.__j=b\n",
+ " \n",
+ " def put_widget(self):\n",
+ " print self.__i,\" \",self.__j\n",
+ "\n",
+ "#Initializing\n",
+ "x=widget(10,20)\n",
+ "y=widget(0,0)\n",
+ "\n",
+ "x.put_widget()\n",
+ "y.put_widget()\n",
+ "\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 20\n",
+ "0 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.6, Page Number: 259"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class myclass:\n",
+ " \n",
+ " #Constructor\n",
+ " def __init__(self,x):\n",
+ " self.a=x\n",
+ " #To get the vale of a\n",
+ " def get_a(self):\n",
+ " return self.a\n",
+ "\n",
+ "#Initializing\n",
+ "ob=myclass(4)\n",
+ "\n",
+ "#Result\n",
+ "print ob.get_a()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.7, Page Number: 260"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "class c1(Structure):\n",
+ " _fields_=[(\"__i\", c_int)] #private member\n",
+ " def get_i(self): #public finctions\n",
+ " return self.__i\n",
+ " def put_i(self,j):\n",
+ " self.__i=j\n",
+ "\n",
+ "#Variable declaration\n",
+ "s=c1()\n",
+ "\n",
+ "s.put_i(10)\n",
+ "\n",
+ "#Result\n",
+ "print s.get_i()\n",
+ " \n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.8, Page Number: 261"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class c1:\n",
+ " def __init__(self):\n",
+ " self.__i=None #private member\n",
+ " def get_i(self): #public finctions\n",
+ " return self.__i\n",
+ " def put_i(self,j):\n",
+ " self.__i=j\n",
+ "\n",
+ "#Variable declaration\n",
+ "s=c1()\n",
+ "\n",
+ "s.put_i(10)\n",
+ "\n",
+ "#Result\n",
+ "print s.get_i()\n",
+ " \n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.9, Page Number: 263 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "#Creates a union\n",
+ "class u_type(Union):\n",
+ " _fields_ = [(\"i\",c_short),\n",
+ " (\"ch\", c_char*2)]\n",
+ " #Constructor\n",
+ " def __init__(self,a):\n",
+ " self.i=a\n",
+ " \n",
+ " #Show the characters that comprise a short int.\n",
+ " def showchars(self):\n",
+ " print self.ch[0]\n",
+ " print self.ch[1]\n",
+ " \n",
+ " \n",
+ "u=u_type(1000)\n",
+ "\n",
+ "#Displays char of 1000\n",
+ "u.showchars()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\ufffd\n",
+ "\u0003\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.10, Page Number: 264 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class c1:\n",
+ " def __init__(self):\n",
+ " self.__i=None\n",
+ " def get_i(self):\n",
+ " return self.i\n",
+ " def put_i(self,j):\n",
+ " self.i=j\n",
+ "\n",
+ "#Variable declaration\n",
+ "s=c1()\n",
+ "\n",
+ "s.put_i(10)\n",
+ "\n",
+ "#Result\n",
+ "print s.get_i()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.11, Page Number: 265 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class c1:\n",
+ " def __init__(self):\n",
+ " self.__i=None\n",
+ " def get_i(self):\n",
+ " return self.i\n",
+ " def put_i(self,j):\n",
+ " self.i=j\n",
+ "\n",
+ "#Variable declaration\n",
+ "s=c1()\n",
+ "\n",
+ "s.put_i(10)\n",
+ "\n",
+ "#Result\n",
+ "print s.get_i()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.12, Page Number: 267 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class display:\n",
+ " def __init__(self):\n",
+ " width=None\n",
+ " height=None\n",
+ " res=None\n",
+ " def set_dim(self,w,h):\n",
+ " self.width=w\n",
+ " self.height=h\n",
+ " def get_dim(self):\n",
+ " return self.width,self.height\n",
+ " def set_res(self,r):\n",
+ " self.res=r\n",
+ " def get_res(self):\n",
+ " return self.res\n",
+ " \n",
+ "#Variable decleration\n",
+ "names=[\"low\",\"medium\",\"high\"] \n",
+ "(low,medium,high)=(0,1,2) #For enumeration type\n",
+ "w=None\n",
+ "h=None\n",
+ "display_mode=[]*3\n",
+ "\n",
+ "for i in range(3):\n",
+ " display_mode.append(display())\n",
+ "\n",
+ "#Initialize the array of objects using member functions\n",
+ "display_mode[0].set_res(low)\n",
+ "display_mode[0].set_dim(640,480)\n",
+ "\n",
+ "display_mode[1].set_res(medium)\n",
+ "display_mode[1].set_dim(800,600)\n",
+ "\n",
+ "display_mode[2].set_res(high)\n",
+ "display_mode[2].set_dim(1600,1200)\n",
+ "\n",
+ "#Result\n",
+ "print \"Available display modes: \"\n",
+ "for i in range(3):\n",
+ " print names[display_mode[i].get_res()],\" : \",\n",
+ " w,h=display_mode[i].get_dim()\n",
+ " print w,\" by \",h"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Available display modes: \n",
+ "low : 640 by 480\n",
+ "medium : 800 by 600\n",
+ "high : 1600 by 1200\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.13, Page Number: 268"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class samp:\n",
+ " __a=None\n",
+ " def __init__(self,n):\n",
+ " self.__a=n\n",
+ " def get_a(self):\n",
+ " return self.__a\n",
+ "\n",
+ "#Initializing the list\n",
+ "sampArray=[samp(-1),samp(-2),samp(-3),samp(-4)]\n",
+ "\n",
+ "#Display\n",
+ "for i in range(4):\n",
+ " print sampArray[i].get_a(),' ',"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "-1 -2 -3 -4 \n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.14, Page Number: 269"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class samp:\n",
+ " __a=None\n",
+ " __b=None\n",
+ " def __init__(self,n,m):\n",
+ " self.__a=n\n",
+ " self.__b=m\n",
+ " def get_a(self):\n",
+ " return self.__a\n",
+ " def get_b(self):\n",
+ " return self.__b\n",
+ " \n",
+ "#Initializing the list\n",
+ "sampArray=[[samp(1,2),samp(3,4)],\n",
+ " [samp(5,6),samp(7,8)],\n",
+ " [samp(9,10),samp(11,12)],\n",
+ " [samp(13,14),samp(15,16)]]\n",
+ "\n",
+ "#Display\n",
+ "for i in range(4):\n",
+ " print sampArray[i][0].get_a(),' ',\n",
+ " print sampArray[i][0].get_b()\n",
+ " print sampArray[i][1].get_a(),' ',\n",
+ " print sampArray[i][1].get_b()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 2\n",
+ "3 4\n",
+ "5 6\n",
+ "7 8\n",
+ "9 10\n",
+ "11 12\n",
+ "13 14\n",
+ "15 16\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.15, Page Number: 270"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "class P_example(Structure):\n",
+ " __num=None\n",
+ " def set_num(self,val):\n",
+ " self.__num=val\n",
+ " def show_num(self):\n",
+ " print self.__num\n",
+ "\n",
+ "#Variable declaration\n",
+ "ob=P_example() #Declare an object to the structure\n",
+ "p=POINTER(P_example) #Declare a pointer to the structure\n",
+ "\n",
+ "ob.set_num(1) #access ob directly\n",
+ "ob.show_num()\n",
+ "\n",
+ "p=ob #assign p the address of ob\n",
+ "p.show_num() #access ob using pointer\n",
+ " \n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n",
+ "1\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.16, Page Number: 271"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class P_example(Structure):\n",
+ " __num=None\n",
+ " def set_num(self,val):\n",
+ " self.__num=val\n",
+ " def show_num(self):\n",
+ " print self.__num\n",
+ " \n",
+ "#Variable declaration\n",
+ "ob=[P_example(),P_example()] #Declare an object to the structure\n",
+ "p=POINTER(P_example) #Declare a pointer to the structure\n",
+ "\n",
+ "ob[0].set_num(10) #access objects directly\n",
+ "ob[1].set_num(20)\n",
+ "\n",
+ "p=ob #obtain pointer to first element\n",
+ "p[0].show_num() #access ob using pointer\n",
+ "\n",
+ "p[1].show_num()\n",
+ "\n",
+ "p[0].show_num()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n",
+ "20\n",
+ "10\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.ipynb b/C++_from_the_Ground/Chapter_12.ipynb
new file mode 100644
index 00000000..a29c9b4d
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_12.ipynb
@@ -0,0 +1,904 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:0b4a52abc3f3246965d1e59241352e867e78e448510ae25e91b7d38dc4dcc202"
+ },
+ "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": [
+ "\n",
+ "\n",
+ "class myclass:\n",
+ " __a=None\n",
+ " __b=None\n",
+ " def __init__(self,i,j):\n",
+ " self.__a=i\n",
+ " self.__b=j\n",
+ " def sum(self,x): #Friend function\n",
+ " return sum1(x)\n",
+ " \n",
+ "def sum1(x): \n",
+ " return x._myclass__a +x._myclass__b #accessing private members\n",
+ "\n",
+ "#Variable declaration\n",
+ "n=myclass(3,4)\n",
+ "\n",
+ "#Result\n",
+ "print n.sum(n)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.2, Page Number: 275"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class c1:\n",
+ " __status=None\n",
+ " def set_status(self,state):\n",
+ " self.__status=state\n",
+ " \n",
+ "class c2:\n",
+ " __status=None\n",
+ " def set_status(self,state):\n",
+ " self.status=state\n",
+ " \n",
+ "#Friend function \n",
+ "def idle(a,b):\n",
+ " if a._c1__status or b._c2__status :\n",
+ " return 0\n",
+ " else:\n",
+ " return 1\n",
+ " \n",
+ "#variable declarations\n",
+ "def IDLE(): #Constants\n",
+ " return 0\n",
+ "def INUSE():\n",
+ " return 1\n",
+ "x=c1()\n",
+ "y=c2()\n",
+ "\n",
+ "x.set_status(IDLE())\n",
+ "y.set_status(IDLE())\n",
+ "\n",
+ "if idle(x,y):\n",
+ " print \"Screen Can Be Used.\"\n",
+ " \n",
+ "x.set_status(INUSE())\n",
+ "\n",
+ "if idle(x,y):\n",
+ " print \"Screen Can Be Used.\"\n",
+ "else:\n",
+ " print \"Pop-up In Use.\"\n",
+ " \n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Screen Can Be Used.\n",
+ "Pop-up In Use.\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.3, Page Number: 277"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def IDLE(): \n",
+ " return 0\n",
+ "def INUSE():\n",
+ " return 1\n",
+ "\n",
+ "class c1:\n",
+ " __status=None\n",
+ " def set_status(self,state):\n",
+ " self.__status=state\n",
+ " def idle(self,b): #now a member of c1\n",
+ " if self.__status or b._c2__status :\n",
+ " return 0\n",
+ " else:\n",
+ " return 1\n",
+ " \n",
+ "class c2:\n",
+ " __status=None #IDLE if off INUSE if on screen\n",
+ " def set_status(self,state):\n",
+ " self.status=state\n",
+ " \n",
+ "#Variable declarations \n",
+ "x=c1()\n",
+ "y=c2()\n",
+ "\n",
+ "x.set_status(IDLE())\n",
+ "y.set_status(IDLE())\n",
+ "\n",
+ "if idle(x,y):\n",
+ " print \"Screen Can Be Used.\"\n",
+ " \n",
+ "x.set_status(INUSE())\n",
+ "\n",
+ "if idle(x,y):\n",
+ " print \"Screen Can Be Used.\"\n",
+ "else:\n",
+ " print \"Pop-up In Use.\"\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Screen Can Be Used.\n",
+ "Pop-up In Use.\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.4, Page Number: 278"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "import time,string\n",
+ "\n",
+ "class 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": [
+ "\n",
+ "import time,string\n",
+ "\n",
+ "class 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": [
+ "\n",
+ "class myclass:\n",
+ " __a=None #private members\n",
+ " __b=None\n",
+ " def setab(self,i,j): #publc functons\n",
+ " self.__a=i\n",
+ " self.__b=j\n",
+ " def showab(self):\n",
+ " print \"a is \",self.__a\n",
+ " print \"b is \",self.__b\n",
+ "\n",
+ "#Variable declaration\n",
+ "ob1 = myclass()\n",
+ "ob2 = myclass()\n",
+ "\n",
+ "#Intalizing\n",
+ "ob1.setab(10,20)\n",
+ "ob2.setab(0,0)\n",
+ "\n",
+ "print \"ob1 before assignment: \"\n",
+ "ob1.showab()\n",
+ "print \"ob2 before assignment: \"\n",
+ "ob2.showab()\n",
+ "\n",
+ "ob2 = ob1 #assign ob1 to ob2\n",
+ "\n",
+ "#Result\n",
+ "print \"ob1 after assignment: \"\n",
+ "ob1.showab()\n",
+ "print \"ob2 after assignment: \"\n",
+ "ob2.showab()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "ob1 before assignment: \n",
+ "a is 10\n",
+ "b is 20\n",
+ "ob2 before assignment: \n",
+ "a is 0\n",
+ "b is 0\n",
+ "ob1 after assignment: \n",
+ "a is 10\n",
+ "b is 20\n",
+ "ob2 after assignment: \n",
+ "a is 10\n",
+ "b is 20\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.7, Page Number: 283"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "from copy import deepcopy\n",
+ " \n",
+ "class OBJ:\n",
+ " def set_i(self,x):\n",
+ " self.__i=x\n",
+ " def out_i(self):\n",
+ " print self.__i,\n",
+ " \n",
+ "def f(x):\n",
+ " x=deepcopy(x)\n",
+ " x.out_i() #outputs 10\n",
+ " x.set_i(100) #this affects only local copy\n",
+ " x.out_i() #outputs 100\n",
+ " \n",
+ "#Variable declaration\n",
+ "o=OBJ()\n",
+ "o.set_i(10)\n",
+ "f(o) \n",
+ "o.out_i() #still outputs 10, value of i unchanged\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 100 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.8, Page Number: 284 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class myclass: \n",
+ " def __init__(self,i):\n",
+ " self.__val=i\n",
+ " print \"Constructing\"\n",
+ " def __del__(self):\n",
+ " print \"Destructing\"\n",
+ " def getval(self):\n",
+ " return self.__val\n",
+ " \n",
+ "def display(ob):\n",
+ " print ob.getval()\n",
+ "\n",
+ "#Varable declaration\n",
+ "a=myclass(10)\n",
+ "\n",
+ "display(a)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Constructing\n",
+ "Destructing\n",
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.9, Page Number: 286"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "class myclass:\n",
+ " def __init__(self,i):\n",
+ " print \"Allocating p\"\n",
+ " self.p=pointer(c_int(i))\n",
+ " def __del__(self):\n",
+ " print \"Freeing p\"\n",
+ " def getval(self):\n",
+ " return self.p[0]\n",
+ " \n",
+ "def display(ob):\n",
+ " print ob.getval()\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=myclass(10)\n",
+ "\n",
+ "display(a)\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Allocating p\n",
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.10, Page Number: 287"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "from ctypes import *\n",
+ "class myclass:\n",
+ " def __init__(self,i):\n",
+ " print \"Allocating p\"\n",
+ " self.p=pointer(c_int(i))\n",
+ " def __del__(self):\n",
+ " print \"Freeing p\"\n",
+ " def getval(self):\n",
+ " return self.p[0]\n",
+ " \n",
+ "def display(ob):\n",
+ " print ob[0].getval()\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=[]\n",
+ "a.append(myclass(10))\n",
+ "\n",
+ "display(a)\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Freeing p\n",
+ "Allocating p\n",
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.11, Page Number: 288"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class sample:\n",
+ " __s=None\n",
+ " def show(self):\n",
+ " print self.__s\n",
+ " def set(self,str):\n",
+ " self.__s=str\n",
+ "\n",
+ "#Return an object of type sample\n",
+ "def input():\n",
+ " str=sample()\n",
+ " instr = \"Hello\" #User input\n",
+ " str.set(instr)\n",
+ " return str\n",
+ "\n",
+ "#Variable declaration\n",
+ "ob=sample()\n",
+ "\n",
+ "#assign returned object to ob\n",
+ "ob=input()\n",
+ "\n",
+ "#Result\n",
+ "ob.show()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.12, Page Number: 289"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class sample:\n",
+ " __s=None\n",
+ " def __init__(self):\n",
+ " self.__s=0\n",
+ " def __del__(self): \n",
+ " print \"Freeing p\"\n",
+ " def show(self):\n",
+ " print self.__s\n",
+ " def set(self,str):\n",
+ " self.__s=str\n",
+ " \n",
+ "#This function takes one object parameter\n",
+ "def input():\n",
+ " str=sample()\n",
+ " instr=\"Hello\" #User input\n",
+ " str.set(instr)\n",
+ " return str\n",
+ "\n",
+ "#Variable declaration\n",
+ "ob=sample()\n",
+ "\n",
+ "#assign returned object to ob\n",
+ "ob=input()\n",
+ "\n",
+ "#Result\n",
+ "ob.show()\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Freeing p\n",
+ "Freeing p\n",
+ "Hello\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.13, Page Number: 292"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class myclass:\n",
+ " __p=None\n",
+ " def __init__(self,i):\n",
+ " if isinstance(i,int):\n",
+ " print \"Allocating p\"\n",
+ " self.__p=i\n",
+ " else:\n",
+ " print \"Copy constructor called\"\n",
+ " self.__p=i.getval()\n",
+ " def __del__(self): \n",
+ " print \"Freeing p\"\n",
+ " def getval(self):\n",
+ " return self.__p\n",
+ " \n",
+ "#This function takes one object parameter\n",
+ "def display(ob):\n",
+ " print ob.getval()\n",
+ "\n",
+ "#Variable declaration\n",
+ "ob=myclass(10)\n",
+ "\n",
+ "#Result\n",
+ "display(ob)\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Allocating p\n",
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.14, Page Number: 294"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "class myclass:\n",
+ " __p=None\n",
+ " def __init__(self,i):\n",
+ " if isinstance(i,int):\n",
+ " print \"Allocating p\"\n",
+ " self.__p=i\n",
+ " else:\n",
+ " print \"Copy constructor called\"\n",
+ " self.__p=i.getval()\n",
+ " def __del__(self): \n",
+ " print \"Freeing p\"\n",
+ " def getval(self):\n",
+ " return self.__p\n",
+ " \n",
+ "\n",
+ "#Variable declaration\n",
+ "a=myclass(10) #calls normal constructor\n",
+ "b=myclass(a) #calls copy constructor\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Allocating p\n",
+ "Copy constructor called\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.15, Page Number: 295"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class myclass:\n",
+ " def __init__(self,i=0):\n",
+ " if isinstance(i,int):\n",
+ " print \"Normal constructor\"\n",
+ " else:\n",
+ " print \"Copy constructor\"\n",
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=myclass() #calls normal constructor\n",
+ "\n",
+ "f=myclass()\n",
+ "a=myclass(f) #Invoke copyconstructor\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Normal constructor\n",
+ "Normal constructor\n",
+ "Copy constructor\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.16, Page Number: 297"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class c1:\n",
+ " def __init__(self):\n",
+ " self.__i=None\n",
+ " def load_i(self,val):\n",
+ " self.__i=val\n",
+ " def get_i(self):\n",
+ " return self.__i\n",
+ " \n",
+ "#Variable declaration \n",
+ "o=c1()\n",
+ "\n",
+ "o.load_i(100)\n",
+ "\n",
+ "#Result\n",
+ "print o.get_i()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "100\n"
+ ]
+ }
+ ],
+ "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.ipynb b/C++_from_the_Ground/Chapter_13.ipynb
new file mode 100644
index 00000000..6d12a5a7
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_13.ipynb
@@ -0,0 +1,883 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:be0df45a07fa8844875025463a4211b5faab9834d4e1dd155b475b36ae6ea27e"
+ },
+ "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": [
+ "\n",
+ "\n",
+ "class three_d:\n",
+ " def __init__(self,i=None,j=None,k=None):\n",
+ " if i==None:\n",
+ " self.x=self.y=self.z=0\n",
+ " else:\n",
+ " self.x=i\n",
+ " self.y=j\n",
+ " self.z=k\n",
+ " #Overload +\n",
+ " def __add__(self,op2):\n",
+ " temp=three_d()\n",
+ " temp.x=self.x + op2.x #These are integer additions\n",
+ " temp.y=self.y + op2.y #and the + retains its original\n",
+ " temp.z=self.z + op2.z #meaning relative to them.\n",
+ " return temp\n",
+ " #Overload assignment\n",
+ " def __assign__(self,op2):\n",
+ " self.x=op2.x #These are integer assignments\n",
+ " self.y=op2.y #and the = retains its original \n",
+ " self.z=op2.z #meaning relative to them\n",
+ " return self\n",
+ " #Show x,y,z coordinates\n",
+ " def show(self):\n",
+ " print self.x,\",\",self.y,\",\",self.z\n",
+ " \n",
+ "#Variable declaration\n",
+ "a=three_d(1,2,3)\n",
+ "b=three_d(10,10,10)\n",
+ "c=three_d()\n",
+ "\n",
+ "a.show()\n",
+ "b.show()\n",
+ "\n",
+ "#add a and b together\n",
+ "c=a+b\n",
+ "c.show()\n",
+ "\n",
+ "#add a,b and c together\n",
+ "c=a+b+c\n",
+ "c.show()\n",
+ "\n",
+ "#demonstrate multiple assignment\n",
+ "c=b=a\n",
+ "c.show()\n",
+ "b.show()\n",
+ " \n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 , 2 , 3\n",
+ "10 , 10 , 10\n",
+ "11 , 12 , 13\n",
+ "22 , 24 , 26\n",
+ "1 , 2 , 3\n",
+ "1 , 2 , 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.2, Page Number: 303"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class three_d:\n",
+ " def __init__(self,i=None,j=None,k=None):\n",
+ " if i==None:\n",
+ " self.x=self.y=self.z=0\n",
+ " else:\n",
+ " self.x=i\n",
+ " self.y=j\n",
+ " self.z=k\n",
+ " #Overload +\n",
+ " def __add__(self,op2):\n",
+ " temp=three_d()\n",
+ " temp.x=self.x + op2.x #These are integer additions\n",
+ " temp.y=self.y + op2.y #and the + retains its original\n",
+ " temp.z=self.z + op2.z #meaning relative to them.\n",
+ " return temp\n",
+ " #Overload assignment\n",
+ " def __assign__(self,op2):\n",
+ " self.x=op2.x #These are integer assignments\n",
+ " self.y=op2.y #and the = retains its original \n",
+ " self.z=op2.z #meaning relative to them\n",
+ " return self\n",
+ " #Overload the increment operator\n",
+ " def __iadd__(self,op2):\n",
+ " self.x+=op2\n",
+ " self.y+=op2\n",
+ " self.z+=op2\n",
+ " return self\n",
+ " #Show x,y,z coordinates\n",
+ " def show(self):\n",
+ " print self.x,\",\",self.y,\",\",self.z\n",
+ " \n",
+ "a=three_d(1,2,3)\n",
+ "b=three_d(10,10,10)\n",
+ "c=three_d()\n",
+ "\n",
+ "a.show()\n",
+ "b.show()\n",
+ "\n",
+ "#add a and b together\n",
+ "c=a+b\n",
+ "c.show()\n",
+ "\n",
+ "#add a,b and c together\n",
+ "c=a+b+c\n",
+ "c.show()\n",
+ "\n",
+ "#demonstrate multiple assignment\n",
+ "c=b=a\n",
+ "c.show()\n",
+ "b.show()\n",
+ " \n",
+ "#Increment c\n",
+ "c+=1\n",
+ "c.show()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 , 2 , 3\n",
+ "10 , 10 , 10\n",
+ "11 , 12 , 13\n",
+ "22 , 24 , 26\n",
+ "1 , 2 , 3\n",
+ "1 , 2 , 3\n",
+ "2 , 3 , 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.3, Page Number: 306"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class three_d:\n",
+ " def __init__(self,i=None,j=None,k=None):\n",
+ " if i==None:\n",
+ " self.x=self.y=self.z=0\n",
+ " else:\n",
+ " self.x=i\n",
+ " self.y=j\n",
+ " self.z=k\n",
+ " #Overload +\n",
+ " def __add__(self,op2):\n",
+ " temp=three_d()\n",
+ " temp.x=self.x + op2.x #These are integer additions\n",
+ " temp.y=self.y + op2.y #and the + retains its original\n",
+ " temp.z=self.z + op2.z #meaning relative to them.\n",
+ " return temp\n",
+ " #Overload assignment\n",
+ " def __assign__(self,op2):\n",
+ " self.x=op2.x #These are integer assignments\n",
+ " self.y=op2.y #and the = retains its original \n",
+ " self.z=op2.z #meaning relative to them\n",
+ " return self\n",
+ " #Overload the increment operator\n",
+ " def __iadd__(self,op2):\n",
+ " self.x+=op2\n",
+ " self.y+=op2\n",
+ " self.z+=op2\n",
+ " return self\n",
+ " #Show x,y,z coordinates\n",
+ " def show(self):\n",
+ " print self.x,\",\",self.y,\",\",self.z\n",
+ " \n",
+ "a=three_d(1,2,3)\n",
+ "b=three_d(10,10,10)\n",
+ "c=three_d()\n",
+ "\n",
+ "a.show()\n",
+ "b.show()\n",
+ "\n",
+ "#add a and b together\n",
+ "c=a+b\n",
+ "c.show()\n",
+ "\n",
+ "#add a,b and c together\n",
+ "c=a+b+c\n",
+ "c.show()\n",
+ "\n",
+ "#demonstrate multiple assignment\n",
+ "c=b=a\n",
+ "c.show()\n",
+ "b.show()\n",
+ " \n",
+ "#Increment c (prefix)\n",
+ "c+=1\n",
+ "c.show()\n",
+ "\n",
+ "#Increment c (postfix)\n",
+ "c+=1\n",
+ "c.show()\n",
+ "\n",
+ "#Implementing prefix\n",
+ "c+=1\n",
+ "a=c\n",
+ "a.show()\n",
+ "c.show()\n",
+ "\n",
+ "#Implementing postfix\n",
+ "a=c\n",
+ "a.show()\n",
+ "c+=1\n",
+ "c.show()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 , 2 , 3\n",
+ "10 , 10 , 10\n",
+ "11 , 12 , 13\n",
+ "22 , 24 , 26\n",
+ "1 , 2 , 3\n",
+ "1 , 2 , 3\n",
+ "2 , 3 , 4\n",
+ "3 , 4 , 5\n",
+ "4 , 5 , 6\n",
+ "4 , 5 , 6\n",
+ "4 , 5 , 6\n",
+ "5 , 6 , 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.4, Page Number: 310"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class three_d:\n",
+ " def __init__(self,i=None,j=None,k=None):\n",
+ " if i==None:\n",
+ " self.x=self.y=self.z=0\n",
+ " else:\n",
+ " self.x=i\n",
+ " self.y=j\n",
+ " self.z=k\n",
+ " #Overload +\n",
+ " def __add__(self,op2):\n",
+ " return add(self,op2)\n",
+ " #Overload assignment\n",
+ " def __assign__(self,op2):\n",
+ " self.x=op2.x #These are integer assignments\n",
+ " self.y=op2.y #and the = retains its original \n",
+ " self.z=op2.z #meaning relative to them\n",
+ " return self\n",
+ " #Show x,y,z coordinates\n",
+ " def show(self):\n",
+ " print self.x,\",\",self.y,\",\",self.z\n",
+ " \n",
+ "#friending the funcion\n",
+ "def add(op1,op2):\n",
+ " temp=three_d()\n",
+ " temp.x=op1.x + op2.x #These are integer additions\n",
+ " temp.y=op1.y + op2.y #and the + retains its original\n",
+ " temp.z=op1.z + op2.z #meaning relative to them.\n",
+ " return temp\n",
+ "\n",
+ "a=three_d(1,2,3)\n",
+ "b=three_d(10,10,10)\n",
+ "c=three_d()\n",
+ "\n",
+ "a.show()\n",
+ "b.show()\n",
+ "\n",
+ "#add a and b together\n",
+ "c=a+b\n",
+ "c.show()\n",
+ "\n",
+ "#add a,b and c together\n",
+ "c=a+b+c\n",
+ "c.show()\n",
+ "\n",
+ "#demonstrate multiple assignment\n",
+ "c=b=a\n",
+ "c.show()\n",
+ "b.show()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 , 2 , 3\n",
+ "10 , 10 , 10\n",
+ "11 , 12 , 13\n",
+ "22 , 24 , 26\n",
+ "1 , 2 , 3\n",
+ "1 , 2 , 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.5, Page Number: 311"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class CL:\n",
+ " def __init__(self):\n",
+ " self.count=0\n",
+ " def __assign__(self,obj):\n",
+ " self.count=obj.count\n",
+ " return self\n",
+ " def __add__(self,i): \n",
+ " return add(self,i)\n",
+ " def __radd__(self,i):\n",
+ " return radd(self,i)\n",
+ "\n",
+ "#This handles ob + int\n",
+ "def add(ob,i):\n",
+ " temp=CL()\n",
+ " temp.count=ob.count+i\n",
+ " return temp\n",
+ " \n",
+ "#This handles int + ob \n",
+ "def radd(ob,i):\n",
+ " temp=CL()\n",
+ " temp.count=i+ob.count\n",
+ " return temp\n",
+ "\n",
+ "#Variable declaration\n",
+ "o=CL()\n",
+ "o.count = 10\n",
+ "\n",
+ "#Result\n",
+ "print o.count, #outputs 10\n",
+ "o=10+o\n",
+ "print o.count, #outputs 20\n",
+ "o=o+12\n",
+ "print o.count #outputs 32\n",
+ "\n",
+ "\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 20 32\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.6, Page Number: 314"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class three_d:\n",
+ " def __init__(self,i=None,j=None,k=None):\n",
+ " if i==None:\n",
+ " self.x=self.y=self.z=0\n",
+ " else:\n",
+ " self.x=i\n",
+ " self.y=j\n",
+ " self.z=k\n",
+ " #Overload +\n",
+ " def __add__(self,op2):\n",
+ " return add(self,op2)\n",
+ " #Overload assignment\n",
+ " def __assign__(self,op2):\n",
+ " self.x=op2.x #These are integer assignments\n",
+ " self.y=op2.y #and the = retains its original \n",
+ " self.z=op2.z #meaning relative to them\n",
+ " return self\n",
+ " #Overload the increment operator\n",
+ " def __iadd__(self,op2):\n",
+ " return iadd(self,op2)\n",
+ " #Show x,y,z coordinates\n",
+ " def show(self):\n",
+ " print self.x,\",\",self.y,\",\",self.z\n",
+ "\n",
+ "#friending the funcion\n",
+ "def add(op1,op2):\n",
+ " temp=three_d()\n",
+ " temp.x=op1.x + op2.x #These are integer additions\n",
+ " temp.y=op1.y + op2.y #and the + retains its original\n",
+ " temp.z=op1.z + op2.z #meaning relative to them.\n",
+ " return temp\n",
+ "def iadd(op1,op2):\n",
+ " op1.x+=op2\n",
+ " op1.y+=op2\n",
+ " op1.z+=op2\n",
+ " return op1\n",
+ " \n",
+ "a=three_d(1,2,3)\n",
+ "b=three_d(10,10,10)\n",
+ "c=three_d()\n",
+ "\n",
+ "a.show()\n",
+ "b.show()\n",
+ "\n",
+ "#add a and b together\n",
+ "c=a+b\n",
+ "c.show()\n",
+ "\n",
+ "#add a,b and c together\n",
+ "c=a+b+c\n",
+ "c.show()\n",
+ "\n",
+ "#demonstrate multiple assignment\n",
+ "c=b=a\n",
+ "c.show()\n",
+ "b.show()\n",
+ " \n",
+ "#Increment c (prefix)\n",
+ "c+=1\n",
+ "c.show()\n",
+ "\n",
+ "#Increment c (postfix)\n",
+ "c+=1\n",
+ "c.show()\n",
+ "\n",
+ "#Implementing prefix\n",
+ "c+=1\n",
+ "a=c\n",
+ "a.show()\n",
+ "c.show()\n",
+ "\n",
+ "#Implementing postfix\n",
+ "a=c\n",
+ "a.show()\n",
+ "c+=1\n",
+ "c.show()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 , 2 , 3\n",
+ "10 , 10 , 10\n",
+ "11 , 12 , 13\n",
+ "22 , 24 , 26\n",
+ "1 , 2 , 3\n",
+ "1 , 2 , 3\n",
+ "2 , 3 , 4\n",
+ "3 , 4 , 5\n",
+ "4 , 5 , 6\n",
+ "4 , 5 , 6\n",
+ "4 , 5 , 6\n",
+ "5 , 6 , 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.7, Page Number: 318"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class sample:\n",
+ " def __init__(self,ob=0):\n",
+ " if isinstance(ob,int):\n",
+ " #Normal constructor\n",
+ " self.__s=\"\"\n",
+ " return\n",
+ " else:\n",
+ " #Copy constructor\n",
+ " self.__s=obj._sample__s\n",
+ " return\n",
+ " def __del__(self):\n",
+ " print \"Freeing s\"\n",
+ " def show(self):\n",
+ " print self.__s\n",
+ " def set(self,str):\n",
+ " self.__s=str\n",
+ " def __assign__(self,ob): #Overload assignment\n",
+ " self.s=ob._sample__s\n",
+ " return self\n",
+ " \n",
+ "def input():\n",
+ " str=sample()\n",
+ " instr=\"Hello\" #User input\n",
+ " str.set(instr)\n",
+ " return str\n",
+ "\n",
+ "ob=sample()\n",
+ "\n",
+ "#assign returned object to ob\n",
+ "ob=input()\n",
+ "\n",
+ "#Result\n",
+ "ob.show()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Freeing s\n",
+ "Freeing s\n",
+ "Hello\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.8, Page Number: 321"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class atype:\n",
+ " def __init__(self):\n",
+ " self.__a=[]\n",
+ " for i in range(SIZE):\n",
+ " self.__a.append(i)\n",
+ " def a(self,i):\n",
+ " return self.__a[i]\n",
+ " \n",
+ "#Variable declaration\n",
+ "SIZE=3\n",
+ "ob=atype()\n",
+ "\n",
+ "#Result\n",
+ "print ob.a(2),\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.9, Page Number: 322"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class atype:\n",
+ " def __init__(self):\n",
+ " self.__a=[]\n",
+ " for i in range(SIZE):\n",
+ " self.__a.append(i)\n",
+ " def a(self,i,j=None):\n",
+ " if j==None:\n",
+ " return self.__a[i]\n",
+ " else:\n",
+ " self.__a[i]=j\n",
+ " \n",
+ "#Variable declaration\n",
+ "SIZE=3 \n",
+ "ob=atype()\n",
+ "\n",
+ "print ob.a(2), #displays 2\n",
+ "\n",
+ "ob.a(2,25)\n",
+ "\n",
+ "print ob.a(2) #now displays 25"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2 25\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.10, Page Number: 323"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class atype:\n",
+ " def __init__(self):\n",
+ " self.__a=[]\n",
+ " for i in range(SIZE):\n",
+ " self.__a.append(i)\n",
+ " def a(self,i,j=None):\n",
+ " if (i<0 or i>SIZE-1):\n",
+ " print \"Index value of\",\n",
+ " print i,\"is out of bounds.\"\n",
+ " return\n",
+ " if j==None:\n",
+ " return self.__a[i]\n",
+ " else:\n",
+ " self.__a[i]=j\n",
+ " \n",
+ "#Variable declaration\n",
+ "SIZE=3 \n",
+ "ob=atype()\n",
+ "\n",
+ "print ob.a(2), #displays 2\n",
+ "\n",
+ "ob.a(2,25)\n",
+ "\n",
+ "print ob.a(2) #now displays 25\n",
+ "\n",
+ "ob.a(44,3) #generates runtime error, 3 out of bounds"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2 25\n",
+ "Index 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": [
+ "\n",
+ "\n",
+ "class three_d:\n",
+ " def __init__(self,i=None,j=None,k=None):\n",
+ " if i==None:\n",
+ " self.x=self.y=self.z=0 #3-D coordinates\n",
+ " else:\n",
+ " self.x=i\n",
+ " self.y=j\n",
+ " self.z=k\n",
+ " #Show X,Y,Z coordinates\n",
+ " def show(self):\n",
+ " print self.x,\",\",self.y,\",\",self.z\n",
+ " #Overload ()\n",
+ " def a(self,a,b,c):\n",
+ " temp = three_d()\n",
+ " temp.x=self.x+a\n",
+ " temp.y=self.y+b\n",
+ " temp.z=self.z+c\n",
+ " return temp\n",
+ " \n",
+ "#Variable declaration\n",
+ "ob1=three_d(1,2,3)\n",
+ "\n",
+ "ob2=ob1.a(10,11,12) #invoke operator ()\n",
+ "\n",
+ "#Result\n",
+ "print \"ob1: \",\n",
+ "ob1.show()\n",
+ "print \"ob2: \",\n",
+ "ob2.show()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "ob1: 1 , 2 , 3\n",
+ "ob2: 11 , 13 , 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.12, Page Number: 326"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class str_type:\n",
+ " def __init__(self,str=\"\"):\n",
+ " self.__string=str\n",
+ " #String concatenation\n",
+ " def __add__(self,str):\n",
+ " temp=str_type()\n",
+ " if isinstance(str,str_type):\n",
+ " temp.__string=self.__string+str.__string\n",
+ " else:\n",
+ " temp.__string=self.__string+str\n",
+ " return temp\n",
+ " #String copy\n",
+ " def __assign__(self,str):\n",
+ " if isinstance(str,str_type):\n",
+ " self.__string=str.__string\n",
+ " else:\n",
+ " self.__string=str\n",
+ " return self\n",
+ " def show_str(self):\n",
+ " print self.__string\n",
+ " \n",
+ "a=str_type(\"Hello \")\n",
+ "b=str_type(\"There\")\n",
+ "c=a+b\n",
+ "c.show_str()\n",
+ "\n",
+ "a=str_type(\"to program in because\")\n",
+ "a.show_str()\n",
+ "\n",
+ "b=c=str_type(\"C++ is fun\")\n",
+ "\n",
+ "c=c+\" \"+a+\" \"+b\n",
+ "c.show_str()\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello There\n",
+ "to program in because\n",
+ "C++ 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.ipynb b/C++_from_the_Ground/Chapter_14.ipynb
new file mode 100644
index 00000000..2f8447ba
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_14.ipynb
@@ -0,0 +1,924 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:d3cc78f10810f320519cd1c8becefb4790578385b5c2b528dc88273651f18c12"
+ },
+ "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": [
+ "\n",
+ "\n",
+ "class road_vehicle:\n",
+ " def __init__(self):\n",
+ " self.__wheels=None\n",
+ " self.__passengers=None\n",
+ " def set_wheels(self,num):\n",
+ " self.__wheels=num\n",
+ " def get_wheels(self):\n",
+ " return self.__wheels\n",
+ " def set_pass(self,num):\n",
+ " self.__passengers=num\n",
+ " def get_pass(self):\n",
+ " return self.__passengers\n",
+ "\n",
+ "#Define a truck\n",
+ "class truck(road_vehicle):\n",
+ " def __init__(self):\n",
+ " self.__cargo=None\n",
+ " def set_cargo(self,size):\n",
+ " self.__cargo=size\n",
+ " def get_cargo(self):\n",
+ " return self.__cargo\n",
+ " def show(self):\n",
+ " print \"wheels: \",self.get_wheels()\n",
+ " print \"passengers: \",self.get_pass()\n",
+ " print \"cargo capacity in cubic feet: \",self.__cargo\n",
+ " \n",
+ "#Define an enum type\n",
+ "(car,van,wagon)=(1,2,3)\n",
+ "type=[\"car\",\"van\",\"wagon\"]\n",
+ " \n",
+ "#Define an automobile\n",
+ "class automobile(road_vehicle):\n",
+ " def __init__(self):\n",
+ " self.car_type=None\n",
+ " def set_type(self,t):\n",
+ " self.car_type=t\n",
+ " def get_type(self):\n",
+ " return self.car_type\n",
+ " def show(self):\n",
+ " print \"wheels: \",self.get_wheels()\n",
+ " print \"passengers: \",self.get_pass()\n",
+ " print \"type: \",\n",
+ " if self.get_type()==1:\n",
+ " print \"car\"\n",
+ " elif self.get_type()==2:\n",
+ " print \"van\"\n",
+ " elif self.get_type()==3:\n",
+ " print \"wagon\"\n",
+ " \n",
+ "#Variable declaration\n",
+ "t1=truck()\n",
+ "t2=truck()\n",
+ "c=automobile()\n",
+ "\n",
+ "t1.set_wheels(18)\n",
+ "t1.set_pass(2)\n",
+ "t1.set_cargo(3200)\n",
+ "\n",
+ "t2.set_wheels(6)\n",
+ "t2.set_pass(3)\n",
+ "t2.set_cargo(1200)\n",
+ "\n",
+ "t1.show()\n",
+ "t2.show()\n",
+ "\n",
+ "c.set_wheels(4)\n",
+ "c.set_pass(6)\n",
+ "c.set_type(van)\n",
+ "\n",
+ "c.show() \n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "wheels: 18\n",
+ "passengers: 2\n",
+ "cargo capacity in cubic feet: 3200\n",
+ "wheels: 6\n",
+ "passengers: 3\n",
+ "cargo capacity in cubic feet: 1200\n",
+ "wheels: 4\n",
+ "passengers: 6\n",
+ "type: van\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.2, Page Number: 335"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " self.__i=self.__j=None\n",
+ " def set(self,a,b):\n",
+ " self.__i=a\n",
+ " self.__j=b\n",
+ " def show(self):\n",
+ " print self.__i,self.__j\n",
+ " \n",
+ "class derived(base):\n",
+ " def __init__(self,x):\n",
+ " self.__k=x\n",
+ " def showk(self):\n",
+ " print self.__k\n",
+ " \n",
+ "#Variable declaration\n",
+ "ob = derived(3)\n",
+ "\n",
+ "ob.set(1,2) #access member of base\n",
+ "ob.show() #access member of base\n",
+ "\n",
+ "ob.showk() #uses member of derived class\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 2\n",
+ "3\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.3, Page Number: 337"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " self.__i=self.__j=None #These act as protected members\n",
+ " def set(self,a,b):\n",
+ " self.__i=a\n",
+ " self.__j=b\n",
+ " def show(self):\n",
+ " print self.__i,self.__j\n",
+ " \n",
+ "class derived(base):\n",
+ " def __init__(self):\n",
+ " self.__k=None\n",
+ " def setk(self):\n",
+ " self.__k=self._base__i*self._base__j #accessing private variables in derived class\n",
+ " def showk(self):\n",
+ " print self.__k\n",
+ " \n",
+ "#Variable declaration\n",
+ "ob = derived()\n",
+ "\n",
+ "ob.set(2,3) #OK, known to be derived\n",
+ "ob.show() #OK, known to be derived\n",
+ "\n",
+ "ob.setk()\n",
+ "ob.showk() #uses member of derived class\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2 3\n",
+ "6\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.4, Page Number: 338"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " self.__i=None\n",
+ " self.__j=None\n",
+ " def set(self,a,b):\n",
+ " self.__i=a\n",
+ " self.__j=b\n",
+ " def show(self):\n",
+ " print self.__i,self.__j\n",
+ " \n",
+ "class derived1(base):\n",
+ " def __init__(self):\n",
+ " self.__k=None\n",
+ " def setk(self):\n",
+ " self.__k=self._base__i*self._base__j\n",
+ " def showk(self):\n",
+ " print self.__k\n",
+ "\n",
+ "class derived2(derived1):\n",
+ " def __init__(self):\n",
+ " self.__m=None\n",
+ " def setm(self):\n",
+ " self.__m=self._base__i-self._base__j\n",
+ " def showm(self):\n",
+ " print self.__m\n",
+ " \n",
+ " \n",
+ "#Variable declaration\n",
+ "ob1 = derived1()\n",
+ "ob2 = derived2()\n",
+ "\n",
+ "ob1.set(2,3) #access member of base\n",
+ "ob1.show() #access member of base\n",
+ "ob1.setk() #uses member of derived1 class\n",
+ "ob1.showk() #uses member of derived1 class\n",
+ "\n",
+ "ob2.set(3,4) #access member of base\n",
+ "ob2.show() #access member of base\n",
+ "ob2.setk() #access member of derived1 class\n",
+ "ob2.setm() #access member of derived2 class\n",
+ "ob2.showk() #uses member of derived1 class\n",
+ "ob2.showm() #uses member of derived1 class\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2 3\n",
+ "6\n",
+ "3 4\n",
+ "12\n",
+ "-1\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.5, Page Number: 341"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " self.__i=None\n",
+ " self._j=None\n",
+ " self.k=None\n",
+ " def seti(self,a):\n",
+ " self.__i=a\n",
+ " def geti(self):\n",
+ " return i\n",
+ " \n",
+ "class derived(base):\n",
+ " def setj(self,a):\n",
+ " self._j=a\n",
+ " def setk(self,a):\n",
+ " self.k=a\n",
+ " def getj(self):\n",
+ " return self._j\n",
+ " def getk(self):\n",
+ " return self.k\n",
+ " \n",
+ "#Variable declaration \n",
+ "ob=derived()\n",
+ "\n",
+ "ob.setk(10)\n",
+ "print ob.getk(),\n",
+ "ob.setj(12)\n",
+ "print ob.getj()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 12\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.6, Page Number: 342"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class base1:\n",
+ " def __init__(self):\n",
+ " self.x=None\n",
+ " def showx(self):\n",
+ " print self.x\n",
+ " \n",
+ "class base2:\n",
+ " def __init__(self):\n",
+ " self.y=None\n",
+ " def showy(self):\n",
+ " print self.y\n",
+ " \n",
+ "class derived(base1,base2):\n",
+ " def set(self,i,j):\n",
+ " self.x=i\n",
+ " self.y=j\n",
+ " \n",
+ "#Variable declaration\n",
+ "ob = derived()\n",
+ "\n",
+ "ob.set(10,20) #provided by derived\n",
+ "ob.showx() #from base1\n",
+ "ob.showy() #from base2\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n",
+ "20\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.7, Page Number: 343"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " print \"Constructing base\"\n",
+ " def __del__(self):\n",
+ " print \"Destructing base\"\n",
+ "\n",
+ "class 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\n",
+ "ob=derived()\n",
+ "\n",
+ "#Does nothing but construct and destruct ob"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Constructing base\n",
+ "Constructing derived\n",
+ "Destructing derived\n",
+ "Destructing base\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.8, Page Number: 344"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " print \"Constructing base\"\n",
+ " def __del__(self):\n",
+ " print \"Destructing base\"\n",
+ "\n",
+ "class 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",
+ "\n",
+ "class 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\n",
+ "ob=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": [
+ "\n",
+ "class base1:\n",
+ " def __init__(self):\n",
+ " print \"Constructing base1\"\n",
+ " def __del__(self):\n",
+ " print \"Destructing base1\"\n",
+ " \n",
+ "class base2:\n",
+ " def __init__(self):\n",
+ " print \"Constructing base2\"\n",
+ " def __del__(self):\n",
+ " print \"Destructing base2\"\n",
+ " \n",
+ "class 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\n",
+ "ob = derived()\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Constructing base1\n",
+ "Constructing base2\n",
+ "Constructing derived\n",
+ "Destructing derived\n",
+ "Destructing base1\n",
+ "Destructing base2\n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.10, Page Number: 347"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class base:\n",
+ " def __init__(self,x):\n",
+ " self._i=x\n",
+ " print \"Constructing base\"\n",
+ " def __del__(self):\n",
+ " print \"Destructing base\"\n",
+ "\n",
+ "class 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\n",
+ "ob=derived(3,4)\n",
+ "\n",
+ "#Result\n",
+ "ob.show() #shows 4 3"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Constructing base\n",
+ "Constructing derived\n",
+ "Destructing derived\n",
+ "Destructing base\n",
+ "4 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.11, Page Number: 348"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class base1:\n",
+ " def __init__(self,x):\n",
+ " self._i=x\n",
+ " print \"Constructing base1\"\n",
+ " def __del__(self):\n",
+ " print \"Destructing base1\"\n",
+ " \n",
+ "class base2:\n",
+ " def __init__(self,x):\n",
+ " self._k=x\n",
+ " print \"Constructing base2\"\n",
+ " def __del__(self):\n",
+ " print \"Destructing base2\"\n",
+ " \n",
+ "class 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\n",
+ "ob = derived(3,4,5)\n",
+ "\n",
+ "#Result\n",
+ "ob.show()\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Constructing base1\n",
+ "Constructing base2\n",
+ "Constructing derived\n",
+ "Destructing derived\n",
+ "Destructing base1\n",
+ "Destructing base2\n",
+ "4 3 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 25
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.12, Page Number: 348"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class base1:\n",
+ " def __init__(self,x):\n",
+ " self._i=x\n",
+ " print \"Constructing base1\"\n",
+ " def __del__(self):\n",
+ " print \"Destructing base1\"\n",
+ " \n",
+ "class base2:\n",
+ " def __init__(self,x):\n",
+ " self._k=x\n",
+ " print \"Constructing base2\"\n",
+ " def __del__(self):\n",
+ " print \"Destructing base2\"\n",
+ " \n",
+ "class 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\n",
+ "ob = derived(3,4)\n",
+ "\n",
+ "#Result\n",
+ "ob.show()\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Constructing base1\n",
+ "Constructing base2\n",
+ "Constructing derived\n",
+ "Destructing derived\n",
+ "Destructing base1\n",
+ "Destructing base2\n",
+ "3 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 26
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.13, Page Number: 351"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class 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",
+ "\n",
+ "class derived(base):\n",
+ " def __init__(self):\n",
+ " self.a=None\n",
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "ob=derived()\n",
+ "\n",
+ "ob._base__i=10 #Accessing private members of base class\n",
+ "ob.j=20 #legal because j and k are public variable in base\n",
+ "ob.k=30\n",
+ "\n",
+ "ob.a=40 #legal because a is public in derived class\n",
+ "ob.seti(10)\n",
+ "\n",
+ "#Result\n",
+ "print 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": [
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " self.i=None\n",
+ "\n",
+ "#derived1 inherits base\n",
+ "class derived1(base):\n",
+ " def __init__(self):\n",
+ " self.__j=None\n",
+ " \n",
+ "#derived2 inherits base\n",
+ "class derived2(base):\n",
+ " def __init__(self):\n",
+ " self.__k=None\n",
+ "\n",
+ "#derived3 inherits from both derived1 and derived2\n",
+ "class derived3(derived1,derived2):\n",
+ " def __init__(self):\n",
+ " self.__sum=None\n",
+ " \n",
+ "#Variable declaration\n",
+ "ob=derived3()\n",
+ "\n",
+ "ob.i=10\n",
+ "ob.j=20\n",
+ "ob.k=30\n",
+ "\n",
+ "ob.sum=ob.i+ob.j+ob.k\n",
+ "\n",
+ "#Result\n",
+ "print 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": [
+ "\n",
+ "class base:\n",
+ " def __init__(self):\n",
+ " self.i=None\n",
+ "\n",
+ "#derived1 inherits base\n",
+ "class derived1(base):\n",
+ " def __init__(self):\n",
+ " self.__j=None\n",
+ " \n",
+ "#derived2 inherits base\n",
+ "class derived2(base):\n",
+ " def __init__(self):\n",
+ " self.__k=None\n",
+ "\n",
+ "#derived3 inherits from both derived1 and derived2\n",
+ "class derived3(derived1,derived2):\n",
+ " def __init__(self):\n",
+ " self.__sum=None\n",
+ " \n",
+ "#Variable declaration\n",
+ "ob=derived3()\n",
+ "\n",
+ "ob.i=10\n",
+ "ob.j=20\n",
+ "ob.k=30\n",
+ "\n",
+ "ob.sum=ob.i+ob.j+ob.k\n",
+ "\n",
+ "#Result\n",
+ "print 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.ipynb b/C++_from_the_Ground/Chapter_15.ipynb
new file mode 100644
index 00000000..211b1c7d
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_15.ipynb
@@ -0,0 +1,427 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:1198072630a182533bf651767f275df9ee5758e0d781322254bb408a8b4cffaa"
+ },
+ "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": [
+ "\n",
+ "\n",
+ "\n",
+ "class 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",
+ " \n",
+ "class 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\n",
+ "p=[B_class()] #acts as a pointer to B_class type\n",
+ "B_ob=B_class()\n",
+ "\n",
+ "dp=[D_class()] #acts as a pointer to D_class type\n",
+ "D_ob=D_class()\n",
+ "\n",
+ "p[0]=B_ob #assigning p to object of base\n",
+ "\n",
+ "\n",
+ "#Access B_class via pointer\n",
+ "p[0].put_author(\"Tom Clancy\")\n",
+ "\n",
+ "#Access D_class via base pointer\n",
+ "p[0]=D_ob\n",
+ "p[0].put_author(\"William Shakespeare\")\n",
+ "\n",
+ "#Show that each author went into proper object\n",
+ "B_ob.show_author()\n",
+ "D_ob.show_author()\n",
+ "print \"\\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\n",
+ "dp[0]=D_ob\n",
+ "dp[0].put_title(\"The Tempest\")\n",
+ "p[0].show_author()\n",
+ "dp[0].show_title()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Tom Clancy\n",
+ "William Shakespeare\n",
+ "\n",
+ "\n",
+ "William Shakespeare\n",
+ "Title: The Tempest\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 15.2, Page Number: 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class base:\n",
+ " def who(self): #virtual function\n",
+ " print \"Base\"\n",
+ "\n",
+ "class first_d(base):\n",
+ " def who(self): #redifine who() relative to first_d\n",
+ " print \"First derivation\"\n",
+ " \n",
+ "class second_d(base):\n",
+ " def who(self): #redifine who() relative to second_d\n",
+ " print \"Second derivation\"\n",
+ " \n",
+ " \n",
+ "#Variable declaration\n",
+ "base_obj=base()\n",
+ "p=[base()]\n",
+ "first_obj=first_d()\n",
+ "second_obj=second_d()\n",
+ "\n",
+ "p[0]=base_obj\n",
+ "p[0].who() #access base's who\n",
+ "\n",
+ "p[0]=first_obj\n",
+ "p[0].who() #access first_d's who\n",
+ "\n",
+ "p[0]=second_obj\n",
+ "p[0].who() #access second_d's who\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Base\n",
+ "First derivation\n",
+ "Second derivation\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 15.3, Page Number: 363"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class base:\n",
+ " def who(self): #virtual function\n",
+ " print \"Base\"\n",
+ "\n",
+ "class first_d(base):\n",
+ " def who(self): #redifine who() relative to first_d\n",
+ " print \"First derivation\"\n",
+ " \n",
+ "class second_d(base):\n",
+ " #who not defined\n",
+ " pass\n",
+ " \n",
+ " \n",
+ "#Variable declaration\n",
+ "base_obj=base()\n",
+ "p=[base()]\n",
+ "first_obj=first_d()\n",
+ "second_obj=second_d()\n",
+ "\n",
+ "p[0]=base_obj\n",
+ "p[0].who() #access base's who\n",
+ "\n",
+ "p[0]=first_obj\n",
+ "p[0].who() #access first_d's who\n",
+ "\n",
+ "p[0]=second_obj\n",
+ "p[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\n",
+ "First derivation\n",
+ "Base\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 15.4, Page Number: 364"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class base:\n",
+ " def who(self): #virtual function\n",
+ " print \"Base\"\n",
+ "\n",
+ "class 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\n",
+ "class second_d(first_d):\n",
+ " #who not defined\n",
+ " pass\n",
+ " \n",
+ " \n",
+ "#Variable declaration\n",
+ "base_obj=base()\n",
+ "p=[base()]\n",
+ "first_obj=first_d()\n",
+ "second_obj=second_d()\n",
+ "\n",
+ "p[0]=base_obj\n",
+ "p[0].who() #access base's who\n",
+ "\n",
+ "p[0]=first_obj\n",
+ "p[0].who() #access first_d's who\n",
+ "\n",
+ "p[0]=second_obj\n",
+ "p[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\n",
+ "First derivation\n",
+ "First derivation\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 15.5, Page Number: 366"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class 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",
+ " \n",
+ "class 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",
+ " \n",
+ "class 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\n",
+ "p=[figure()] #pointer to base type\n",
+ "t=triangle() #objects of derived type\n",
+ "r=rectangle()\n",
+ "\n",
+ "p[0]=t\n",
+ "p[0].set_dim(10.0,5.0)\n",
+ "p[0].show_area()\n",
+ "\n",
+ "p[0]=r\n",
+ "p[0].set_dim(10.0,5.0)\n",
+ "p[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 .\n",
+ "Rectangle 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": [
+ "\n",
+ "class 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",
+ " \n",
+ "class 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",
+ " \n",
+ "class 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",
+ "class 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\n",
+ "p=[figure()] #pointer to base type\n",
+ "t=triangle() #objects of derived type\n",
+ "r=rectangle()\n",
+ "c=circle()\n",
+ "\n",
+ "p[0]=t\n",
+ "p[0].set_dim(10.0,5.0)\n",
+ "p[0].show_area()\n",
+ "\n",
+ "p[0]=r\n",
+ "p[0].set_dim(10.0,5.0)\n",
+ "p[0].show_area()\n",
+ "\n",
+ "p[0]=c\n",
+ "p[0].set_dim(9.0)\n",
+ "p[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 .\n",
+ "Rectangle with dimensions 10.0 x 5.0 has an area of 50.0 .\n",
+ "Circle 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.ipynb b/C++_from_the_Ground/Chapter_16.ipynb
new file mode 100644
index 00000000..98e3dc8a
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_16.ipynb
@@ -0,0 +1,674 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:60d976a21b55caeaa47bb2c8586e986eed54234a29f15a816e7270f74ad2660e"
+ },
+ "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": [
+ "\n",
+ "def swapargs(a,b):\n",
+ " temp=a[0]\n",
+ " a[0]=b[0]\n",
+ " b[0]=temp\n",
+ "\n",
+ "#Variable declaration\n",
+ "i=[10]\n",
+ "j=[20]\n",
+ "x=[10.1]\n",
+ "y=[23.3]\n",
+ "a=['x']\n",
+ "b=['z']\n",
+ "\n",
+ "print \"Original i, j: \",i[0],j[0]\n",
+ "print \"Original x,y: \",x[0],y[0]\n",
+ "print \"Original a,b: \",a[0],b[0]\n",
+ "\n",
+ "swapargs(i,j) #swap integers\n",
+ "swapargs(x,y) #swap floats\n",
+ "swapargs(a,b) #swap chars\n",
+ "\n",
+ "#Result\n",
+ "print \"Swapped i, j: \",i[0],j[0]\n",
+ "print \"Swapped x,y: \",x[0],y[0]\n",
+ "print \"Swapped a,b: \",a[0],b[0]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Original i, j: 10 20\n",
+ "Original x,y: 10.1 23.3\n",
+ "Original a,b: x z\n",
+ "Swapped i, j: 20 10\n",
+ "Swapped x,y: 23.3 10.1\n",
+ "Swapped a,b: z x\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 16.2, Page Number: 378"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def myfunc(x,y):\n",
+ " print x,y\n",
+ " \n",
+ "myfunc(10,\"hi\")\n",
+ "myfunc(0.23,10L)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 hi\n",
+ "0.23 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 16.3, Page Number: 379"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def swapargs(a,b):\n",
+ " if isinstance(a[0],int): #integer version\n",
+ " temp=a[0]\n",
+ " a[0]=b[0]\n",
+ " b[0]=temp\n",
+ " print \"Inside swapargs int specialization.\"\n",
+ " else: #generic version\n",
+ " temp=a[0]\n",
+ " a[0]=b[0]\n",
+ " b[0]=temp\n",
+ " print \"Inside template swapargs.\"\n",
+ "\n",
+ "#Variable declaration\n",
+ "i=[10]\n",
+ "j=[20]\n",
+ "x=[10.1]\n",
+ "y=[23.3]\n",
+ "a=['x']\n",
+ "b=['z']\n",
+ "\n",
+ "print \"Original i, j: \",i[0],j[0]\n",
+ "print \"Original x,y: \",x[0],y[0]\n",
+ "print \"Original a,b: \",a[0],b[0]\n",
+ "\n",
+ "swapargs(i,j) #calls explicitly overloaded swapargs()\n",
+ "swapargs(x,y) #calls generic swapargs()\n",
+ "swapargs(a,b) #calls generic swapargs()\n",
+ "\n",
+ "#Result\n",
+ "print \"Swapped i, j: \",i[0],j[0]\n",
+ "print \"Swapped x,y: \",x[0],y[0]\n",
+ "print \"Swapped a,b: \",a[0],b[0]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Original i, j: 10 20\n",
+ "Original x,y: 10.1 23.3\n",
+ "Original a,b: x z\n",
+ "Inside swapargs int specialization.\n",
+ "Inside template swapargs.\n",
+ "Inside template swapargs.\n",
+ "Swapped i, j: 20 10\n",
+ "Swapped x,y: 23.3 10.1\n",
+ "Swapped a,b: z x\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 16.4, Page Number: 381"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def f(a,b=None):\n",
+ " if(b==None): #First version of f()\n",
+ " print \"Inside f(X a)\"\n",
+ " else: #Second version of f()\n",
+ " print \"Inside f(X a, Y b)\"\n",
+ " \n",
+ "f(10) #calls f(X)\n",
+ "f(10,20) #calls f(X,Y)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Inside f(X a)\n",
+ "Inside 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": [
+ " \n",
+ "def repeat(data,times):\n",
+ " while times:\n",
+ " print data\n",
+ " times-=1\n",
+ " \n",
+ "repeat(\"This is a test\",3)\n",
+ "repeat(100,5)\n",
+ "repeat(99.0/2, 4)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "This is a test\n",
+ "This is a test\n",
+ "This is a test\n",
+ "100\n",
+ "100\n",
+ "100\n",
+ "100\n",
+ "100\n",
+ "49.5\n",
+ "49.5\n",
+ "49.5\n",
+ "49.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 16.6, Page Number: 383"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def myabs(val):\n",
+ " if val<0:\n",
+ " return -val\n",
+ " else:\n",
+ " return val\n",
+ "\n",
+ "#Result\n",
+ "print myabs(-10)\n",
+ "print myabs(-10.0)\n",
+ "print myabs(-10L)\n",
+ "print myabs(-10.0)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n",
+ "10.0\n",
+ "10\n",
+ "10.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 16.7, Page Number: 385"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def SIZE():\n",
+ " return 100\n",
+ "class queue:\n",
+ " def __init__(self):\n",
+ " self.q=[]\n",
+ " self.sloc=self.rloc=0\n",
+ " #Put an object into the queue\n",
+ " def qput(self,i):\n",
+ " if self.sloc==SIZE():\n",
+ " print \"Queue is full.\"\n",
+ " return\n",
+ " self.sloc+=1\n",
+ " self.q.append(i)\n",
+ " #Get an object from the queue.\n",
+ " def qget(self):\n",
+ " if self.rloc==self.sloc:\n",
+ " print \"Queue Underflow.\"\n",
+ " return\n",
+ " a=self.rloc\n",
+ " self.rloc+=1\n",
+ " return self.q[a]\n",
+ " \n",
+ "#Create two integer queues\n",
+ "a=queue()\n",
+ "b=queue()\n",
+ "a.qput(10)\n",
+ "b.qput(19)\n",
+ "a.qput(20)\n",
+ "b.qput(1)\n",
+ "\n",
+ "print a.qget(),\n",
+ "print a.qget(),\n",
+ "print b.qget(),\n",
+ "print b.qget()\n",
+ "\n",
+ "#Create two double queues\n",
+ "c=queue()\n",
+ "d=queue()\n",
+ "c.qput(10.12)\n",
+ "d.qput(19.99)\n",
+ "c.qput(-20.0)\n",
+ "d.qput(0.986)\n",
+ "\n",
+ "print c.qget(),\n",
+ "print c.qget(),\n",
+ "print d.qget(),\n",
+ "print d.qget()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 20 19 1\n",
+ "10.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": [
+ "\n",
+ "class myclass:\n",
+ " def __init__(self,a,b):\n",
+ " self.__i=a\n",
+ " self.__j=b\n",
+ " def show(self):\n",
+ " print self.__i,self.__j\n",
+ "\n",
+ "ob1=myclass(10,0.23)\n",
+ "ob2=myclass('X',\"This is a test\")\n",
+ "\n",
+ "#Result\n",
+ "ob1.show() #Show int,double\n",
+ "ob2.show() #Show char.char*\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 0.23\n",
+ "X 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": [
+ " \n",
+ "def SIZE():\n",
+ " return 10\n",
+ "\n",
+ "class atype:\n",
+ " def __init__(self):\n",
+ " self.a=[]\n",
+ " for i in range(SIZE()):\n",
+ " self.a.append(i)\n",
+ " #Implementing the [] overloading\n",
+ " def op1(self,i,j):\n",
+ " if (i<0 or i>=SIZE()):\n",
+ " print \"\\nIndex value of \",\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " return \n",
+ " self.a[i]=j\n",
+ " def op2(self,i):\n",
+ " if (i<0 or i>SIZE()-1):\n",
+ " print \"\\nIndex value of \",\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " return\n",
+ " return self.a[i]\n",
+ " \n",
+ "#Variable declaration\n",
+ "intob=atype()\n",
+ "doubleob=atype()\n",
+ " \n",
+ "print \"Integer array: \",\n",
+ "for i in range(SIZE()):\n",
+ " intob.op1(i,i)\n",
+ "for i in range(SIZE()):\n",
+ " print intob.op2(i),\n",
+ " \n",
+ "print \"\"\n",
+ "\n",
+ "print \"Double array: \",\n",
+ "for i in range(SIZE()):\n",
+ " doubleob.op1(i,float(i)/3)\n",
+ "for i in range(SIZE()):\n",
+ " print doubleob.op2(i),\n",
+ " \n",
+ "intob.op1(12,100)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Integer array: 0 1 2 3 4 5 6 7 8 9 \n",
+ "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \n",
+ "Index value of 12 is out-of-bounds.\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 16.10, Page Number: 389"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class atype:\n",
+ " def __init__(self,size):\n",
+ " self.a=[]\n",
+ " self.size=size\n",
+ " for i in range(size):\n",
+ " self.a.append(i)\n",
+ " #Implementing the [] overloading\n",
+ " def op1(self,i,j):\n",
+ " if (i<0 or i>self.size-1):\n",
+ " print \"\\nIndex value of \",\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " return\n",
+ " self.a[i]=j\n",
+ " def op2(self,i):\n",
+ " if (i<0 or i>self.size-1):\n",
+ " print \"\\nIndex value of \",\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " return\n",
+ " return self.a[i]\n",
+ " \n",
+ "#Variable declaration\n",
+ "intob=atype(10)\n",
+ "doubleob=atype(15)\n",
+ " \n",
+ "print \"Integer array: \",\n",
+ "for i in range(10):\n",
+ " intob.op1(i,i)\n",
+ "for i in range(10):\n",
+ " print intob.op2(i),\n",
+ " \n",
+ "print \"\"\n",
+ "\n",
+ "print \"Double array: \",\n",
+ "for i in range(15):\n",
+ " doubleob.op1(i,float(i)/3)\n",
+ "for i in range(15):\n",
+ " print doubleob.op2(i),\n",
+ " \n",
+ "intob.op1(12,100) #generates runtime error\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Integer array: 0 1 2 3 4 5 6 7 8 9 \n",
+ "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 3.33333333333 3.66666666667 4.0 4.33333333333 4.66666666667 \n",
+ "Index value of 12 is out-of-bounds.\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 16.11, Page Number: 391"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class atype:\n",
+ " def __init__(self,size=10):\n",
+ " self.a=[]\n",
+ " for i in range(size):\n",
+ " self.a.append(i)\n",
+ " #Implementing the [] overloading\n",
+ " def op1(self,i,j):\n",
+ " if (i<0 and i>SIZE()-1):\n",
+ " print \"Index value of \"\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " exit(1)\n",
+ " self.a[i]=j\n",
+ " def op2(self,i):\n",
+ " if (i<0 and i>SIZE()-1):\n",
+ " print \"Index value of \"\n",
+ " print i,\" is out-of-bounds.\"\n",
+ " exit()\n",
+ " return self.a[i]\n",
+ " \n",
+ "#Variable declaration\n",
+ "intob=atype(100)\n",
+ "doubleob=atype()\n",
+ "defarray=atype()\n",
+ " \n",
+ "print \"Integer array: \",\n",
+ "for i in range(100):\n",
+ " intob.op1(i,i)\n",
+ "for i in range(100):\n",
+ " print intob.op2(i),\n",
+ " \n",
+ "print \"\"\n",
+ "\n",
+ "print \"Double array: \",\n",
+ "for i in range(10):\n",
+ " doubleob.op1(i,float(i)/3)\n",
+ "for i in range(10):\n",
+ " print doubleob.op2(i),\n",
+ " \n",
+ "print \"\"\n",
+ "\n",
+ "print \"Defarray array: \",\n",
+ "for i in range(10):\n",
+ " doubleob.op1(i,i)\n",
+ "for i in range(10):\n",
+ " print doubleob.op2(i),\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Integer array: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 \n",
+ "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \n",
+ "Defarray array: 0 1 2 3 4 5 6 7 8 9\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 16.12, Page Number: 393"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class 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",
+ " \n",
+ "d=myclass(10.1)\n",
+ "print \"double: \",d.getx(),\"\\n\"\n",
+ "\n",
+ "i=myclass(5)\n",
+ "print \"int: \",i.getx()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Inside generic myclass\n",
+ "double: 10.1 \n",
+ "\n",
+ "Inside myclassspecialization\n",
+ "int: 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.ipynb b/C++_from_the_Ground/Chapter_17.ipynb
new file mode 100644
index 00000000..c50f48aa
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_17.ipynb
@@ -0,0 +1,730 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:636fe844c88ef46d09064f199c6b3a4c62e262611a41cc6f126f08459b94982e"
+ },
+ "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": [
+ "\n",
+ "print \"start\"\n",
+ "\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",
+ " print \"Caught an exception -- value is:\",\n",
+ " print i\n",
+ "\n",
+ "print \"end\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "start\n",
+ "Inside try block\n",
+ "Caught an exception -- value is: 99\n",
+ "end\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 17.2, Page Number: 399"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "\n",
+ "\n",
+ "def 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",
+ " \n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "start\n",
+ "Inside try block\n",
+ "Abnormal program termination\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 17.3, Page Number: 400"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def Xtest(test):\n",
+ " print \"Inside Xtest, test is: \",test\n",
+ " if(test):\n",
+ " raise Exception(test)\n",
+ " \n",
+ "print \"start\"\n",
+ "\n",
+ "try: #start a try block\n",
+ " print \"Inside try block\"\n",
+ " Xtest(0)\n",
+ " Xtest(1)\n",
+ " Xtest(2)\n",
+ "except Exception,i: #catch an error\n",
+ " print \"Caught an exception -- value is:\",\n",
+ " print i\n",
+ "\n",
+ "print \"end\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "start\n",
+ "Inside try block\n",
+ "Inside Xtest, test is: 0\n",
+ "Inside Xtest, test is: 1\n",
+ "Caught an exception -- value is: 1\n",
+ "end\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 17.4, Page Number: 401"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def Xhandler(test):\n",
+ " try:\n",
+ " if(test):\n",
+ " raise Exception(test)\n",
+ " except Exception,i:\n",
+ " print \"Caught One! Ex #:\",i\n",
+ " \n",
+ "print \"start\"\n",
+ "\n",
+ "Xhandler(1)\n",
+ "Xhandler(2)\n",
+ "Xhandler(0)\n",
+ "Xhandler(3)\n",
+ "\n",
+ "print \"end\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "start\n",
+ "Caught One! Ex #: 1\n",
+ "Caught One! Ex #: 2\n",
+ "Caught One! Ex #: 3\n",
+ "end\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 17.5, Page Number: 401"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class MyException:\n",
+ " def __init__(self,s):\n",
+ " self.str_what=s\n",
+ " \n",
+ "#Variable declaration\n",
+ "a=None \n",
+ "b=None\n",
+ "\n",
+ "try:\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\n",
+ "except 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:\n",
+ "Cannot divide by zero!\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 17.6, Page Number: 403"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class MyException:\n",
+ " def __init__(self,s):\n",
+ " self.x=s\n",
+ " \n",
+ "def 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",
+ " \n",
+ "print \"start\"\n",
+ "\n",
+ "Xhandler(1)\n",
+ "Xhandler(2)\n",
+ "Xhandler(0)\n",
+ "Xhandler(3)\n",
+ "\n",
+ "print \"end\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "start\n",
+ "Caught One! Ex #: 1\n",
+ "Caught One! Ex #: 2\n",
+ "Caught a string: Value is zero\n",
+ "Caught One! Ex #: 3\n",
+ "end\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 17.7, Page Number: 404"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class B:\n",
+ " pass\n",
+ "\n",
+ "class D(B):\n",
+ " pass\n",
+ "\n",
+ "derived=D()\n",
+ "\n",
+ "try:\n",
+ " raise B()\n",
+ "except B as b:\n",
+ " print \"Caught a base class.\"\n",
+ "except 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": [
+ "\n",
+ "def 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",
+ "\n",
+ "print \"start\"\n",
+ "\n",
+ "Xhandler(0)\n",
+ "Xhandler(1)\n",
+ "Xhandler(2)\n",
+ "\n",
+ "print \"end\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "start\n",
+ "Caught One!\n",
+ "Caught One!\n",
+ "Caught One!\n",
+ "end\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 17.9, Page Number: 405"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class MyException:\n",
+ " def __init__(self,s):\n",
+ " self.x=s\n",
+ " \n",
+ "def 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",
+ "\n",
+ "print \"start\"\n",
+ "\n",
+ "Xhandler(0)\n",
+ "Xhandler(1)\n",
+ "Xhandler(2)\n",
+ "\n",
+ "print \"end\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "start\n",
+ "Caught 0\n",
+ "Caught One!\n",
+ "Caught One!\n",
+ "end\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 17.10, Page Number: 407"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class MyException:\n",
+ " def __init__(self,s):\n",
+ " self.x=s\n",
+ " \n",
+ "#This function can only throw ints, chars and doubles\n",
+ "def 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",
+ "\n",
+ "print \"start\"\n",
+ "try:\n",
+ " Xhandler(0)\n",
+ "except 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",
+ "\n",
+ "print \"end\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "start\n",
+ "Caught int\n",
+ "end\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 17.11, Page Number: 408"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def 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",
+ " \n",
+ "print \"start\"\n",
+ "try:\n",
+ " Xhandler()\n",
+ "except Exception,c:\n",
+ " print \"Caught char * inside main\"\n",
+ " \n",
+ "print \"end\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "start\n",
+ "Caugh char * inside Xhandler\n",
+ "Caught char * inside main\n",
+ "end\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 17.12, Page Number: 410"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "#Variable declaration\n",
+ "p=[]\n",
+ "\n",
+ "try:\n",
+ " for i in range(32):\n",
+ " p.append(c_int())\n",
+ "except MemoryError,m:\n",
+ " print \"Allocation failure.\"\n",
+ " \n",
+ "for i in range(32):\n",
+ " p[i]=i\n",
+ " \n",
+ "for 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": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "#Variable declaration\n",
+ "p=[]\n",
+ "\n",
+ "\n",
+ "for i in range(32):\n",
+ " p.append(c_int()) \n",
+ "if not(p):\n",
+ " print \"Allocation failure.\"\n",
+ " \n",
+ "for i in range(32):\n",
+ " p[i]=i\n",
+ " \n",
+ "for 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": [
+ "\n",
+ "class 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",
+ " \n",
+ "p1=[]*3\n",
+ "p2=[]\n",
+ " \n",
+ "try:\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\n",
+ "except MemoryError:\n",
+ " print \"Allocation error\"\n",
+ " \n",
+ "p1[2].show()\n",
+ "p2[0].show()\n",
+ "\n",
+ "\n",
+ "for i in xrange(2,-1,-1):\n",
+ " del p1[i] #delete array\n",
+ "print \"Deleting array of thee_d objects.\"\n",
+ "\n",
+ "del p2[0] #delete object\n",
+ "print \"Deleting three_d object.\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Allocating array of three_d objects.\n",
+ "Constructing 0, 0, 0\n",
+ "Constructing 0, 0, 0\n",
+ "Constructing 0, 0, 0\n",
+ "Allocating three_d object.\n",
+ "Constructing 5 , 6 , 7\n",
+ "0 , 0 , 0\n",
+ "5 , 6 , 7\n",
+ "Destructing\n",
+ "Destructing\n",
+ "Destructing\n",
+ "Deleting array of thee_d objects.\n",
+ "Destructing\n",
+ "Deleting 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.ipynb b/C++_from_the_Ground/Chapter_18.ipynb
new file mode 100644
index 00000000..df4cabc2
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_18.ipynb
@@ -0,0 +1,881 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:85eae8322c208f1f26e859bb0f4dac6f86b35c3ac105ed98ac2d0fbf05287f0e"
+ },
+ "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": [
+ "\n",
+ "class 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\n",
+ "a=three_d(1,2,3)\n",
+ "b=three_d(3,4,5)\n",
+ "c=three_d(5,6,7)\n",
+ "\n",
+ "#Result\n",
+ "print 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": [
+ "\n",
+ "class 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 \n",
+ "def repr():\n",
+ " return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=three_d(1,2,3)\n",
+ "b=three_d(3,4,5)\n",
+ "c=three_d(5,6,7)\n",
+ "\n",
+ "print 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": [
+ "\n",
+ "class 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\n",
+ "a=three_d(1,2,3)\n",
+ "\n",
+ "print a\n",
+ "\n",
+ "#User input\n",
+ "print \"Enter X,Y,Z values:\"\n",
+ "a=three_d(4,5,6) \n",
+ "print a"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1, 2, 3\n",
+ "Enter X,Y,Z values:\n",
+ "4, 5, 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 18.4, Page Number: 428"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "print '{0:+d}'.format(123), #for ios::showpos\n",
+ "if(123.23>0):\n",
+ " i='{0:e}'.format(123.23) #for ios::scientific \n",
+ " i='+'+i\n",
+ " print i\n",
+ "else :\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": [
+ "\n",
+ "import string\n",
+ "\n",
+ "print '{0:+d}'.format(123), #for ios::showpos\n",
+ "if(123.23>0):\n",
+ " i='{0:e}'.format(123.23) #for ios::scientific \n",
+ " i='+'+i\n",
+ " print i\n",
+ "else :\n",
+ " print '{0:e}'.format(123.23)\n",
+ "\n",
+ " \n",
+ "print '{:10.2f}'.format(123.23), #2 digits left of decimal\n",
+ "if(123.23>0):\n",
+ " i='{0:.2e}'.format(123.23) #for ios::scientific \n",
+ " i='+'+i\n",
+ " print i\n",
+ "else :\n",
+ " print '{0:.2e}'.format(123.23)\n",
+ " \n",
+ " \n",
+ "print '{:#>10}'.format(str(123)), #for ios::fill\n",
+ "if(123.23>0): \n",
+ " i='{0:.2e}'.format(123.23) #for ios::scientific \n",
+ " i='+'+i\n",
+ " print i\n",
+ "else :\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": [
+ "\n",
+ "print '{0:.0e}'.format(1000.243) #for setprecision\n",
+ "print '{:>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": [
+ "\n",
+ "print '{0:+d}'.format(123), #for ios::showpos\n",
+ "if(123.23>0):\n",
+ " i='{0:e}'.format(123.23) #for ios::scientific \n",
+ " i='+'+i\n",
+ " print i\n",
+ "else :\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": [
+ "\n",
+ "#User input\n",
+ "s=\" Hello\"\n",
+ "\n",
+ "#Result\n",
+ "print 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": [
+ "\n",
+ "def setup(s):\n",
+ " return '{:$<10}'.format(str(s))\n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "print 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": [
+ "\n",
+ "def prompt():\n",
+ " print \"Enter number using hex format:\"\n",
+ " hex=0x46\n",
+ " return hex\n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "i=prompt()\n",
+ "print i\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 18.11, Page Number: 438"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "out=open(\"test\",'w')\n",
+ "\n",
+ "\n",
+ "if(not(out)):\n",
+ " print \"Cannot open file.\"\n",
+ "else:\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": [
+ "\n",
+ " \n",
+ "In=open(\"test\",'r')\n",
+ " \n",
+ "if(not(In)):\n",
+ " print \"Cannot open file.\"\n",
+ "else:\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",
+ "\n",
+ "This 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": [
+ "\n",
+ "\n",
+ "import sys\n",
+ " \n",
+ "if not(len(sys.argv)==2):\n",
+ " print \"Usage: PR \\n\"\n",
+ "else:\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": [
+ "\n",
+ "import sys\n",
+ "\n",
+ "p=\"hello there\"\n",
+ "\n",
+ "out=open(\"test\",'w')\n",
+ "\n",
+ "#In case file cannot open\n",
+ "if(not(out)):\n",
+ " print \"Cannot open file.\"\n",
+ "else:\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": [
+ "\n",
+ "import sys\n",
+ "\n",
+ "n=[1,2,3,4,5]\n",
+ "\n",
+ "#Open a file 'test'\n",
+ "out=open(\"test\",'w')\n",
+ "\n",
+ "#In case file cannot open\n",
+ "if(not(out)):\n",
+ " print \"Cannot open file.\"\n",
+ "else:\n",
+ " #Write to file\n",
+ " for i in range(5):\n",
+ " out.write(chr(n[i]))\n",
+ " out.close()\n",
+ " \n",
+ "for i in range(5): #clear array\n",
+ " n[i]=0\n",
+ " \n",
+ "#Open the file\n",
+ "In=open(\"test\",'r')\n",
+ "\n",
+ "#In case file cannot open\n",
+ "if(not(In)):\n",
+ " print \"Cannot open file.\"\n",
+ "else:\n",
+ " #Read file\n",
+ " for i in range(5):\n",
+ " n[i]=ord(In.read(1))\n",
+ "\n",
+ "#Result, shows value from file\n",
+ "for i in range(5):\n",
+ " print n[i],\n",
+ " \n",
+ "In.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