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()
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_()
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()
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()
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()
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
'''
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)
'''
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")
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
'''
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()
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_()
'''
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()
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()