{
 "metadata": {
  "name": "",
  "signature": "sha256:fa5fb390e0208b333276a0ed5185d5e56d34940c09a52bd389b2d0242ea600f2"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 8: Harnessing polymorphism"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.1, Page No.134"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Base:\n",
      "    def Identify(self,adr):\n",
      "        print \"Base Class called by \",hex(adr)\n",
      "\n",
      "class SubA(Base):\n",
      "    def __init__(self,adr):\n",
      "        Base.Identify(self,adr)\n",
      "class SubB(Base):\n",
      "    def __init__(self,adr):\n",
      "        Base.Identify(self,adr)\n",
      "ptrA=1\n",
      "ptrB=SubA(id(ptrA))\n",
      "ptrC=2\n",
      "ptrc=SubB(id(ptrC))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Base Class called by  0x12fd8e0\n",
        "Base Class called by  0x12fd8d4\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.2, Page No.136"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Parent:\n",
      "    def Common(self):\n",
      "        print \"I am part of this family\"\n",
      "    def identify(self):\n",
      "        print \"I am the parent\"\n",
      "\n",
      "class Child(Parent):\n",
      "    def identify(self):\n",
      "        print \"I am the child\"\n",
      "        \n",
      "class Grandchild(Child):\n",
      "    def identify(self):\n",
      "        print \"I am the grandchild\"\n",
      "    def Relate(self):\n",
      "        print \"Grandchild has parent and grandparent\"\n",
      "\n",
      "son=Child()\n",
      "grandson=Grandchild()\n",
      "parent=Parent()\n",
      "print parent.Common(),\" \",son.identify()\n",
      "print parent.Common(),\" \",grandson.identify()\n",
      "print parent.Common(),\" \",parent.identify()\n",
      "print grandson.Relate()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "I am part of this family\n",
        "None   I am the child\n",
        "None\n",
        "I am part of this family\n",
        "None   I am the grandchild\n",
        "None\n",
        "I am part of this family\n",
        "None   I am the parent\n",
        "None\n",
        "Grandchild has parent and grandparent\n",
        "None\n"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.3, Page No.138"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Bird:\n",
      "    def Talk(self):\n",
      "        print \"A Bird talks...\"\n",
      "    def Fly(self):\n",
      "        print \"A Bird flies...\"\n",
      "class Pigeon(Bird):\n",
      "    def Talk(self):\n",
      "        print \"Coo!Coo!\"\n",
      "    def Fly(self):\n",
      "        print \"A Pigeon flies away...\"\n",
      "class Chicken(Bird):\n",
      "    def Talk(self):\n",
      "        print \"Cluk!Cluk!\"\n",
      "    def Fly(self):\n",
      "        print \"I'm just a chicken- I can't fly!\"\n",
      "pPigeon=Pigeon()\n",
      "pChicken=Chicken()\n",
      "pBird=Bird()\n",
      "print \"\",pPigeon.Talk()\n",
      "print \"\",pPigeon.Fly()\n",
      "print \"\",pChicken.Talk()\n",
      "print \"\",pChicken.Fly()\n",
      "print \"\",pBird.Talk()\n",
      "print \"\",pBird.Fly()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Coo!Coo!\n",
        "None\n",
        " A Pigeon flies away...\n",
        "None\n",
        " Cluk!Cluk!\n",
        "None\n",
        " I'm just a chicken- I can't fly!\n",
        "None\n",
        " A Bird talks...\n",
        "None\n",
        " A Bird flies...\n",
        "None\n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.4, Page No. 140"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Bird:\n",
      "    def Talk(self):\n",
      "        return -1\n",
      "    def Fly(self):\n",
      "        return -1\n",
      "class Pigeon(Bird):\n",
      "    def Talk(self):\n",
      "        print \"Coo!Coo!\"\n",
      "    def Fly(self):\n",
      "        print \"A Pigeon flies away...\"\n",
      "class Chicken(Bird):\n",
      "    def Talk(self):\n",
      "        print \"Cluk!Cluk!\"\n",
      "    def Fly(self):\n",
      "        print \"I'm just a chicken- I can't fly!\"\n",
      "pPegion=Pigeon()\n",
      "pChicken=Chicken()\n",
      "pPegion.Talk()\n",
      "pChicken.Talk()\n",
      "pBird=Bird()\n",
      "data=pBird.Talk()\n",
      "if data==-1:\n",
      "    print \"Error!-Program ended\"\n",
      "    exit()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Coo!Coo!\n",
        "Cluk!Cluk!\n",
        "Error!-Program ended\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.5, Page No.142"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Rect:\n",
      "    height=0\n",
      "    width=0\n",
      "    def __init__(self,initWidth,initHeight):\n",
      "        self.height=initHeight\n",
      "        self.width=initWidth\n",
      "    def getArea(self):\n",
      "        return self.height*self.width\n",
      "    def getEdge(self):\n",
      "        return ((2*self.height)+(2*self.width))      \n",
      "    def Draw(self):\n",
      "        for i in range(0,self.height):\n",
      "            for j in range(0,self.width):\n",
      "                print \"X\",\n",
      "            print \"\\n\"\n",
      "\n",
      "pQuad=Rect(7,3)\n",
      "pSquare=Rect(5,5)\n",
      "\n",
      "pQuad.Draw()\n",
      "print \"Area is:\",pQuad.getArea()\n",
      "print \"Perimeter is:\",pQuad.getEdge()\n",
      "\n",
      "pSquare.Draw()\n",
      "print \"Area is:\",pSquare.getArea()\n",
      "print \"Perimeter is:\",pSquare.getEdge()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "X X X X X X X \n",
        "\n",
        "X X X X X X X \n",
        "\n",
        "X X X X X X X \n",
        "\n",
        "Area is: 21\n",
        "Perimeter is: 20\n",
        "X X X X X \n",
        "\n",
        "X X X X X \n",
        "\n",
        "X X X X X \n",
        "\n",
        "X X X X X \n",
        "\n",
        "X X X X X \n",
        "\n",
        "Area is: 25\n",
        "Perimeter is: 20\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.6, Page No.144"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Boat:\n",
      "    length=0\n",
      "    def getLength(self):\n",
      "        return self.length\n",
      "class Sailboat(Boat):\n",
      "    mast=0\n",
      "    def getMast(self):\n",
      "        return self.mast\n",
      "class Laser(Sailboat):\n",
      "    def __init__(self):\n",
      "        self.mast=19\n",
      "        self.length=35\n",
      "    def Model(self):\n",
      "        print \"Laser Classic:\"\n",
      "    def Boom(self):\n",
      "        print \"Boom: 14ft\"\n",
      "pLaser=Laser()\n",
      "pLaser.Model()\n",
      "print \"Length:\",pLaser.getLength(),\"ft\"\n",
      "print \"Height:\",pLaser.getMast(),\"ft\"\n",
      "pLaser.Boom()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Laser Classic:\n",
        "Length: 35 ft\n",
        "Height: 19 ft\n",
        "Boom: 14ft\n"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.7, Page No.147"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Calculator:\n",
      "    status=\"true\"\n",
      "    num1=0.0\n",
      "    num2=0.0\n",
      "    oper='+'\n",
      "    def __init__(self):\n",
      "        self.status=\"true\"\n",
      "    def launch(self):\n",
      "        print \"***SUM CALCULATOR***\"\n",
      "        print \"Enter a number, an operator (+,-,*,/), and another number\\nEnter Zero to exit\"\n",
      "    def readInput(self):\n",
      "        print \">\",\n",
      "        self.num1=float(raw_input())\n",
      "        if self.num1==0:\n",
      "            self.status=\"false\"\n",
      "        else:\n",
      "            self.oper=raw_input()\n",
      "            self.num2=float(raw_input())\n",
      "    def writeOutput(self):\n",
      "        if self.status==\"true\":\n",
      "            if self.oper=='+':\n",
      "                print self.num1+self.num2\n",
      "            elif self.oper=='-':\n",
      "                print self.num1-self.num2\n",
      "            elif self.oper=='*':\n",
      "                print self.num1*self.num2\n",
      "            elif self.oper=='/':\n",
      "                if self.num2!=0:\n",
      "                    print self.num1/self.num2\n",
      "                else:\n",
      "                    print \"Cannot divide by zero\"\n",
      "    def run(self):\n",
      "        return self.status\n",
      "    \n",
      "            \n",
      "        \n",
      "    \n",
      "pCalc=Calculator()\n",
      "pCalc.launch()\n",
      "while (pCalc.run()==\"true\"):\n",
      "    pCalc.readInput()\n",
      "    pCalc.writeOutput()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " ***SUM CALCULATOR***\n",
        "Enter a number, an operator (+,-,*,/), and another number\n",
        "Enter Zero to exit\n",
        ">"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "32\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "+\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "32\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 64.0\n",
        ">"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5.25\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "-\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "8.75\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " -3.5\n",
        ">"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "8\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "*\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 24.0\n",
        ">"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "20\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "/\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 4.0\n",
        ">"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "20\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "/\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Cannot divide by zero\n",
        ">"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 4
    }
   ],
   "metadata": {}
  }
 ]
}