Chapter 12: Composition and Inheritance

Example 12.1, Page no: 273

In [1]:
class Person:
    def __init__(self,n="",nat="U.S.A.",s=1):
        self.name = n
        self.nationality = nat
        self.sex = s

    def printName(self):
        print  self.name,
    
    def printNationality(self):
        print  self.nationality,

creator = Person("Bjarne Stroustrup", "Denmark")
print "The creator of C++ was " ,
creator.printName() 
print ", who was born in " ,
creator.printNationality() 
The creator of C++ was  Bjarne Stroustrup , who was born in  Denmark

Example 12.2, Page no: 274

In [2]:
class Date:
    def __init__(self,m=0,d=0,y=0):
        self.month = m
        self.day = d
        self.year = y
        
    def setDate(self,m,d,y):
        self.month = m
        self.day = d
        self.year = y
    # Python doesn't have >> operator for input so we are just using input function
    def input(self):
        self.month = int(raw_input())        
        self.day = int(raw_input())
        self.year = int(raw_input())        
    
    # Python doesn't have << operator for output so we are just using print function
    def print_(self):
        monthName = ["", "January","February","March", "April", "May", "June",\
                         "July", "August","September", "October", "November",\
                             "December"]
        print monthName[self.month] , self.day , "," , self.year

peace =  Date(11,11,1918)
print "World War I ended on " ,
peace.print_()
peace.setDate(8,14,1945)
print "World War II ended on " ,
peace.print_()
print "Enter month, day, and year: "
date = Date()
date.input()
print "The date is " , 
date.print_()
World War I ended on  November 11 , 1918
World War II ended on  August 14 , 1945
Enter month, day, and year: 
7
4
1976
The date is  July 4 , 1976

Example 12.3, Page no: 274

In [3]:
class Date:
    def __init__(self,m=0,d=0,y=0):
        self.month = m
        self.day = d
        self.year = y
        
    def setDate(self,m,d,y):
        self.month = m
        self.day = d
        self.year = y
    # Python doesn't have >> operator for input so we are just using input function
    def input(self):
        self.month = int(raw_input())        
        self.day = int(raw_input())
        self.year = int(raw_input())        
    
    # Python doesn't have << operator for output so we are just using print function
    def print_(self):
        monthName = ["", "January","February","March", "April", "May", "June",\
                         "July", "August","September", "October", "November",\
                             "December"]
        print monthName[self.month] , self.day , "," , self.year

class Person:
    def __init__(self,n="",s=0,nat="U.S.A."):
        self.name = n
        self.nationality = nat
        self.sex = s
        self.dob = Date()
        self.dod = Date()
    def setDOB(self,m,d,y):
        self.dob.setDate(m,d,y)
    def setDOD(self,m,d,y):
        self.dod.setDate(m,d,y)
    def printName(self):
        print  self.name,
    def printNationality(self):
        print  self.nationality,
    def printDOB(self):
        self.dob.print_()
    def printDOD(self):
        self.dod.print_()

author = Person("Thomas Jefferson", 1)
author.setDOB(4,13,1743)
author.setDOD(7,4,1826)
print "The author of the Declaration of Independence was ",
author.printName()
print ".\nHe was born on ",
author.printDOB()
print " and died on ",
author.printDOD()
The author of the Declaration of Independence was  Thomas Jefferson .
He was born on  April 13 , 1743
 and died on  July 4 , 1826

Example 12.4, Page no: 276

In [4]:
class Date:
    def __init__(self,m=0,d=0,y=0):
        self.month = m
        self.day = d
        self.year = y
        
    def setDate(self,m,d,y):
        self.month = m
        self.day = d
        self.year = y
    # Python doesn't have >> operator for input so we are just using input function
    def input(self):
        self.month = int(raw_input())        
        self.day = int(raw_input())
        self.year = int(raw_input())        
    
    # Python doesn't have << operator for output so we are just using print function
    def print_(self):
        monthName = ["", "January","February","March", "April", "May", "June",\
                         "July", "August","September", "October", "November",\
                             "December"]
        print monthName[self.month] , self.day , "," , self.year

class Person:
    def __init__(self,n="",s=0,nat="U.S.A."):
        self.name = n
        self.nationality = nat
        self.sex = s
        self.dob = Date()
        self.dod = Date()
    def setDOB(self,m,d,y):
        self.dob.setDate(m,d,y)
    def setDOD(self,m,d,y):
        self.dod.setDate(m,d,y)
    def printName(self):
        print  self.name,
    def printNationality(self):
        print  self.nationality,
    def printDOB(self):
        self.dob.print_()
    def printDOD(self):
        self.dod.print_()

class Student(Person):
    def __init__(self,n,s=0,i=""):
        Person.__init__(self,n,s)
        self.id = i
        self.credits = 0
        self.gpa = 0
        self.dom = Date()

    def setDOM(self,m,d,y):
        self.dom.setDate(m, d, y)
    def printDOM(self):
        self.dom.print_()

x = Student("Ann Jones", 0, "219360061")
x.setDOB(5, 13, 1977)
x.setDOM(8, 29, 1995)
x.printName()
print "\n\t Born: " ,
x.printDOB()
print "\n\tMatriculated: ",
x.printDOM()
Ann Jones 
	 Born:  May 13 , 1977

	Matriculated:  August 29 , 1995

Example 12.5, Page no: 276

In [5]:
class Date:
    def __init__(self,m=0,d=0,y=0):
        self.month = m
        self.day = d
        self.year = y
        
    def setDate(self,m,d,y):
        self.month = m
        self.day = d
        self.year = y
    # Python doesn't have >> operator for input so we are just using input function
    def input(self):
        self.month = int(raw_input())        
        self.day = int(raw_input())
        self.year = int(raw_input())        
    
    # Python doesn't have << operator for output so we are just using print function
    def print_(self):
        monthName = ["", "January","February","March", "April", "May", "June",\
                         "July", "August","September", "October", "November",\
                             "December"]
        print monthName[self.month] , self.day , "," , self.year

class Person:
    def __init__(self,n="",s=0,nat="U.S.A."):
        self.name = n
        self.nationality = nat
        self.sex = s
        self.dob = Date()
        self.dod = Date()
    def setDOB(self,m,d,y):
        self.dob.setDate(m,d,y)
    def setDOD(self,m,d,y):
        self.dod.setDate(m,d,y)
    def printName(self):
        print  self.name,
    def printNationality(self):
        print  self.nationality,
    def printDOB(self):
        self.dob.print_()
    def printDOD(self):
        self.dod.print_()

class Student(Person):
    def __init__(self,n,s=0,i=""):
        Person.__init__(self,n,s)
        self.id = i
        self.credits = 0
        self.gpa = 0
        self.dom = Date()

    def setDOM(self,m,d,y):
        self.dom.setDate(m, d, y)
    def printDOM(self):
        self.dom.print_()
    def printSex(self):
        if self.sex == 1:
            print "male"
        else:
            print 'female'

x = Student("Ann Jones", 0, "219360061")
x.setDOB(5, 13, 1977)
x.setDOM(8, 29, 1995)
x.setDOD(7,4,1826)
x.printName()
print "\n\t Born: " ,  
x.printDOB()
print "\n\t Sex: " ,
x.printSex()
print "\n\tMatriculated: ",
x.printDOM()
Ann Jones 
	 Born:  May 13 , 1977

	 Sex:  female

	Matriculated:  August 29 , 1995

Example 12.6, Page no: 279

In [6]:
class X:
    def __init__(self):
        self.a = 0
    def f(self):
        print "X::f() executing"
class Y(X):
    def __init__(self):
        self.a = 0
    def f(self):
        print "Y::f() executing"
x = X()
x.a = 22
x.f()
print "x.a = " , x.a
y = Y()
y.a = 44
# assigns 44 to the a defined in Y
y._X__a = 66
# assigns 66 to the a defined in X
y.f()
# invokes the f() defined in Y
X.f(x)
# invokes the f() defined in X
print "y.a = " , y.a 
print "y._X__a = " , y._X__a 
z = y
print "z.a = " , z._X__a 
X::f() executing
x.a =  22
Y::f() executing
X::f() executing
y.a =  44
y._X__a =  66
z.a =  66

Example 12.7, Page no: 280

In [7]:
'''
Note : Python destuctor is called when program goes exit. So output may be differ than c.
'''
class X:
    def __init__(self):
        print "X::X() constructor executing "
    def __del__(self):
        print "X::X() destructor executing "

class Y(X):
    def __init__(self):
        X.__init__(self)
        print "Y::Y() constructor executing "
    def __del__(self):
        print "Y::Y() destructor executing "

class Z(Y):
    def __init__(self,i):
        Y.__init__(self)
        print "Z::Z(" , i , ") constructor executing "
    def __del__(self):
        print "Z::Z() destructor executing "
        

Z = Z(44)
X::X() constructor executing 
Y::Y() constructor executing 
Z::Z( 44 ) constructor executing 

Example 12.8, Page no: 281

In [8]:
'''
Note : Python destuctor is called when program goes exit. So output may be differ than c.
'''
class Person:
    def __init__(self,s):
        self.name = s
    def __del__(self):
        pass

class Student(Person):
    def __init__(self,s,m):
        Person.__init__(self,s)
        self.major = m
    def __del__(self):
        pass
x = Person("Bob")
y = Student("Sarah", "Biology")

Example 12.9, Page no: 282

In [9]:
class Person:
    def __init__(self,n="",s=0,nat="U.S.A."):
        self.name = n
        self.nationality = nat
        self.sex = s
        self.dob = Date()
        self.dod = Date()
    def setDOB(self,m,d,y):
        self.dob.setDate(m,d,y)
    def setDOD(self,m,d,y):
        self.dod.setDate(m,d,y)
    def printName(self):
        print  self.name,
    def printNationality(self):
        print  self.nationality,
    def printDOB(self):
        self.dob.print_()
    def printDOD(self):
        self.dod.print_()
    def setHSgraduate(self,g):
        self.hs = g
    def isHSgraduate(self):
        return hs

Example 12.10, Page no: 283

In [10]:
'''
Note : By default all methods in python are virtual. so output would be differ than c.
'''
class X:
    def f(self):
        print "X::f() executing"
class Y(X):
    def f(self):
        print "Y::f() executing"
x = X()
y = Y()
p = x
p.f()
p = y
p.f()
X::f() executing
Y::f() executing

Example 12.11, Page no: 284

In [11]:
class Person:
    def __init__(self,n):
        self.name = n
    def print_(self):
        print 'My name is' , self.name

class Student(Person):
    def __init__(self,s,g):
        Person.__init__(self,s)
        self.gpa = g
    def print_(self):
        print 'My name is ',self.name, ' and my G.P.A. is', self.gpa

class Professor(Person):
    def __init__(self,s,n):
        Person.__init__(self,s)
        self.publs = n
    def print_(self):
        print 'My name is ', self.name,' and i have ' , self.publs,' publications.'

x = Person("Bob")
p = x
p.print_()
y = Student("Tom", 3.47)
p = y
p.print_()
z = Professor("Ann", 7)
p = z
p.print_()
My name is Bob
My name is  Tom  and my G.P.A. is 3.47
My name is  Ann  and i have  7  publications.

Example 12.12, Page no: 285

In [13]:
'''
This program is similar to Example 12.6:
'''
class X:
    def __init__(self):
        self.p = [0,0]
        print 'X().',
    def __del__(self):
        print '~X().'

class Y(X):
    def __init__(self):
        X.__init__(self)
        self.q = []
        for i in range(1023):
            self.q.append(0)
        print 'Y() : Y::q = ', hex(id(self.q)) ,'.',
    def __del__(self):
        print '~Y().'

for i in range(8):
    r = Y()
X(). Y() : Y::q =  0x90d91cc . ~Y().
X(). Y() : Y::q =  0x90c944c . ~Y().
X(). Y() : Y::q =  0x90d91cc . ~Y().
X(). Y() : Y::q =  0x90c944c . ~Y().
X(). Y() : Y::q =  0x90d91cc . ~Y().
X(). Y() : Y::q =  0x90c944c . ~Y().
X(). Y() : Y::q =  0x90d91cc . ~Y().
X(). Y() : Y::q =  0x90c944c . ~Y().

Example 12.13, Page no: 288

In [14]:
class Media:
    def __init__(self):
        self.title = ''
    def print_(self):
        pass
    def id(self):
        pass

class Book(Media):
    def __init__(self,a='',t="",p="",i=""):
        self.author = a
        self.publisher = p
        self.isbn = i
        self.title = t
    def print_(self):
        print self.title , " by " , self.author
    def id(self):
        return self.isbn

class CD(Media):
    def __init__(self,t="",c="",m="",n=""):
        self.composer = c
        self.make = m
        self.number = n
        self.title = t
    def print_(self):
        print self.title , ", " , self.composer
    def id(self):
        s = str(self.make) + ' ' + str(self.number)
        return s

class Magazine(Media):
    def __init__(self,t="",i="",v=0, n=0):
        self.issn = i
        self.volume = v
        self.number = n
        self.title = t
    def print_(self):
        print self.title , " Magazine, Vol. ", self.volume , ", No." , self.number
    def id(self):
        return self.issn

book = Book("Bjarne Stroustrup", "The C++ Programming Language","Addison-Wesley", "0-201-53992-6")
magazine = Magazine("TIME", "0040-781X", 145, 23)
cd = CD("BACH CANTATAS", "Johann Sebastian Bach","ARCHIV", "D120541")
book.print_()
print "\tid: " , book.id() 
magazine.print_()
print "\tid: " , magazine.id() 
cd.print_()
print "\tid: " , cd.id()
The C++ Programming Language  by  Bjarne Stroustrup
	id:  0-201-53992-6
TIME  Magazine, Vol.  145 , No. 23
	id:  0040-781X
BACH CANTATAS ,  Johann Sebastian Bach
	id:  ARCHIV D120541