Chapter 14: Inheritance

Example 14.1, Page Number: 333

In [1]:
class road_vehicle:
    def __init__(self):
        self.__wheels=None
        self.__passengers=None
    def set_wheels(self,num):
        self.__wheels=num
    def get_wheels(self):
        return self.__wheels
    def set_pass(self,num):
        self.__passengers=num
    def get_pass(self):
        return self.__passengers

#Define a truck
class truck(road_vehicle):
    def __init__(self):
        self.__cargo=None
    def set_cargo(self,size):
        self.__cargo=size
    def get_cargo(self):
        return self.__cargo
    def show(self):
        print "wheels: ",self.get_wheels()
        print "passengers: ",self.get_pass()
        print "cargo capacity in cubic feet: ",self.__cargo
 
#Define an enum type
(car,van,wagon)=(1,2,3)
type=["car","van","wagon"]
 
#Define an automobile
class automobile(road_vehicle):
    def __init__(self):
        self.car_type=None
    def set_type(self,t):
        self.car_type=t
    def get_type(self):
        return self.car_type
    def show(self):
        print "wheels: ",self.get_wheels()
        print "passengers: ",self.get_pass()
        print "type: ",
        if self.get_type()==1:
            print "car"
        elif self.get_type()==2:
            print "van"
        elif self.get_type()==3:
            print "wagon"
 
#Variable declaration
t1=truck()
t2=truck()
c=automobile()

t1.set_wheels(18)
t1.set_pass(2)
t1.set_cargo(3200)

t2.set_wheels(6)
t2.set_pass(3)
t2.set_cargo(1200)

t1.show()
t2.show()

c.set_wheels(4)
c.set_pass(6)
c.set_type(van)

c.show()          
    
wheels:  18
passengers:  2
cargo capacity in cubic feet:  3200
wheels:  6
passengers:  3
cargo capacity in cubic feet:  1200
wheels:  4
passengers:  6
type:  van

Example 14.2, Page Number: 335

In [1]:
class base:
    def __init__(self):
        self.__i=self.__j=None
    def set(self,a,b):
        self.__i=a
        self.__j=b
    def show(self):
        print self.__i,self.__j
        
class derived(base):
    def __init__(self,x):
        self.__k=x
    def showk(self):
        print self.__k
 
#Variable declaration
ob = derived(3)

ob.set(1,2)         #access member of base
ob.show()           #access member of base

ob.showk()          #uses member of derived class
        
1 2
3

Example 14.3, Page Number: 337

In [10]:
class base:
    def __init__(self):
        self.__i=self.__j=None  #These act as protected members
    def set(self,a,b):
        self.__i=a
        self.__j=b
    def show(self):
        print self.__i,self.__j
        
class derived(base):
    def __init__(self):
        self.__k=None
    def setk(self):
        self.__k=self._base__i*self._base__j  #accessing private variables in derived class
    def showk(self):
        print self.__k
 
#Variable declaration
ob = derived()

ob.set(2,3)         #OK, known to be derived
ob.show()           #OK, known to be derived

ob.setk()
ob.showk()          #uses member of derived class
        
2 3
6

Example 14.4, Page Number: 338

In [11]:
class base:
    def __init__(self):
        self.__i=None
        self.__j=None
    def set(self,a,b):
        self.__i=a
        self.__j=b
    def show(self):
        print self.__i,self.__j
        
class derived1(base):
    def __init__(self):
        self.__k=None
    def setk(self):
        self.__k=self._base__i*self._base__j
    def showk(self):
        print self.__k

class derived2(derived1):
    def __init__(self):
        self.__m=None
    def setm(self):
        self.__m=self._base__i-self._base__j
    def showm(self):
        print self.__m
        
        
#Variable declaration
ob1 = derived1()
ob2 = derived2()

ob1.set(2,3)         #access member of base
ob1.show()           #access member of base
ob1.setk()           #uses member of derived1 class
ob1.showk()          #uses member of derived1 class

ob2.set(3,4)         #access member of base
ob2.show()           #access member of base
ob2.setk()           #access member of derived1 class
ob2.setm()           #access member of derived2 class
ob2.showk()          #uses member of derived1 class
ob2.showm()          #uses member of derived1 class
        
2 3
6
3 4
12
-1

Example 14.5, Page Number: 341

In [1]:
class base:
    def __init__(self):
        self.__i=None
        self._j=None
        self.k=None
    def seti(self,a):
        self.__i=a
    def geti(self):
        return i
    
class derived(base):
    def setj(self,a):
        self._j=a
    def setk(self,a):
        self.k=a
    def getj(self):
        return self._j
    def getk(self):
        return self.k
   
#Variable declaration    
ob=derived()

ob.setk(10)
print ob.getk(),
ob.setj(12)
print ob.getj()
10 12

Example 14.6, Page Number: 342

In [17]:
class base1:
    def __init__(self):
        self.x=None
    def showx(self):
        print self.x
    
class base2:
    def __init__(self):
        self.y=None
    def showy(self):
        print self.y
        
class derived(base1,base2):
    def set(self,i,j):
        self.x=i
        self.y=j
 
#Variable declaration
ob = derived()

ob.set(10,20)  #provided by derived
ob.showx()     #from base1
ob.showy()     #from base2
10
20

Example 14.7, Page Number: 343

In [19]:
class base:
    def __init__(self):
        print "Constructing base"
    def __del__(self):
        print "Destructing base"

class derived(base):
    def __init__(self):
        base.__init__(self)
        print "Constructing derived"
    def __del__(self):
        print "Destructing derived"
        for b in self.__class__.__bases__:
            b.__del__(self)

#Variable declaration
ob=derived()

#Does nothing but construct and destruct ob
Constructing base
Constructing derived
Destructing derived
Destructing base

Example 14.8, Page Number: 344

In [ ]:
class base:
    def __init__(self):
        print "Constructing base"
    def __del__(self):
        print "Destructing base"

class derived1(base):
    def __init__(self):
        base.__init__(self)
        print "Constructing derived1"
    def __del__(self):
        print "Destructing derived1"
        super(derived1,self).__del__(self)

class derived2(derived1):
    def __init__(self):
        derived1.__init__(self)
        print "Constructing derived2"
    def __del__(self):
        print "Destructing derived2"
        super(self.__class__,self).__del__(self)
            
#Variable declaration
ob=derived2()

#Does nothing but construct and destruct ob

Example 14.9, Page Number: 345

In [21]:
class base1:
    def __init__(self):
        print "Constructing base1"
    def __del__(self):
        print "Destructing base1"
    
class base2:
    def __init__(self):
        print "Constructing base2"
    def __del__(self):
        print "Destructing base2"
        
class derived(base1,base2):
    def __init__(self):
        for b in self.__class__.__bases__:
            b.__init__(self)
        print "Constructing derived"
    def __del__(self):
        print "Destructing derived"
        for b in self.__class__.__bases__:
            b.__del__(self)
 
#Variable declaration
ob = derived()
Constructing base1
Constructing base2
Constructing derived
Destructing derived
Destructing base1
Destructing base2

Example 14.10, Page Number: 347

In [23]:
class base:
    def __init__(self,x):
        self._i=x
        print "Constructing base"
    def __del__(self):
        print "Destructing base"

class derived(base):
    def __init__(self,x,y):
        base.__init__(self,y)
        self.__j=x
        print "Constructing derived"
    def __del__(self):
        print "Destructing derived"
        for b in self.__class__.__bases__:
            b.__del__(self)
    def show(self):
        print self._i,self.__j

#Variable declaration
ob=derived(3,4)

#Result
ob.show()              #shows 4 3
Constructing base
Constructing derived
Destructing derived
Destructing base
4 3

Example 14.11, Page Number: 348

In [25]:
class base1:
    def __init__(self,x):
        self._i=x
        print "Constructing base1"
    def __del__(self):
        print "Destructing base1"
    
class base2:
    def __init__(self,x):
        self._k=x
        print "Constructing base2"
    def __del__(self):
        print "Destructing base2"
        
class derived(base1,base2):
    def __init__(self,x,y,z):
        self.__j=x
        i=0
        for b in self.__class__.__bases__:
            if i==0:
                b.__init__(self,y)
            else :
                b.__init__(self,z)
            i+=1
        print "Constructing derived"
    def __del__(self):
        print "Destructing derived"
        for b in self.__class__.__bases__:
            b.__del__(self)
    def show(self):
        print self._i,self.__j,self._k
 
#Variable declaration
ob = derived(3,4,5)

#Result
ob.show()
Constructing base1
Constructing base2
Constructing derived
Destructing derived
Destructing base1
Destructing base2
4 3 5

Example 14.12, Page Number: 348

In [26]:
class base1:
    def __init__(self,x):
        self._i=x
        print "Constructing base1"
    def __del__(self):
        print "Destructing base1"
    
class base2:
    def __init__(self,x):
        self._k=x
        print "Constructing base2"
    def __del__(self):
        print "Destructing base2"
        
class derived(base1,base2):
    def __init__(self,x,y):
        i=0
        for b in self.__class__.__bases__:
            if i==0:
                b.__init__(self,x)
            else :
                b.__init__(self,y)
            i+=1
        print "Constructing derived"
    def __del__(self):
        print "Destructing derived"
        for b in self.__class__.__bases__:
            b.__del__(self)
    def show(self):
        print self._i,self._k
 
#Variable declaration
ob = derived(3,4)

#Result
ob.show()
Constructing base1
Constructing base2
Constructing derived
Destructing derived
Destructing base1
Destructing base2
3 4

Example 14.13, Page Number: 351

In [28]:
class base:
    def __init__(self):
        self.__i=None
        self.j=self.k=None
    def seti(self,x):
        self.__i=x
    def geti(self):
        return self.__i

class derived(base):
    def __init__(self):
        self.a=None


#Variable declaration
ob=derived()

ob._base__i=10     #Accessing private members of base class
ob.j=20            #legal because j and k are public variable in base
ob.k=30

ob.a=40           #legal because a is public in derived class
ob.seti(10)

#Result
print ob.geti(),ob.j,ob.a
10 20 40

Example 14.14, Page Number: 354

In [29]:
class base:
    def __init__(self):
        self.i=None

#derived1 inherits base
class derived1(base):
    def __init__(self):
        self.__j=None
          
#derived2 inherits base
class derived2(base):
    def __init__(self):
        self.__k=None

#derived3 inherits from both derived1 and derived2
class derived3(derived1,derived2):
    def __init__(self):
        self.__sum=None
 
#Variable declaration
ob=derived3()

ob.i=10
ob.j=20
ob.k=30

ob.sum=ob.i+ob.j+ob.k

#Result
print ob.i,ob.j,ob.k,ob.sum
10 20 30 60

Example 14.15, Page Number: 355

In [30]:
class base:
    def __init__(self):
        self.i=None

#derived1 inherits base
class derived1(base):
    def __init__(self):
        self.__j=None
          
#derived2 inherits base
class derived2(base):
    def __init__(self):
        self.__k=None

#derived3 inherits from both derived1 and derived2
class derived3(derived1,derived2):
    def __init__(self):
        self.__sum=None
 
#Variable declaration
ob=derived3()

ob.i=10
ob.j=20
ob.k=30

ob.sum=ob.i+ob.j+ob.k

#Result
print ob.i,ob.j,ob.k,ob.sum
10 20 30 60
In [ ]: