class Base:
def Identify(self,adr):
print "Base Class called by ",hex(adr)
class SubA(Base):
def __init__(self,adr):
Base.Identify(self,adr)
class SubB(Base):
def __init__(self,adr):
Base.Identify(self,adr)
ptrA=1
ptrB=SubA(id(ptrA))
ptrC=2
ptrc=SubB(id(ptrC))
class Parent:
def Common(self):
print "I am part of this family"
def identify(self):
print "I am the parent"
class Child(Parent):
def identify(self):
print "I am the child"
class Grandchild(Child):
def identify(self):
print "I am the grandchild"
def Relate(self):
print "Grandchild has parent and grandparent"
son=Child()
grandson=Grandchild()
parent=Parent()
print parent.Common()," ",son.identify()
print parent.Common()," ",grandson.identify()
print parent.Common()," ",parent.identify()
print grandson.Relate()
class Bird:
def Talk(self):
print "A Bird talks..."
def Fly(self):
print "A Bird flies..."
class Pigeon(Bird):
def Talk(self):
print "Coo!Coo!"
def Fly(self):
print "A Pigeon flies away..."
class Chicken(Bird):
def Talk(self):
print "Cluk!Cluk!"
def Fly(self):
print "I'm just a chicken- I can't fly!"
pPigeon=Pigeon()
pChicken=Chicken()
pBird=Bird()
print "",pPigeon.Talk()
print "",pPigeon.Fly()
print "",pChicken.Talk()
print "",pChicken.Fly()
print "",pBird.Talk()
print "",pBird.Fly()
class Bird:
def Talk(self):
return -1
def Fly(self):
return -1
class Pigeon(Bird):
def Talk(self):
print "Coo!Coo!"
def Fly(self):
print "A Pigeon flies away..."
class Chicken(Bird):
def Talk(self):
print "Cluk!Cluk!"
def Fly(self):
print "I'm just a chicken- I can't fly!"
pPegion=Pigeon()
pChicken=Chicken()
pPegion.Talk()
pChicken.Talk()
pBird=Bird()
data=pBird.Talk()
if data==-1:
print "Error!-Program ended"
exit()
class Rect:
height=0
width=0
def __init__(self,initWidth,initHeight):
self.height=initHeight
self.width=initWidth
def getArea(self):
return self.height*self.width
def getEdge(self):
return ((2*self.height)+(2*self.width))
def Draw(self):
for i in range(0,self.height):
for j in range(0,self.width):
print "X",
print "\n"
pQuad=Rect(7,3)
pSquare=Rect(5,5)
pQuad.Draw()
print "Area is:",pQuad.getArea()
print "Perimeter is:",pQuad.getEdge()
pSquare.Draw()
print "Area is:",pSquare.getArea()
print "Perimeter is:",pSquare.getEdge()
class Boat:
length=0
def getLength(self):
return self.length
class Sailboat(Boat):
mast=0
def getMast(self):
return self.mast
class Laser(Sailboat):
def __init__(self):
self.mast=19
self.length=35
def Model(self):
print "Laser Classic:"
def Boom(self):
print "Boom: 14ft"
pLaser=Laser()
pLaser.Model()
print "Length:",pLaser.getLength(),"ft"
print "Height:",pLaser.getMast(),"ft"
pLaser.Boom()
class Calculator:
status="true"
num1=0.0
num2=0.0
oper='+'
def __init__(self):
self.status="true"
def launch(self):
print "***SUM CALCULATOR***"
print "Enter a number, an operator (+,-,*,/), and another number\nEnter Zero to exit"
def readInput(self):
print ">",
self.num1=float(raw_input())
if self.num1==0:
self.status="false"
else:
self.oper=raw_input()
self.num2=float(raw_input())
def writeOutput(self):
if self.status=="true":
if self.oper=='+':
print self.num1+self.num2
elif self.oper=='-':
print self.num1-self.num2
elif self.oper=='*':
print self.num1*self.num2
elif self.oper=='/':
if self.num2!=0:
print self.num1/self.num2
else:
print "Cannot divide by zero"
def run(self):
return self.status
pCalc=Calculator()
pCalc.launch()
while (pCalc.run()=="true"):
pCalc.readInput()
pCalc.writeOutput()