Chapter 15: Virtual Functions and Polymorphism

Example 15.1, Page Number: 358

In [2]:
class B_class:
    def __init__(self):
        self.author=None
    def put_author(self,s):
        self.author=s
    def show_author(self):
        print self.author
        
class D_class(B_class):
    def __init__(self):
        self.title=None
    def put_title(self,num):
        self.title=num
    def show_title(self):
        print "Title:",self.title
        
#Variable declaration
p=[B_class()]       #acts as a pointer to B_class type
B_ob=B_class()

dp=[D_class()]      #acts as a pointer to D_class type
D_ob=D_class()

p[0]=B_ob           #assigning p to object of base


#Access B_class via pointer
p[0].put_author("Tom Clancy")

#Access D_class via base pointer
p[0]=D_ob
p[0].put_author("William Shakespeare")

#Show that each author went into proper object
B_ob.show_author()
D_ob.show_author()
print "\n"

#Since put_title() and show_title() are not part of the base class, 
#they are not accessible via the base pointer p and must be accessed 
#either directly, or, as shown here, through a pointer to the 
#derived type
dp[0]=D_ob
dp[0].put_title("The Tempest")
p[0].show_author()
dp[0].show_title()
Tom Clancy
William Shakespeare


William Shakespeare
Title: The Tempest

Example 15.2, Page Number: 361

In [3]:
class base:
    def who(self):                #virtual function
        print "Base"

class first_d(base):
    def who(self):                #redifine who() relative to first_d
        print "First derivation"
        
class second_d(base):
    def who(self):                #redifine who() relative to second_d
        print "Second derivation"
 
        
#Variable declaration
base_obj=base()
p=[base()]
first_obj=first_d()
second_obj=second_d()

p[0]=base_obj
p[0].who()           #access base's who

p[0]=first_obj
p[0].who()           #access first_d's who

p[0]=second_obj
p[0].who()           #access second_d's who
Base
First derivation
Second derivation

Example 15.3, Page Number: 363

In [4]:
class base:
    def who(self):                #virtual function
        print "Base"

class first_d(base):
    def who(self):                #redifine who() relative to first_d
        print "First derivation"
        
class second_d(base):
    #who not defined
    pass
 
        
#Variable declaration
base_obj=base()
p=[base()]
first_obj=first_d()
second_obj=second_d()

p[0]=base_obj
p[0].who()           #access base's who

p[0]=first_obj
p[0].who()           #access first_d's who

p[0]=second_obj
p[0].who()           #access base's who because
                     #second_d does not redefine it.
Base
First derivation
Base

Example 15.4, Page Number: 364

In [5]:
class base:
    def who(self):                #virtual function
        print "Base"

class first_d(base):
    def who(self):                #redifine who() relative to first_d
        print "First derivation"
 
#second_d now inherited first_d -- not base
class second_d(first_d):
    #who not defined
    pass
 
        
#Variable declaration
base_obj=base()
p=[base()]
first_obj=first_d()
second_obj=second_d()

p[0]=base_obj
p[0].who()           #access base's who

p[0]=first_obj
p[0].who()           #access first_d's who

p[0]=second_obj
p[0].who()           #access first_d's who because
                     #second_d does not redefine it.
Base
First derivation
First derivation

Example 15.5, Page Number: 366

In [6]:
class figure:
    def __init__(self):
        self._x=None
        self._y=None
    def set_dim(self,i,j):
        self._x=i
        self._y=j
    def show_area(self):
        print "No area computation defined",
        print "for this class."
        
class triangle(figure):
    def show_area(self):
        print "Triangle with height",
        print self._x,"and base",self._y,
        print "has an area of",
        print self._x*0.5*self._y,"."
        
class rectangle(figure):
     def show_area(self):
        print "Rectangle with dimensions",
        print self._x,"x",self._y,
        print "has an area of",
        print self._x*self._y,"."
        
#Variable declaration
p=[figure()]                #pointer to base type
t=triangle()                #objects of derived type
r=rectangle()

p[0]=t
p[0].set_dim(10.0,5.0)
p[0].show_area()

p[0]=r
p[0].set_dim(10.0,5.0)
p[0].show_area()
        
Triangle with height 10.0 and base 5.0 has an area of 25.0 .
Rectangle with dimensions 10.0 x 5.0 has an area of 50.0 .

Example 15.6, Page Number: 368

In [7]:
class figure:
    def __init__(self):
        self._x=None
        self._y=None
    def set_dim(self,i,j=0):
        self._x=i
        self._y=j
    def show_area(self):
        print "No area computation defined",
        print "for this class."
        
class triangle(figure):
    def show_area(self):
        print "Triangle with height",
        print self._x,"and base",self._y,
        print "has an area of",
        print self._x*0.5*self._y,"."
        
class rectangle(figure):
     def show_area(self):
        print "Rectangle with dimensions",
        print self._x,"x",self._y,
        print "has an area of",
        print self._x*self._y,"."
        
class circle(figure):
     def show_area(self):
        print "Circle with radius",
        print self._x,
        print "has an area of",
        print 3.14*self._x*self._x,"."
        
        
#Variable declaration
p=[figure()]                #pointer to base type
t=triangle()                #objects of derived type
r=rectangle()
c=circle()

p[0]=t
p[0].set_dim(10.0,5.0)
p[0].show_area()

p[0]=r
p[0].set_dim(10.0,5.0)
p[0].show_area()

p[0]=c
p[0].set_dim(9.0)
p[0].show_area()
        
Triangle with height 10.0 and base 5.0 has an area of 25.0 .
Rectangle with dimensions 10.0 x 5.0 has an area of 50.0 .
Circle with radius 9.0 has an area of 254.34 .
In [ ]: