{ "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": {} } ] }