Chapter 10 Class Relationships

Program Source Code 10.1, page no: 286

In [1]:
import sys
import string
class Super:                      #class super (base class)
     
    
    def __init__(self,x=0):    #constructor
        self.__a=x
        print 'Super constructor called'
       
    def __del__(self):              #destructor
        print 'Super destructor called'
    def _printInfo(self):
        print 'a= ',self.__a,
        print 'size of data component of Super class is ',sys.getsizeof(self.__a)
    def print1():
        Super.printInfo()
        
class Sub(Super):   #derived class
    
    def __init__(self,y=0):       #constructor
        Super.__init__(self,y)
        self.__b=y+10
        print 'Sub constructor called'
    def _del__(self):                       #destructor
        print 'Sub destructor called'
    def _printInfo(self):
        Super._printInfo(self)
        print 'b= ',self.__b,
        print 'size of data component of Sub class is ',sys.getsizeof(self.__b)                #sizeof(self.__b) 

        
#variable declaration
super1=Super(100 )
sub1=Sub(200 )
super1._printInfo()
sub1._printInfo()
print 'Size of super1 is',sys.getsizeof(super1)
print 'Size of sub1 is',sys.getsizeof(sub1)
Super constructor called
Super constructor called
Sub constructor called
a=  100 size of data component of Super class is  24
a=  200 size of data component of Super class is  24
b=  210 size of data component of Sub class is  24
Size of super1 is 64
Size of sub1 is 64

Program Source Code 10.2, page no: 295

In [2]:
class A:         #base class
    def __init__(self,x):      #constructor
        self.__a=x+10              
        print 'A constructor called A: a',self.__a
    def __del__(self):
        print 'A des'
        
class B(A):       #derived class
    def __init__(self,x):            #constructor
        self.__b=x+20
        print 'B constructor called B: b',self.__b
    def __del__(self):
        print 'B des'
        
class C(A,B):     
    def __init__(self,x):   #constructor
        self.__c=x+30
        print 'C constructor called C: c',self.__c
    def __del__(self):
        print 'C des'
                
class D(A,B):
    def __init__(self,x):              #constructor
        self.__d=x+60
        print 'D constructor called D: d',self.__d
    def __del__(self):
        print 'D des'
        
        
class E(C,D):
    def __init__(self,x):                  #constructor
        self.__e=x+90
        A.__init__(self,x+100)
        B.__init__(self,x+110)
        C.__init__(self,x+120)
        D.__init__(self,x+130)
        print 'E constructor called E: e',self.__e
    def __del__(self):
        print 'E des'
#variable declaration        
e=E(1000)
A constructor called A: a 1110
B constructor called B: b 1130
C constructor called C: c 1150
D constructor called D: d 1190
E constructor called E: e 1090

Program Source Code 10.3, page no: 299

In [9]:
class Aclass:
    def __init__(self):       #constructor
        pass
    def __del__(self):
        pass   
    def printFraction(self,f):             #friend function
        print 'Function AClass:: printFraction, prints Fraction: ',f._Fraction__num,'/',f._Fraction__denom
        
    def setFractionToZero(self,f):
        f.__num=0
        f.__denom=1
        print 'Function AClass:: setfractionToZero, sets fraction to zero: '
        
class Bclass:
    def __init__(self):                   #constructor
        pass
    def __del__(self):
        pass
    def printFraction(self,f):        #friend function
        print 'Function BClass:: printFraction, prints Fraction: ',f._Fraction__num,'/',f._Fraction__denom
    def setFractionToZero(self,f):
        print 'Function BClass:: setfractionToZero CANNOT, CANT set fraction to 0 '
        
        
class Fraction:
    def __init__(self,a=0,b=1):             #constructor
        self.__num=a
        self.__denom=b
    def __del__(self):
        pass
    def printFraction(self):
        print 'Function Fraction::printFraction,print Fraction',self.__num,'/',self.__denom
        
    def setFractionToZero(self):
        self.__num=0
        self.__denom=1
        print 'Function Fraction::setfractionToZero, sets fraction to zero  '
        
def printFraction(f):
        print 'printFraction cannot print Fraction'
def setFractionToZero(f):
        #f.__num=0
        #f.__denom=1
        print 'setfractionToZero, sets fraction to zero  '

        
#variable declaration
aobj=Aclass()                    #object initialised
bobj=Bclass()
print 'Please enter values of numberator and denominator: '
n=input()      #user input
d=input()
f1=Fraction(n,d)

f1.printFraction()       #function call
f1.setFractionToZero()
print ''
aobj.printFraction(f1)
aobj.setFractionToZero(f1)
print ''
bobj.printFraction(f1)
bobj.setFractionToZero(f1)
print ''
printFraction(f1)
setFractionToZero(f1)
Please enter values of numberator and denominator: 
4
5
Function Fraction::printFraction,print Fraction 4 / 5
Function Fraction::setfractionToZero, sets fraction to zero  

Function AClass:: printFraction, prints Fraction:  0 / 1
Function AClass:: setfractionToZero, sets fraction to zero: 

Function BClass:: printFraction, prints Fraction:  0 / 1
Function BClass:: setfractionToZero CANNOT, CANT set fraction to 0 

printFraction cannot print Fraction
setfractionToZero, sets fraction to zero  

Program Source Code 10.4, page no: 312

In [11]:
class Employee:      #base class
    def whoAmI(self):
        pass
    
class Manager(Employee):      #derived class
    def whoAmI(self):
        return Manager
    
class Programmer(Employee):          #derived class
    def whoAmI(self):
        return Programmer
class Tester(Employee):               #derived class
    def whoAmI(self):
        return Tester
    
e=Employee()    #object of Employee()
e=[Employee() for j in range(5)]
e[0]=Programmer()
e[1]=Manager()
e[2]=Programmer()
e[3]=Tester()
e[4]=Tester()
for i in range(len(e)):
    print 'Employee ',i+1,'is: ',e[i].whoAmI().__name__
Employee  1 is:  Programmer
Employee  2 is:  Manager
Employee  3 is:  Programmer
Employee  4 is:  Tester
Employee  5 is:  Tester
In [ ]: