{
 "metadata": {
  "name": "",
  "signature": "sha256:1bf3a3c32b50a821205df977feb0399264ec0b05a84acfd47497b8eab54ae29b"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 7: Creating Classes and objects"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.1, Page No.118"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class dog:\n",
      "    def bark(self):\n",
      "        print \"WOOF!\"\n",
      "    \n",
      "    def setAge(self,age):\n",
      "        self.age=age\n",
      "    \n",
      "    def setWeight(self,weight):\n",
      "        self.weight=weight\n",
      "    \n",
      "    def setColor(self,color):\n",
      "        self.color=color\n",
      "    \n",
      "    def getAge(self):\n",
      "        return self.age\n",
      "    \n",
      "    def getWeight(self):\n",
      "        return self.weight\n",
      "    \n",
      "    def getColor(self):\n",
      "        return self.color\n",
      "    \n",
      "    \n",
      "if __name__=='__main__':\n",
      "    fido=dog()\n",
      "    fido.setAge(3)\n",
      "    fido.setWeight(15)\n",
      "    fido.setColor(\"brown\")\n",
      "    \n",
      "    print \"Fido is a\",fido.getColor(),\" dog\"\n",
      "    print \"Fido is \",fido.getAge(),\" years old\"\n",
      "    print \"Fido Weighs \",fido.getWeight(),\" pounds\"\n",
      "    fido.bark()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Fido is a brown  dog\n",
        "Fido is  3  years old\n",
        "Fido Weighs  15  pounds\n",
        "WOOF!\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.2, Page No.120"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class dog:\n",
      "    def bark(self):\n",
      "        print \"WOOF!\"\n",
      "    \n",
      "    def setValues(self,age,weight,color):\n",
      "        self.age=age\n",
      "        self.weight=weight\n",
      "        self.color=color\n",
      "        \n",
      "    def getAge(self):\n",
      "        return self.age\n",
      "    \n",
      "    def getWeight(self):\n",
      "        return self.weight\n",
      "    \n",
      "    def getColor(self):\n",
      "        return self.color\n",
      "    \n",
      "    \n",
      "if __name__=='__main__':\n",
      "    fido=dog()\n",
      "    fido.setValues(3,15,\"brown\")\n",
      "    poonch=dog()\n",
      "    poonch.setValues(4,18,\"gray\")\n",
      "    print \"Fido is a\",fido.getColor(),\" dog\"\n",
      "    print \"Fido is \",fido.getAge(),\" years old\"\n",
      "    print \"Fido Weighs \",fido.getWeight(),\" pounds\"\n",
      "    fido.bark()\n",
      "    \n",
      "    print \"Poonch is \",poonch.getAge(),\" years old \",poonch.getColor(),\" dog who weighs \",poonch.getWeight(),\" pounds\",poonch.bark()\n",
      "   \n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Fido is a brown  dog\n",
        "Fido is  3  years old\n",
        "Fido Weighs  15  pounds\n",
        "WOOF!\n",
        "Poonch is  4  years old  gray  dog who weighs  18  pounds WOOF!\n",
        "None\n"
       ]
      }
     ],
     "prompt_number": 36
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.3, Page No.122"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class dog:\n",
      "    def __init__(self,age,weight,color):\n",
      "        self.age=age\n",
      "        self.weight=weight\n",
      "        self.color=color\n",
      "        \n",
      "    def bark(self):\n",
      "        print \"WOOF!\"\n",
      "    \n",
      "    def getAge(self):\n",
      "        return self.age\n",
      "    \n",
      "    def getWeight(self):\n",
      "        return self.weight\n",
      "    \n",
      "    def getColor(self):\n",
      "        return self.color\n",
      "    \n",
      "    def __del__(self):\n",
      "        print \"Object destroyed\"\n",
      "    \n",
      "if __name__=='__main__':\n",
      "    fido=dog(3,15,\"brown\")\n",
      "    poonch=dog(4,18,\"gray\")\n",
      "   \n",
      "    print \"Fido is a\",fido.getColor(),\" dog\"\n",
      "    print \"Fido is \",fido.getAge(),\" years old\"\n",
      "    print \"Fido Weighs \",fido.getWeight(),\" pounds\"\n",
      "    fido.bark()\n",
      "    \n",
      "    print \"Poonch is \",poonch.getAge(),\" years old \",poonch.getColor(),\" dog who weighs \",poonch.getWeight(),\" pounds\",poonch.bark()\n",
      "   \n",
      "    del fido\n",
      "    del poonch"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Fido is a brown  dog\n",
        "Fido is  3  years old\n",
        "Fido Weighs  15  pounds\n",
        "WOOF!\n",
        "Poonch is  4  years old  gray  dog who weighs  18  pounds WOOF!\n",
        "None\n",
        "Object destroyed\n",
        "Object destroyed\n"
       ]
      }
     ],
     "prompt_number": 37
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exmaple 7.4, Page No.124"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class dog:\n",
      "    def __init__(self,age=1,weight=2,color=\"black\"):\n",
      "        self.age=age\n",
      "        self.weight=weight\n",
      "        self.color=color \n",
      "    \n",
      "    def bark(self,noise):\n",
      "        print noise\n",
      "    \n",
      "    def getAge(self):\n",
      "        return self.age\n",
      "    \n",
      "    def getWeight(self):\n",
      "        return self.weight\n",
      "    \n",
      "    def getColor(self):\n",
      "        return self.color\n",
      "    \n",
      "    def __del__(self):\n",
      "        print \"Object destroyed\"\n",
      "    \n",
      "if __name__=='__main__':\n",
      "    fido=dog(3,15,\"brown\")\n",
      "    poonch=dog(4,18,\"gray\")\n",
      "    rex=dog()\n",
      "    sammy=dog(2,6,\"white\")\n",
      "    \n",
      "    \n",
      "    print \"Fido is a\",fido.getColor(),\" dog\"\n",
      "    print \"Fido is \",fido.getAge(),\" years old\"\n",
      "    print \"Fido Weighs \",fido.getWeight(),\" pounds\"\n",
      "    fido.bark(\"WOOF!\")\n",
      "    \n",
      "    print \"Poonch is \",poonch.getAge(),\" years old \",poonch.getColor(),\" dog who weighs \",poonch.getWeight(),\" pounds\",poonch.bark(\"WOOF!\")\n",
      "    print \"Rex is \",rex.getAge(),\" years old \",rex.getColor(),\" dog who weighs \",rex.getWeight(),\" pounds\",rex.bark(\"GRRR!\")\n",
      "    print \"Sammy is \",sammy.getAge(),\" years old \",sammy.getColor(),\" dog who weighs \",sammy.getWeight(),\" pounds\",sammy.bark(\"BOWOW!\")\n",
      "    del fido\n",
      "    del poonch\n",
      "    del rex\n",
      "    del sammy"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Fido is a brown  dog\n",
        "Fido is  3  years old\n",
        "Fido Weighs  15  pounds\n",
        "WOOF!\n",
        "Poonch is  4  years old  gray  dog who weighs  18  pounds WOOF!\n",
        "None \n",
        "Rex is  1  years old  black  dog who weighs  2  pounds GRRR!\n",
        "None \n",
        "Sammy is  2  years old  white  dog who weighs  6  pounds BOWOW!\n",
        "None \n",
        "Object destroyed\n",
        "Object destroyed\n",
        "Object destroyed\n",
        "Object destroyed\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.5, Page No.126"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Polygon:\n",
      "    weight=0\n",
      "    height=0\n",
      "    def setValues(self,w,h):\n",
      "        self.weight=w\n",
      "        self.height=h\n",
      "\n",
      "class Rectangle(Polygon):\n",
      "    def area(self):\n",
      "        return self.weight*self.height\n",
      "\n",
      "class Triangle(Polygon):\n",
      "    def area(self):\n",
      "        return (self.weight*self.height)/2\n",
      "\n",
      "rect=Rectangle()\n",
      "trgl=Triangle()\n",
      "rect.setValues(4,5)\n",
      "trgl.setValues(4,5)\n",
      "print \"Rectangle Area:\",rect.area()\n",
      "print \"Triangle Area:\",trgl.area()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Rectangle Area: 20\n",
        "Triangle Area: 10\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.6, Page No.128"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Parent:\n",
      "    def __init__(self):\n",
      "        print \"Default Parent Constructor Called\"\n",
      "    def __init__(self,a):\n",
      "        print \"Overloaded Parent Constructor Called\"\n",
      "\n",
      "class Daughter(Parent):\n",
      "    def __init__(self):\n",
      "        print \"Derived Daughter class default constructor called\"\n",
      "\n",
      "class Son(Parent):\n",
      "    def __init__(self,a):\n",
      "        Parent.__init__(self,a)\n",
      "        print \"Derived Son class overloaded constructor called\"\n",
      "\n",
      "emma=Daughter()\n",
      "andrew=Son(0)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Derived Daughter class default constructor called\n",
        "Overloaded Parent Constructor Called\n",
        "Derived Son class overloaded constructor called\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example No.7.7, Page No. 130"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Man:\n",
      "    def Speak(self, msg=\"Hello\"):\n",
      "        print \"   \",msg\n",
      "\n",
      "class Hombre(Man):\n",
      "    def Speak(self,msg):\n",
      "        print msg\n",
      "henry=Man()\n",
      "enrique=Hombre()\n",
      "henry.Speak()\n",
      "henry.Speak(\"It's a beautiful evening.\")\n",
      "enrique.Speak(\"Hola!\")\n",
      "enrique.Speak(\"Es una trade hermosa\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "    Hello\n",
        "    It's a beautiful evening.\n",
        "Hola!\n",
        "Es una trade hermosa\n"
       ]
      }
     ],
     "prompt_number": 22
    }
   ],
   "metadata": {}
  }
 ]
}