Chapter 8: Harnessing polymorphism

Example 8.1, Page No.134

In [10]:
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))
Base Class called by  0x12fd8e0
Base Class called by  0x12fd8d4

Example 8.2, Page No.136

In [18]:
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()
I am part of this family
None   I am the child
None
I am part of this family
None   I am the grandchild
None
I am part of this family
None   I am the parent
None
Grandchild has parent and grandparent
None

Example 8.3, Page No.138

In [23]:
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()
 Coo!Coo!
None
 A Pigeon flies away...
None
 Cluk!Cluk!
None
 I'm just a chicken- I can't fly!
None
 A Bird talks...
None
 A Bird flies...
None

Example 8.4, Page No. 140

In [1]:
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()
Coo!Coo!
Cluk!Cluk!
Error!-Program ended

Example 8.5, Page No.142

In [13]:
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()
X X X X X X X 

X X X X X X X 

X X X X X X X 

Area is: 21
Perimeter is: 20
X X X X X 

X X X X X 

X X X X X 

X X X X X 

X X X X X 

Area is: 25
Perimeter is: 20

Example 8.6, Page No.144

In [17]:
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()
Laser Classic:
Length: 35 ft
Height: 19 ft
Boom: 14ft

Example 8.7, Page No.147

In [4]:
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()
 ***SUM CALCULATOR***
Enter a number, an operator (+,-,*,/), and another number
Enter Zero to exit
>32
+
32
 64.0
>5.25
-
8.75
 -3.5
>8
*
3
 24.0
>20
/
5
 4.0
>20
/
0
 Cannot divide by zero
>0