diff options
Diffstat (limited to 'C++_Demystified:_A_Self-Teaching_Guide/chapter14.ipynb')
-rw-r--r-- | C++_Demystified:_A_Self-Teaching_Guide/chapter14.ipynb | 779 |
1 files changed, 779 insertions, 0 deletions
diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter14.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter14.ipynb new file mode 100644 index 00000000..57cde338 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter14.ipynb @@ -0,0 +1,779 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 14: The Road Ahead: Structures and Classes" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.1, page no. 317" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "declaring structure\n", + "note: there is no concept of structure in python. We will use class instead\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = 0" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.2, page no. 318" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "declares a three-element Person array, assigns values to the member\n", + "variables of each element, and then outputs their values\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = 0\n", + "\n", + "MAX = 3\n", + "p = []\n", + "\n", + "for x in range(MAX):\n", + " per = Person()\n", + " print \"Enter person's name: \",\n", + " per.name = raw_input()\n", + " print \"Enter height in inches: \",\n", + " per.height = int(raw_input())\n", + " p.append(per)\n", + "\n", + "print \"Outputting person data\\n\";\n", + "print \"======================\\n\";\n", + "for x in range(MAX):\n", + " print \"Person \", x + 1, \"'s name is \", p[x].name, \" and height is \", p[x].height" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Kent\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jefff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "40\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Outputting person data\n", + "\n", + "======================\n", + "\n", + "Person 1 's name is Jeff and height is 50\n", + "Person 2 's name is Kent and height is 50\n", + "Person 3 's name is Jefff and height is 40\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.3, page no. 320" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "constructors & destructors\n", + "note: there are no destructors in Python\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = 0\n", + "\n", + "MAX = 3\n", + "p1 = Person()\n", + "print \"The person's name is \", p1.name, \" and height is \", p1.height" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The person's name is and height is 0\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.4, page no. 322" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "constructors\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " def __init__(self):\n", + " self.name = \"no name assigned\"\n", + " self.height = -1\n", + "\n", + "MAX = 3\n", + "p1 = Person()\n", + "print \"The person's name is \", p1.name, \" and height is \", p1.height" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The person's name is no name assigned and height is -1\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.5, page no. 323" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "constructors with arguments\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " def __init__(self, s=\"no name assigned\", h=-1):\n", + " self.name = s\n", + " self.height = h\n", + "\n", + "MAX = 3\n", + "print \"Enter a person's name: \",\n", + "s = raw_input()\n", + "print \"Enter height in inches: \",\n", + "h = int(raw_input())\n", + "p1 = Person(s, h)\n", + "print \"The person's name is \", p1.name, \" and height is \", p1.height" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "39\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The person's name is Jeff and height is 39\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.6, page no. 324" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Separating the Constructor Prototype and Implementation\n", + "note: separation is not possible in Python we will do it the same way as previous\n", + "program\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " def __init__(self, s=\"no name assigned\", h=-1):\n", + " self.name = s\n", + " self.height = h\n", + "\n", + "MAX = 3\n", + "print \"Enter a person's name: \",\n", + "s = raw_input()\n", + "print \"Enter height in inches: \",\n", + "h = int(raw_input())\n", + "p1 = Person(s, h)\n", + "print \"The person's name is \", p1.name, \" and height is \", p1.height\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The person's name is Jeff and height is 50\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.7, page no. 325" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Passing Structures as Function Arguments\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = \"\"\n", + " def setValues(self, pers):\n", + " print \"Enter person's name: \",\n", + " pers.name = raw_input()\n", + " print \"Enter height in inches: \",\n", + " pers.height = int(raw_input())\n", + " def getValues(self, pers):\n", + " print \"Person's name is \", pers.name, \" height is \", pers.height\n", + "\n", + "p1 = Person()\n", + "p1.setValues(p1)\n", + "print \"Outputting person data\"\n", + "print \"======================\"\n", + "p1.getValues(p1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "60\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Outputting person data\n", + "======================\n", + "Person's name is Jeff height is 60\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.8, page no. 326" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "nesting structures\n", + "\"\"\"\n", + "\n", + "class Date:\n", + " month = 0\n", + " day = 0\n", + " year = 0\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = 0\n", + " bDay = Date()\n", + " def setValues(self, pers):\n", + " print \"Enter person's name: \",\n", + " pers.name = raw_input()\n", + " print \"Enter height in inches: \",\n", + " pers.height = int(raw_input())\n", + " print \"Enter birthdate\"\n", + " pers.bDay.month = int(raw_input(\"Month: \"))\n", + " pers.bDay.day = int(raw_input(\"Day: \"))\n", + " pers.bDay.year = int(raw_input(\"Year: \"))\n", + " def getValues(self, pers):\n", + " print \"Person's name: \", pers.name\n", + " print \"Person's height\", pers.height\n", + " print \"Person's birthday in mm/dd/yyyy format is: \", pers.bDay.month, pers.bDay.day, pers.bDay.year\n", + "\n", + "p1 = Person()\n", + "p1.setValues(p1)\n", + "print \"Outputting person data\"\n", + "print \"======================\"\n", + "p1.getValues(p1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "59\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 05\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 1999\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Outputting person data\n", + "======================\n", + "Person's name: Jeff\n", + "Person's height 59\n", + "Person's birthday in mm/dd/yyyy format is: 20 5 1999\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.9, page no. 330" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "classes\n", + "note: there won't be any error unlike the situation mentioned in textbook\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = \"\"\n", + " def setValues(self, pers):\n", + " print \"Enter person's name: \",\n", + " pers.name = raw_input()\n", + " print \"Enter height in inches: \",\n", + " pers.height = int(raw_input())\n", + " def getValues(self, pers):\n", + " print \"Person's name is \", pers.name, \" height is \", pers.height\n", + "\n", + "p1 = Person()\n", + "p1.setValues(p1)\n", + "print \"Outputting person data\"\n", + "print \"======================\"\n", + "p1.getValues(p1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "JEff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Outputting person data\n", + "======================\n", + "Person's name is JEff height is 50\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.10, page no. 331" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "private & public specifiers\n", + "note: there are no such specifiers in Python\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = \"\"\n", + " def setValues(self, pers):\n", + " print \"Enter person's name: \",\n", + " pers.name = raw_input()\n", + " print \"Enter height in inches: \",\n", + " pers.height = int(raw_input())\n", + " def getValues(self):\n", + " print \"Person's name is \", self.getName(), \" height is \", self.getHeight()\n", + " def getName(self):\n", + " return self.name\n", + " def getHeight(self):\n", + " return self.height\n", + "\n", + "p1 = Person()\n", + "p1.setValues(p1)\n", + "print \"Outputting person data\"\n", + "print \"======================\"\n", + "p1.getValues()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "60\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Outputting person data\n", + "======================\n", + "Person's name is Jeff height is 60\n" + ] + } + ], + "prompt_number": 10 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |