{ "metadata": { "name": "", "signature": "sha256:e85379c4218575d4b069259557cffbbc2d0259e3ba5d0e030c11dd77aae5e38d" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.1, Page Number:444" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#A simple classA having a public data member x\n", "class A:\n", " def __init__(self):\n", " self.x=None\n", "\n", "#A simple classA having a public data member y \n", "class B(A): #derived class\n", " def __init__(self):\n", " self.y=None\n", " \n", "b=B() #create a instance b of Derived class B\n", "b.x=20\n", "b.y=30\n", "\n", "print 'member of A:',b.x\n", "print 'Member of B:',b.y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "member of A: 20\n", "Member of B: 30\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.2, Page Number:445" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class A:\n", " def __init__(self): #class A having x as a private data member\n", " self.__x=20\n", " \n", " def showx(self):\n", " print \"x=\",self.__x\n", " \n", " \n", "class B(A): #Derived class\n", " def __init__(self):\n", " self.y=30 #class B having y as a public data member\n", " \n", " def show(self):\n", " a=A()\n", " a.showx()\n", " print \"y=\",self.y\n", " \n", " \n", "b=B() #declaration of object\n", " #class the method of derived class object by a derived class instance\n", "b.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x= 20\n", "y= 30\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.3, Page Number:447" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#base class\n", "class A:\n", " def __init__(self):\n", " self.x=None #x is a public member\n", " \n", " \n", "#derived class\n", "class B(A):\n", " def __init__(self):\n", " self.y=40 \n", " A.__x=20 #since it is privately inherites base class ,x become private member of it\n", " \n", " def show(self):\n", " print \"x=\",A.__x\n", " print \"y=\",self.y\n", " \n", "b=B()\n", "b.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x= 20\n", "y= 40\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.4, Page Number:448" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class A:\n", " def __init__(self):\n", " self.__x=20 #x is a privet member of it\n", " \n", " def showx(self): \n", " print \"x=\",self.__x\n", " \n", " \n", "class B(A):\n", " def __init__(self):\n", " self.y=40 #y is a public member of it\n", " \n", " def show(self):\n", " a=A()\n", " a.showx() #call the base class method\n", " print \"y=\",self.y\n", " \n", " \n", "b=B()\n", "b.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x= 20\n", "y= 40\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.5, Page Number:449" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class A:\n", " def __init__(self):\n", " self._x=None #x is a protected member of the base class\n", " \n", " \n", "class B(A): #private inheritance,x become a private member of the derived class\n", " def __init__(self):\n", " self.y=40\n", " self.__x=30\n", " \n", " \n", " def show(self):\n", " print \"x=\",self.__x\n", " print \"y=\",self.y\n", " \n", " \n", "b=B()\n", "b.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x= 30\n", "y= 40\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.6, Page Number:456" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class ABC: #Base class\n", " def __init__(self):\n", " self._name=None #these 2 are protected data member\n", " self._age=None\n", " \n", "class abc(ABC): #Derived class ,Public derivation\n", " def __init__(self):\n", " self.height=None\n", " self.weight=None\n", " \n", " def getdata(self):\n", " \n", " self.name=raw_input(\"Enter a name: \") #take inputes to all the data members \n", " self.age=raw_input(\"Enter a age: \") \n", " self._height=raw_input(\"Enter a Height: \") \n", " self._weight=raw_input(\"Enter a Weight: \") \n", " \n", " def show(self): #display the value of data members\n", " print 'Name:',self.name \n", " print 'Age:',self.age,\"years\"\n", " print 'Height:',self._height,\"Feets\"\n", " print 'Weight:',self._weight,\"kg.\"\n", " \n", " \n", "x=abc()\n", "x.getdata()\n", "x.show()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a name: Santosh\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a age: 24\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Height: 4.5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Weight: 50\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Name: Santosh\n", "Age: 24 years\n", "Height: 4.5 Feets\n", "Weight: 50 kg.\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.7, Page Number:458" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class A1: #super Base class,have 2 protected data members\n", " def __init__(self):\n", " self._name=None\n", " self._age=None\n", "\n", " \n", "class A2(A1): #Public derivation\n", " def __init(self):\n", " self._height=None\n", " self._weight=None\n", "\n", "class A3(A2): #public Derivation\n", " def __init__(self):\n", " self._sex=None\n", " \n", " \n", " def get(self): #get input \n", " self._name=raw_input(\"Name: \")\n", " self._age=raw_input(\"Age: \")\n", " self._sex=raw_input(\"Sex: \")\n", " \n", " self._height=raw_input(\"Height: \")\n", " self._weight=raw_input(\"Weight: \")\n", " \n", " def show(self): #Display values of all the data members\n", " print \"Name:\",self._name\n", " print \"Age:\",self._age ,\"years\"\n", " print \"Sex:\",self._sex\n", " print \"Height:\",self._height ,\"Feet\"\n", " print \"Weight:\",self._weight ,\"Kg.\"\n", " \n", "\n", "x=A3()\n", "x.get()\n", "x.show()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Name: Balaji\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Age: 26\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Sex: M\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Height: 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Weight: 49.5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Name: Balaji\n", "Age: 26 years\n", "Sex: M\n", "Height: 4 Feet\n", "Weight: 49.5 Kg.\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.8, Page Number:459" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Example of multiple Inheritance\n", "\n", "class A:\n", " def __init__(self):\n", " self._a=None\n", " \n", "class B:\n", " def __init__(self):\n", " self._b=None\n", " \n", " \n", "class C:\n", " def __init__(self):\n", " self._c=None\n", " \n", "class D:\n", " def __init__(self):\n", " self._d=None\n", " \n", "class E(A,B,C,D): #inherites all the base classes publically\n", " def __init__(self):\n", " self.e=None\n", " \n", " def getdata(self):\n", " print \"Enter the value of a,b,c &d &e:\"\n", " self._a=input()\n", " self._b=input()\n", " self._c=input()\n", " self._d=input()\n", " self._e=input()\n", " \n", " def show(self):\n", " print\"a=\",self._a,\"b=\",self._b,\"c=\",self._c,\"d=\",self._d,\"e=\",self._e\n", " \n", " \n", "x=E() #x is the instance of the derived class\n", "x.getdata() #call the methods of derived class through x \n", "x.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the value of a,b,c &d &e:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "16\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "a= 1 b= 2 c= 4 d= 8 e= 16\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.9, Page Number:461" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class red: #these three base class\n", " def __init__(self):\n", " print \"Red\",\n", " \n", "class yellow:\n", " def __init__(self):\n", " print \"Yellow\",\n", " \n", "class blue:\n", " def __init__(self):\n", " print \"Blue\",\n", " \n", "class orange(red,yellow): #public multiple Derivation\n", " def __init__(self):\n", " red.__init__(self)\n", " yellow.__init__(self)\n", " print \"=Orange\",\n", " \n", "class green(blue,yellow): #public multiple Derivation\n", " def __init__(self):\n", " blue.__init__(self)\n", " yellow.__init__(self)\n", " print \"=Green\",\n", " \n", "class violet(red,blue): #public multiple Derivation\n", " def __init__(self):\n", " red.__init__(self)\n", " blue.__init__(self)\n", " print \"=Violet\",\n", " \n", "class reddishbrown(orange,violet): #public multiple & multilevel Derivation\n", " def __init__(self):\n", " for b in self.__class__.__bases__:\n", " b.__init__(self)\n", " print \"=Reddishbrown\"\n", " \n", "class yellowishbrown(green,orange): #public multiple & multilevel Derivation\n", " def __init__(self):\n", " for b in self.__class__.__bases__:\n", " b.__init__(self)\n", " print \"=Yellowishbrown\"\n", " \n", "class bluishbrown(violet,green): #public multiple & multilevel Derivation\n", " def __init__(self):\n", " for b in self.__class__.__bases__:\n", " b.__init__(self)\n", " print \"=Bluishbrown\"\n", " \n", " \n", " \n", "r=reddishbrown() #create instances of the derived class\n", "b=bluishbrown()\n", "y=yellowishbrown()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Red Yellow =Orange Red Blue =Violet =Reddishbrown\n", "Red Blue =Violet Blue Yellow =Green =Bluishbrown\n", "Blue Yellow =Green Red Yellow =Orange =Yellowishbrown\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.10, Page Number:463" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# WAP to create a derived class from multiple base classes\n", "\n", "class PLAYER: #these three are the base classes\n", " def __init__(self):\n", " self._name=None\n", " self._gender=None\n", " self._age\n", " \n", "class PHYSIQUE(PLAYER):\n", " def __init__(self):\n", " self._height=None\n", " self._weight=None\n", " \n", "class LOCATION:\n", " def __init__(self):\n", " self._city=None\n", " self._pin=None\n", " \n", "class GAME(PHYSIQUE,LOCATION): #Multiple derivation\n", " def __init__(self):\n", " self._game=None\n", " def getdata(self): #Method to take inputes\n", " print\"Enter the following information\\n\\n\"\n", " self._name=raw_input(\"Name:\")\n", " self._gender=raw_input(\"Gender:\")\n", " self._age=raw_input(\"Age:\")\n", " self._height=raw_input(\"Height:\")\n", " self._weight=raw_input(\"Weight:\")\n", " self._city=raw_input(\"City:\")\n", " self._pin=raw_input(\"Pin:\")\n", " self._game=raw_input(\"game:\")\n", " \n", " \n", " \n", " def show(self): #Method for displaying inputes\n", " print\"Entered Information!!\"\n", " print\"Name:\",self._name\n", " print \"Gender:\",self._gender\n", " print \"Age:\",self._age\n", " print \"Height:\",self._height\n", " print \"Weight:\",self._weight\n", " print \"City :\",self._city\n", " print \"Pincode:\",self._pin\n", " print \"Game :\",self._game\n", " \n", " \n", "G=GAME() #create an instance of the derived class\n", "G.getdata() #call the public methods by the created instances\n", "G.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the following information\n", "\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Name:Mahesh\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Gender:M\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Age:25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Height:4.9\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Weight:55\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "City:Nanded\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Pin:431603\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "game:Cricket\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Entered Information!!\n", "Name: Mahesh\n", "Gender: M\n", "Age: 25\n", "Height: 4.9\n", "Weight: 55\n", "City : Nanded\n", "Pincode: 431603\n", "Game : Cricket\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.11, Page Number:467" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Multipath Inheritance,concept of virtual classes\n", "\n", "class A1: #Super base class\n", " def __init__(self):\n", " self._a1=None\n", " \n", "class A2(A1): #base class 1,inherites Super Base class\n", " def __init__(self):\n", " self._a2=None\n", " \n", "class A3(A1): #base class 2,inherites Super Base class\n", " def __init__(self):\n", " self._a3=None\n", " \n", "class A4(A2,A3): #derived class ,public derivation of both the base classes\n", " def __init__(self):\n", " self.__a4=None\n", " \n", " def get(self):\n", " print \"Enter the value of a1,a2,a3,and a4:\"\n", " self._a1=input()\n", " self._a2=input()\n", " self._a3=input()\n", " self.__a4=input()\n", " \n", " def put(self):\n", " print \"a1=\",self._a1,\"a2=\",self._a2,\"a3=\",self._a3,\"a4=\",self.__a4\n", " \n", " \n", " \n", "a=A4() #create the instance of the derived class\n", "a.get()\n", "a.put()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the value of a1,a2,a3,and a4:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "a1= 5 a2= 8 a3= 7 a4= 3\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.12, Page Number:469" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#To show order of execution of the constructors and destructors in multiple inheritance\n", "\n", "#**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++\n", "\n", "class A:\n", " def __init__(self):\n", " print\"Zero argument Constructor of base class A\"\n", " \n", " def __del__(self):\n", " print\"Destructor of class A\"\n", " \n", "class B:\n", " def __init__(self):\n", " print\"Zero argument Constructor of base class B\"\n", " \n", " def __del__(self):\n", " print\"Destructor of class B\"\n", "\n", "class C(A,B):\n", " def __init__(self):\n", " for b in self.__class__.__bases__:\n", " b.__init__(self)\n", " print\"Zero argument Constructor of base class C\"\n", " \n", " def __del__(self):\n", " print\"Destructor of class C\"\n", " for b in self.__class__.__bases__:\n", " b.__del__(self)\n", " \n", "c=C() #create instance of derived class" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Zero argument Constructor of base class A\n", "Zero argument Constructor of base class B\n", "Zero argument Constructor of base class C\n", "Destructor of class C\n", "Destructor of class A\n", "Destructor of class B\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.13, Page Number:471" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#WAP to use constructor and destructor in all the classess\n", "\n", "class A1:\n", " def __init__(self): #take name and age as input in super base class\n", " self._name=raw_input(\"Name:\")\n", " self._age=raw_input(\"Age:\")\n", " \n", " def __del__(self):\n", " print\"Name:\",self._name\n", " print\"Age\",self._age\n", " \n", " \n", "class A2(A1): #take height and weight as input in base base class,public derivation \n", " def __init__(self):\n", " A1.__init__(self)\n", " self._height=raw_input(\"Height:\")\n", " self._weight=raw_input(\"Weight:\")\n", " \n", " def __del__(self):\n", " print\"Height:\",self._height\n", " print\"Weight:\",self._weight\n", " A1.__del__(self)\n", " \n", " \n", "class A3(A2): #take sex as input in derived class,derived from class A2\n", " def __init__(self):\n", " A2.__init__(self)\n", " self.__sex=raw_input(\"Sex:\")\n", " def __del__(self): #display all the input taken by all the base classes\n", " print\"Sex:\",self.__sex\n", " A2.__del__(self)\n", " \n", " \n", "x=A3() #create instance x of the class A3\n", "\n", "del x #call the destructor" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Name:Ajay\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Age:20\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Height:4.5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Weight:40\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Sex:M\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sex: M\n", "Height: 4.5\n", "Weight: 40\n", "Name: Ajay\n", "Age 20\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.14, Page Number:472" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#To create derived class from the base class,by constructor and destructor\n", "class in_t:\n", " def __init__(self):\n", " self._i=1\n", " print\"Constructor in_t()\"\n", " \n", " def __del__(self):\n", " print\"Destructor in_t()\"\n", " \n", "class floa_t:\n", " def __init__(self):\n", " self._f=1.5\n", " print\"Constructor floa_t()\"\n", " \n", " def __del__(self):\n", " print\"Destructor floa_t()\"\n", " \n", " \n", "class cha_r(in_t,floa_t): #multiple derivation\n", " def __init__(self):\n", " self._c='A'\n", " print\"Constructor cha_r()\"\n", " for b in self.__class__.__bases__:\n", " b.__init__(self)\n", " \n", " def show(self):\n", " print\"i=\",self._i\n", " print \"f=\",self._f\n", " print \"c=\",self._c\n", " \n", " def __del__(self):\n", " print \"Destructing cha_r()\"\n", " for b in self.__class__.__bases__:\n", " b.__del__(self)\n", " \n", "a=cha_r() #create derived class instance and call the public method of the derived class\n", "a.show() #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Constructor cha_r()\n", "Constructor in_t()\n", "Constructor floa_t()\n", "i= 1\n", "f= 1.5\n", "c= A\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.15, Page Number:474" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class I:\n", " def __init__(self):\n", " self.x=None\n", " \n", "class II(I):\n", " def __init__(self):\n", " self.__y=None\n", " \n", " def set(self,j,k):\n", " self.x=j\n", " self.__y=k\n", " \n", " def show(self):\n", " print \"X=\",self.x, \"Y=\",self.__y\n", " \n", " \n", "i=II()\n", "i.set(4,5)\n", "i.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "X= 4 Y= 5\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.16, Page Number:475" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class I:\n", " def __init__(self):\n", " self.x=10\n", " print \"In the Base class constuctor\"\n", " \n", "class II(I):\n", " def __init__(self):\n", " I.__init__(self)\n", " self.__y=None\n", " \n", "i=II()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "In the Base class constuctor\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.17, Page Number:475" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#base class without constructor and derived class with constructor\n", "class I:\n", " pass\n", "class II(I):\n", " def __init__(self):\n", " print \"In derived class constructor\"\n", " \n", "i=II()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "In derived class constructor\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.18, Page Number:476" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#both the class have constructor\n", "class I:\n", " def __init__(self):\n", " print \"In base class Constructor\"\n", " \n", "class II(I):\n", " def __init__(self):\n", " I.__init__(self)\n", " print \"In derived Class constructor\"\n", " \n", "i=II()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "In base class Constructor\n", "In derived Class constructor\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.19, Page Number:477" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#multiple constructor in base class and single constructor in the derived class\n", "\n", "class I:\n", " def __init__(self):\n", " print \"Zero argument base class construtor\"\n", " \n", " def __init__(self,k):\n", " self.x=None\n", " print \"One argument base class construtor\"\n", " \n", " \n", "class II(I):\n", " def __init__(self,j,k=None): #default constructor\n", " I.__init__(self,k)\n", " self.__y=j\n", " print \"One argument derived class constructor\"\n", " \n", "i=II(2) #create the instance of the base class by passing initial value " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "One argument base class construtor\n", "One argument derived class constructor\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.20, Page Number:478" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#base and derived class without default constructor\n", "class I:\n", " def __init__(self,k):\n", " self.x=k\n", " print \"One argument base class construtor\"\n", " \n", "class II(I):\n", " def __init__(self,j):\n", " I.__init__(self,j)\n", " self.__y=j\n", " print \"One argument derived class construtor\"\n", " \n", "i=II(2) #create the instance of the base class by passing initial value " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "One argument base class construtor\n", "One argument derived class construtor\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.21, Page Number:479" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Constructors and multiple inheritance\n", "class I:\n", " def __init__(self):\n", " print \"Zero argument constructor of base class I\"\n", " \n", "class II:\n", " def __init__(self):\n", " print \"Zero argument constructor of base class II\"\n", " \n", "class III(II,I): #class III inhrites class II and I\n", " def __init__(self):\n", " for b in self.__class__.__bases__:\n", " b.__init__(self) \n", " print \"Zero argument constructor of base class III\"\n", " \n", "i=III() #create an instance of the base class\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Zero argument constructor of base class II\n", "Zero argument constructor of base class I\n", "Zero argument constructor of base class III\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.22, Page Number:480" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Constructors in multiple inhritance with invoking constructor of the base classes\n", "class I:\n", " def __init__(self):\n", " print \"Zero argument constructor of base class I\"\n", " \n", "class II:\n", " def __init__(self):\n", " print \"Zero argument constructor of base class II\"\n", " \n", "class III(II,I):\n", " def __init__(self):\n", " II.__init__(self)\n", " I.__init__(self)\n", " print \"Zero argument constructor of base class III\"\n", " \n", "i=III()\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Zero argument constructor of base class II\n", "Zero argument constructor of base class I\n", "Zero argument constructor of base class III\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.23, Page Number:481" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#multiple inheritance,invoking the base classes explicitly\n", "\n", "class I:\n", " def __init__(self):\n", " print \"Zero argument constructor of base class I\"\n", " \n", "class II:\n", " def __init__(self):\n", " print \"Zero argument constructor of base class II\"\n", " \n", "class III(II,I): #Class I is virtually inherited so its constructor called first\n", " def __init__(self):\n", " I.__init__(self)\n", " II.__init__(self)\n", " print \"Zero argument constructor of base class III\"\n", " \n", "i=III()\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Zero argument constructor of base class I\n", "Zero argument constructor of base class II\n", "Zero argument constructor of base class III\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.24, Page Number:482" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#multilevel Inheritance,observation of the execution of the constructors\n", "class I:\n", " def __init__(self):\n", " print \"Zero argument constructor of base class I\"\n", " \n", "class II(I):\n", " def __init__(self):\n", " I.__init__(self)\n", " print \"Zero argument constructor of base class II\"\n", " \n", "class III(II): #Class I is virtually inherited so its constructor called first\n", " def __init__(self):\n", " II.__init__(self)\n", " print \"Zero argument constructor of base class III\"\n", " \n", "i=III()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Zero argument constructor of base class I\n", "Zero argument constructor of base class II\n", "Zero argument constructor of base class III\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.25, Page Number:484" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#use object of one class in another class as a member\n", "class I:\n", " def __init__(self):\n", " self.x=20\n", " print \"Constructor of class I\"\n", " \n", "class II:\n", " \n", " def __init__(self):\n", " self.k=30\n", " y=I()\n", " print \"Constructor of class II\"\n", " print \"x=\",y.x #print here because it become local variable in this scope only,it not visible to def show\n", " \n", " \n", " def show(self):\n", " print \"k=\",self.k\n", " \n", "ii=II()\n", "ii.show() #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Constructor of class I\n", "Constructor of class II\n", "x= 20\n", "k= 30\n" ] } ], "prompt_number": 19 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.26, Page Number:484" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#access a member variable of base class using object,class name, and direct\n", "\n", "class A1:\n", " def __init__(self):\n", " self.name=None\n", " self.age=None\n", " \n", "class A2(A1):\n", " def __init__(self):\n", " A1.__init__(self)\n", " a=A1()\n", " print \"Access using name of the class:\"\n", " A1.name=raw_input(\"Name:\")\n", " A1.age=raw_input(\"Age:\")\n", " \n", " print \"Access using object of the class\"\n", " a.name=raw_input(\"Name:\")\n", " a.age=raw_input(\"Age:\")\n", " \n", " print \"Access using direct member variables:\"\n", " self.name=raw_input(\"Name:\")\n", " self.age=raw_input(\"Age:\")\n", " self.__height=raw_input(\"Height:\")\n", " self.__weight=raw_input(\"Weight:\")\n", " \n", " print \"Display using object of the class\" #since object of class A1 has scope in constructor method so we can access it only \n", " print \"Name:\",a.name # within this method.It is not visible in destructor function.\n", " print \"Age:\",a.age\n", " \n", " \n", " \n", " def __del__(self):\n", " print \"Destructor of Derived class\"\n", " print \"Display using class name\"\n", " print \"Name:\",A1.name\n", " print \"Age:\",A1.age\n", " \n", " print \"Display using direct member variable\"\n", " print \"Name:\",self.name\n", " print \"Age\",self.age\n", " print \"height:\",self.__height\n", " print \"Weight:\",self.__weight\n", " \n", "x=A2()\n", "\n", "del x\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Access using name of the class:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Name:Ajay\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Age:21\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Access using object of the class\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Name:Amit\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Age:20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Access using direct member variables:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Name:Arun\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Age:19\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Height:5.5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Weight:31\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Display using object of the class\n", "Name: Amit\n", "Age: 20\n", "Destructor of Derived class\n", "Display using class name\n", "Name: Ajay\n", "Age: 21\n", "Display using direct member variable\n", "Name: Arun\n", "Age 19\n", "height: 5.5\n", "Weight: 31\n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.27, Page Number:488" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#derive a class from two base classes,object of these 2 classes is the member variable of the third class\n", "\n", "class A:\n", " def __init__(self):\n", " self.a1=None\n", " \n", "class B:\n", " def __init__(self):\n", " self.b1=None\n", " \n", "class AB:\n", " def __init__(self):\n", " a=A()\n", " b=B()\n", " a.a1=65 #initialize the two data members of the class A and B and Display them\n", " b.b1=66\n", " print \"a1=\",a.a1, \"b1=\",b.b1\n", " \n", " def __del__(self):\n", " pass\n", " \n", " \n", "ab=AB()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a1= 65 b1= 66\n" ] } ], "prompt_number": 21 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.28, Page Number:489" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#create derived class from qualifier class\n", "\n", "class A:\n", " def __init__(self):\n", " self.x=None\n", " \n", " class B:\n", " def __init__(self):\n", " self.y=None\n", " \n", " \n", "class C(A,A.B): #A.B is the inner class of the class A\n", " def __init__(self,j,k,l):\n", " self.x=j\n", " self.y=k\n", " self.z=l\n", " \n", " def show(self):\n", " print \"x=\",self.x,\"y=\",self.y,\"z=\",self.z\n", " \n", " \n", "c=C(4,7,1)\n", "c.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x= 4 y= 7 z= 1\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.29, Page Number:490" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#initialize member variable of the base class and derived class using constructor of the derived class\n", "\n", "class A:\n", " def __init__(self):\n", " self._x=None #protected members\n", " self._y=None\n", " \n", "class B(A):\n", " def __init__(self):\n", " self.z=3\n", " self.__x=1 #private members\n", " self.__y=2\n", " \n", " print \"x=\",self.__x,\"y=\",self.__y,\"z=\",self.z\n", " \n", "b=B()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x= 1 y= 2 z= 3\n" ] } ], "prompt_number": 24 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.30, Page Number:491" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#access data members by object pointer\n", "\n", "from ctypes import *\n", "import ctypes\n", "class A:\n", " def __init__(self):\n", " self.x=1\n", " self.y=2\n", " \n", "class B(A):\n", " def __init__(self):\n", " A.__init__(self)\n", " self.z=3\n", " \n", "b=B()\n", "\n", "\n", "i=c_int(b.z)\n", "p=pointer(i)\n", "print \"Address of z:\",addressof(p),\"Value of Z:\",p[0] #access the\n", "\n", "i = c_int(b.y)\n", "p = pointer(i)\n", "print \"Address of y:\",addressof(p),\"Value of y:\",p[0] \n", "\n", "i = c_int(b.x)\n", "p = pointer(i)\n", "print \"Address of x:\",addressof(p),\"Value of x:\",p[0] \n", "\n", "#**NOTE-In case of C++ the data members of the derived class and base class are stored in contigious memory locations so we can \n", "#access the three variables by using a pointer of derived class and decrementing its value. But in case of Python they are NOT stored \n", "#in contogious memory locations so for accessing each data member we have to create individual object pointer for each class." ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Address of z: 57077392 Value of Z: 3\n", "Address of y: 57074448 Value of y: 2\n", "Address of x: 57077648 Value of x: 1\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.31, Page Number:492" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#overload member function in base and derived class\n", "\n", "class B:\n", " def show(self):\n", " print \"In base class function\"\n", " \n", "class D(B):\n", " def show(self):\n", " \n", " print \"In Derived class\"\n", " \n", " \n", "b=B()\n", "d=D()\n", "\n", "b.show()\n", "d.show()\n", "\n", "bp=[B()] #create a base class pointer variable\n", "bp[0]=d #assign address of the derived class object to the base class pointer\n", "bp[0].show() #call the derived class method by base class pointer\n", "b.show() #calling the base class method by base class object" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "In base class function\n", "In Derived class\n", "In Derived class\n", "In base class function\n" ] } ], "prompt_number": 25 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.32, Page Number:495" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Constuctor and destructors in single inheritance\n", "class Father:\n", " def __init__(self):\n", " print \"Base Class constructor.\"\n", " self._name=raw_input(\"Enter Father Name:\")\n", " \n", " def __del__(self):\n", " print \"Base class Destructor.\"\n", " \n", "class Child(Father):\n", " def __init__(self):\n", " Father.__init__(self)\n", " print \"Derived class constructor.\"\n", " self.__cname=raw_input(\"Enter child name:\")\n", " \n", " def __del__(self):\n", " print \"Derived class destructor.\"\n", " print \"\",self.__cname,\"\",self.__name\n", " for b in self.__class__.__bases__:\n", " b.__del__(self)\n", " \n", " \n", " \n", "C=Child()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Base Class constructor.\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Father Name:Manoj\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Derived class constructor.\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter child name:Sanjay\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Derived class destructor.\n", " Sanjay Manoj\n", "Base class Destructor.\n" ] } ], "prompt_number": 37 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.33, Page Number:496" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Constuctor and destructors in multilevel inheritance\n", "\n", "class Grandfather:\n", " def __init__(self):\n", " print\"Constructor of class grandfather\"\n", " self._gname=raw_input(\"Enter Grandfather Name:\")\n", " \n", " \n", " def __del__(self):\n", " print \"Destructor of class grandfather\"\n", " \n", " \n", "class Father(Grandfather):\n", " def __init__(self):\n", " Grandfather.__init__(self)\n", " print\"Constructor of class Father\"\n", " self._name=raw_input(\"Enter Father Name:\")\n", " \n", " \n", " def __del__(self):\n", " print \"Destructor of class Father\"\n", " Grandfather.__del__(self)\n", " \n", "class Child(Father):\n", " def __init__(self):\n", " Father.__init__(self)\n", " print\"Constructor of class Child\"\n", " self.__cname=raw_input(\"Enter Child Name:\")\n", " \n", " \n", " def __del__(self):\n", " print \"Destructor of class Child\"\n", " print \"Grandfather:\",self._gname,\"Father:\",self._name,\"Child:\",self.__cname\n", " Father.__del__(self) \n", " \n", " \n", "C=Child()\n", "\n", "del C #call the destructor of the derived class\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Constructor of class grandfather\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Grandfather Name:x\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Constructor of class Father\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Father Name:y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Constructor of class Child\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Child Name:z\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Destructor of class Child\n", "Grandfather: x Father: y Child: z\n", "Destructor of class Father\n", "Destructor of class grandfather\n" ] } ], "prompt_number": 39 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.34, Page Number:498" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#program to explain multilevel inheritance with member function\n", "class Grandfather:\n", " def __init__(self):\n", " self.__gname=None\n", " \n", " def getg(self):\n", " self.__gname=raw_input(\"Enter Grandfather Name:\")\n", " \n", " def showg(self):\n", " print \"Grandfather Name:\",self.__gname\n", " \n", " \n", "class Father(Grandfather):\n", " def __init__(self):\n", " self.__name=None\n", " \n", " def getf(self):\n", " self.__name=raw_input(\"Enter Father Name:\")\n", " \n", " def showf(self):\n", " print \"Father Name:\",self.__name\n", " \n", " \n", "class Child(Father):\n", " def __init__(self):\n", " self.__cname=None\n", " \n", " def getc(self):\n", " self.getg()\n", " self.getf()\n", " self.__cname=raw_input(\"Enter Child Name:\")\n", " \n", " def showc(self):\n", " self.showg()\n", " self.showf()\n", " print \"child Name:\",self.__cname\n", " \n", "C=Child()\n", "C.getc()\n", "C.showc()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Grandfather Name:XXX\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Father Name:YYY\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Child Name:ZZZ\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Grandfather Name: XXX\n", "Father Name: YYY\n", "child Name: ZZZ\n" ] } ], "prompt_number": 40 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.35, Page Number:499" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#execution of constructor and destructor in multilevel inheritance\n", "\n", "class A:\n", " def __init__(self):\n", " print \"Constructor of class A\"\n", " \n", " def __del__(self):\n", " print \"Destructor of class A\"\n", " \n", " \n", "class B:\n", " def __init__(self):\n", " print \"Constructor of class B\"\n", " \n", " def __del__(self):\n", " print \"Destructor of class B\"\n", " \n", "class C:\n", " def __init__(self):\n", " print \"Constructor of class C\"\n", " \n", " def __del__(self):\n", " print \"Destructor of class C\"\n", " \n", " \n", "class D(A,B,C):\n", " def __init__(self):\n", " for b in self.__class__.__bases__:\n", " b.__init__(self) \n", " print \"Constructor of class D\"\n", " \n", " def __del__(self):\n", " print \"Destructor of class D\"\n", " for b in self.__class__.__bases__:\n", " b.__del__(self)\n", " \n", "x=D() \n", " #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Constructor of class A\n", "Constructor of class B\n", "Constructor of class C\n", "Constructor of class D\n", "Destructor of class D\n", "Destructor of class A\n", "Destructor of class B\n", "Destructor of class C\n" ] } ], "prompt_number": 42 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.37, Page Number:502" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#execution of constructor and destructor in multilevel inheritance\n", "\n", "class A:\n", " def __init__(self):\n", " print \"Constructor of class A\"\n", " \n", " def __del__(self):\n", " print \"Destructor of class A\"\n", " \n", " \n", "class B(A):\n", " def __init__(self):\n", " A.__init__(self)\n", " print \"Constructor of class B\"\n", " \n", " def __del__(self):\n", " print \"Destructor of class B\"\n", " A.__del__(self)\n", " \n", "class C:\n", " def __init__(self):\n", " print \"Constructor of class C\"\n", " \n", " def __del__(self):\n", " print \"Destructor of class C\"\n", " \n", " \n", "class D(B,C):\n", " def __init__(self):\n", " B.__init__(self)\n", " C.__init__(self)\n", " print \"Constructor of class D\"\n", " \n", " def __del__(self):\n", " print \"Destructor of class D\"\n", " B.__del__(self)\n", " C.__del__(self)\n", " \n", "x=D() \n", "del x\n", " #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Constructor of class A\n", "Constructor of class B\n", "Constructor of class C\n", "Constructor of class D\n", "Destructor of class D\n", "Destructor of class B\n", "Destructor of class A\n", "Destructor of class C\n" ] } ], "prompt_number": 54 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.37, Page Number:502" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#demonstrate single inheritance\n", "\n", "class A:\n", " def __init__(self,j=0):\n", " self._c=j\n", " \n", " def show(self):\n", " print \"c=\",self._c\n", " \n", " def inc(self):\n", " self._c=self._c+1\n", " return self._c\n", " \n", "class B(A):\n", " \n", " def __init_(self):\n", " for b in self.__class__.__bases__:\n", " b.__init__(self)\n", " \n", " def dec(self):\n", " self._c=self._c-1\n", " return self._c\n", " \n", " \n", "a=B()\n", "a.inc()\n", "a.show()\n", "\n", "\n", "a.dec()\n", "a.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "c= 1\n", "c= 0\n" ] } ], "prompt_number": 44 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.38, Page Number:502" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#access method from private inheritance\n", "class B:\n", " def one(self):\n", " print \"one\"\n", " \n", " def __two(self):\n", " print \"two\"\n", " \n", "class D(B):\n", " def __init__(self):\n", " pass\n", " \n", "d=D()\n", "d.one()\n", "#d.two() #Not accesible" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "one\n" ] } ], "prompt_number": 45 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.39, Page Number:503" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#create a comman constructor in the lowermost class, in the multilevel inheritance\n", "class A:\n", " def __init__(self):\n", " self.x=None\n", " \n", "class B(A):\n", " def __init__(self):\n", " self.y=None\n", " \n", "class C(B):\n", " def __init__(self,j,k,l):\n", " self.z=l\n", " self.x=j\n", " self.y=k\n", " \n", " def show(self):\n", " print \"x=\",self.x,\"Y=\",self.y,\"z=\",self.z\n", " \n", "c=C(4,7,1)\n", "c.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x= 4 Y= 7 z= 1\n" ] } ], "prompt_number": 46 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 11.40, Page Number:504" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Explicitly call the base constructor in multiple inheritance\n", "\n", "class X:\n", " def __init__(self,a):\n", " print a,\n", " \n", "class Y:\n", " def __init__(self,b):\n", " print b,\n", " \n", "class Z(X,Y):\n", " def __init__(self,p,q,r):\n", " X.__init__(self,p)\n", " Y.__init__(self,q)\n", " print r\n", " \n", " \n", "z=Z(1,2,3)\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 3\n" ] } ], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }