summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_8(1).ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C++_from_the_Ground/Chapter_8(1).ipynb')
-rw-r--r--C++_from_the_Ground/Chapter_8(1).ipynb327
1 files changed, 327 insertions, 0 deletions
diff --git a/C++_from_the_Ground/Chapter_8(1).ipynb b/C++_from_the_Ground/Chapter_8(1).ipynb
new file mode 100644
index 00000000..da5f88d2
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_8(1).ipynb
@@ -0,0 +1,327 @@
+{
+ "metadata": {
+ "name": "Chapter 8"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h1>Chapter 8: Functions,Part Two: References, Overloading, and Default Arguments<h1>"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.1, Page Number: 158<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Example for call by value'''\n\ndef sqr_it(x):\n x=x*x\n return x\n\n#Variable decleration\nt=10\n\n#Result; function calling\nprint sqr_it(t),' ',t\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "100 10\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.2, Page Number: 159<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Program to swap two values; implementing call by pointers'''\n\ndef swap(x,y):\n temp=x\n x=y\n y=temp\n return x, y\n\n#Variable decleration\ni=10\nj=20 \n\n#Initial values\nprint \"Initial values of i and j: \",\nprint i,' ',j\n\n#Calling function to swap\ni, j=swap(i,j)\n\n#Result\nprint \"Swapped values of i and j: \",\nprint i,' ',j",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Initial values of i and j: 10 20\nSwapped values of i and j: 20 10\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.3, Page Number: 161<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Example to implement call by reference in python'''\n\ndef f(i):\n i=10\n return i #Returning the value since the function cannot access the variables in the calling scope.\n\n#Variable Decleration\nval=1\n\nprint \"Old value for val: \",val\nval=f(val) #Function call\n\n#Result\nprint \"New value for val: \",val\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Old value for val: 1\nNew value for val: 10\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.4, Page Number: 162<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Program to swap two values; implementing call by reference'''\n\ndef swap(i,j):\n temp=i[0]\n i[0]=j[0]\n j[0]=temp\n return i, j\n\n#Variable decleration\ni=[]\nj=[]\ni.append(10)\nj.append(20)\n\n#Initial values\nprint \"Initial values of i and j: \",\nprint i[0],' ',j[0]\n\n#Calling function to swap\ni, j=swap(i,j)\n\n#Result\nprint \"Swapped values of i and j: \",\nprint i[0],' ',j[0]",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Initial values of i and j: 10 20\nSwapped values of i and j: 20 10\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.5, Page Number: 164<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''An example program'''\n#Since python cannot return references, the value is changed using the variable itself\n\n#Variable Decleration\nval = 100.0\n\ndef f():\n global val\n return val\n\n#Result\nprint val\n\nnewval=f() #function call\nprint newval\n\nval=99.1 #change val's value\nprint f() #print val's new value\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "100.0\n100.0\n99.1\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.6, Page Number: 166<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Changing contents of a list'''\n#Since python cannot return references, the program has been modified.\n\n#Function definition\ndef change_it(i,n):\n global vals\n vals[i]=n\n\n#Variable Decleration\nvals=[1.1,2.2,3.3,4.4,5.5]\n\nprint \"Here are the original values: \",\nfor i in range(5):\n print vals[i],\nprint\n \n#Function call\nchange_it(1,5298.23)\nchange_it(3,-98.8)\n\n#Result\nprint \"Here are the changed values: \",\nfor i in range(5):\n print vals[i],",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Here are the original values: 1.1 2.2 3.3 4.4 5.5\nHere are the changed values: 1.1 5298.23 3.3 -98.8 5.5\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.7, Page Number: 167<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementing a simple safe array'''\n\n#Variable Decleration\nvals=[None]*10\nerror=-1\n\n#put values into the array\ndef put(i,n):\n global vals\n if i>=0 and i<10:\n vals[i]=n\n else:\n print \"Bounds Error!\"\n error=n\n\n#obtain a value from the array\ndef get(i):\n if i>=0 and i<10:\n return vals[i]\n else:\n print \"Bounds error!\"\n return -1\n \n#put values into the array\nput(0,10)\nput(1,20)\nput(9,30)\n\n#Result\nprint get(0),' ',get(1),' ',get(9),\n\n#now, intentionally generate an errer\nput(12,1)",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 20 30 Bounds Error!\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.8, Page Number: 169<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementing references in pyhton'''\n\n#Variable Decleration\ni=[]\nj=i #independent reference\n\nj.append(10) #Here i and j are just references to [10]\n\nprint j[0],\" \",i[0] \n\nk=121\ni[0]=k #copies k's value into j[0] \n\n#Result\nprint j[0]",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 10\n121\n"
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.9, Page Number: 170<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Function overloading in python'''\n#Overloading is not supported by python, hence it is implemented by default arguments.\n\ndef f(i,j=None):\n if j==None: \n if isinstance(i,int): #for 1st function\n print \"In f(int), i is \",i \n else: #for 3rd function\n print \"In f(double), k is \",i\n else: #for 2nd arguments\n print \"In f(int,int), i is \",i,\", j is \",j\n \n#calling function\nf(10)\nf(10,20)\nf(12.23)\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "In f(int), i is 10\nIn f(int,int), i is 10 , j is 20\nIn f(double), k is 12.23\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.10, Page Number: 171<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Create an overloaded function of abs called myabs()'''\n\n#myabs() in three ways\ndef myabs(i):\n if isinstance(i,int): #first instance\n print \"Using integer myabs(): \",\n if i<0:\n return -i\n else:\n return i\n elif isinstance(i,float): #second instance\n print \"Using double myabs(): \",\n if(i<0.0):\n return -i\n else:\n return i\n elif isinstance(i,long): #third instance\n print \"Using long myabs(): \",\n if i<0:\n return -i\n else:\n return i\n\n#Result; calling the function \nprint myabs(-10)\nprint myabs(-11.0)\nprint myabs(-9L)\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Using integer myabs(): 10\nUsing double myabs(): 11.0\nUsing long myabs(): 9\n"
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.11, Page Number: 174<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''An example of default arguments'''\n\nimport os\n\ndef clrscr(size=25):\n while(size):\n print \"\"\n size-=1\n \nfor i in range(30):\n print i\n clrscr() #clears 25 lines\n\nfor i in range(30):\n print i\n clrscr(10) #clears 10 lines\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n1\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n2\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n3\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n4\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n5\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n6\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n7\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n8\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n9\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n10\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n11\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n12\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n13\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n14\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n15\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n16\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n17\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n18\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n19\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n20\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n21\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n22\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n23\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n24\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n25\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n26\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n27\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n28\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n29\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n0\n \n \n \n \n \n \n \n \n \n \n1\n \n \n \n \n \n \n \n \n \n \n2\n \n \n \n \n \n \n \n \n \n \n3\n \n \n \n \n \n \n \n \n \n \n4\n \n \n \n \n \n \n \n \n \n \n5\n \n \n \n \n \n \n \n \n \n \n6\n \n \n \n \n \n \n \n \n \n \n7\n \n \n \n \n \n \n \n \n \n \n8\n \n \n \n \n \n \n \n \n \n \n9\n \n \n \n \n \n \n \n \n \n \n10\n \n \n \n \n \n \n \n \n \n \n11\n \n \n \n \n \n \n \n \n \n \n12\n \n \n \n \n \n \n \n \n \n \n13\n \n \n \n \n \n \n \n \n \n \n14\n \n \n \n \n \n \n \n \n \n \n15\n \n \n \n \n \n \n \n \n \n \n16\n \n \n \n \n \n \n \n \n \n \n17\n \n \n \n \n \n \n \n \n \n \n18\n \n \n \n \n \n \n \n \n \n \n19\n \n \n \n \n \n \n \n \n \n \n20\n \n \n \n \n \n \n \n \n \n \n21\n \n \n \n \n \n \n \n \n \n \n22\n \n \n \n \n \n \n \n \n \n \n23\n \n \n \n \n \n \n \n \n \n \n24\n \n \n \n \n \n \n \n \n \n \n25\n \n \n \n \n \n \n \n \n \n \n26\n \n \n \n \n \n \n \n \n \n \n27\n \n \n \n \n \n \n \n \n \n \n28\n \n \n \n \n \n \n \n \n \n \n29\n \n \n \n \n \n \n \n \n \n \n"
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.12, Page Number: 176<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''A customized version of strcat'''\n\n#Variable Decleration\nstr1 = \"This is a test\"\nstr2 = \"0123456789\"\n\n#function for concatenation\ndef mystrcat(s1,s2,l=-1):\n if l==-1:\n l=len(str2)\n s2=s2[:l] #truncates s2\n s1=s1+s2 #concatenates the 2 strings\n return s1\n\nstr1=mystrcat(str1,str2,5) #concatenate 5 chars\nprint str1\n\nstr1 = \"this is a test\" #reset str1\n\nstr1=mystrcat(str1, str2) #concatenate entire string\nprint str1",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "This is a test01234\nthis is a test0123456789\n"
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.13, Page Number: 177<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Overload ambiguity'''\n#Here there wont be any ambiguity because float and double are the same.\n\ndef myfunc(i):\n return i\n \nprint myfunc(10.1),\nprint myfunc(10)",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10.1 10\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.14, Page Number: 178<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Another ambiguity'''\n\nfrom ctypes import *\n\ndef myfunc(ch):\n if isinstance(ch,c_int):\n return chr(ch.value+1)\n elif isinstance(ch,c_uint):\n return chr(ch.value-1)\n \n \nprint myfunc(c_int(ord('c'))),\nprint myfunc(c_uint(88))",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": " d W\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 8.15, Page Number: 179<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''More ambiguity'''\n\ndef myfunc(i,j=1):\n if j==1:\n return i*j\n else:\n return i\n \n \nprint myfunc(4,5),\nprint myfunc(10)",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "4 10\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file