class Father:
__name=[chr]*20
def __init__(self, fname):
self.__name=fname
def show(self):
print "Father name:", self.__name
class Son(Father):
__name=[chr]*20
def __init__(self, sname, fname):
Father.__init__(self, fname)
self.__name=sname
def show(self):
print "Son name:", self.__name
fp=[Father]
f1=Father("Eshwarappa")
fp=f1
fp.show()
s1=Son("Rajkumar", "Eshwarappa")
fp=s1
Father.show(fp)
class Father():
__name=[chr]*20
def __init__(self, fname):
self.__name=fname
def show(self):
print "Father name:", self.__name
class Son(Father):
__name=[chr]*20
def __init__(self, sname, fname):
Father.__init__(self, fname)
self.__name=sname
def show(self):
print "Son name:", self.__name
fp=[Father]
f1=Father("Eshwarappa")
fp=f1
fp.show()
s1=Son("Rajkumar", "Eshwarappa")
fp=s1
fp.show()
class Father():
__f_age=int
def __init__(self, n):
self.__f_age=n
def GetAge(self):
return self.__f_age
class Son(Father):
__s_age=int
def __init__(self, n, m):
Father.__init__(self, n)
self.__s_age=m
def GetAge(self):
return self.__s_age
def son_func(self):
print "son's own function"
basep=[Father]
basep=Father(45)
print "basep points to base object..."
print "Father's Age:",
print basep.GetAge()
del basep
basep=[Son(45, 20)]
print "basep points to derived object..."
print "Son's Age:",
print Father.GetAge(basep[0])
print "By typecasting, ((Son*) basep)..."
print "Son's age:",basep[0].GetAge()
del basep
son1=Son(45, 20)
derivedp=[son1]
print "accessing through derived class pointer..."
print "Son's Age:",
print derivedp[0].GetAge()
class Father():
__f_age=int
def __init__(self, n):
self.__f_age=n
def GetAge(self):
return self.__f_age
class Son(Father):
__s_age=int
def __init__(self, n, m):
Father.__init__(self, n)
self.__s_age=m
def GetAge(self):
return self.__s_age
basep=[Father]
basep=Father(45)
print "Father's Age:",
print basep.GetAge()
del basep
basep=[Son(45, 20)]
print "Son's Age:",
print basep[0].GetAge()
del basep
class graphics:
def draw(self):
print "point"
class line(graphics):
def draw(self):
print "line"
class triangle(graphics):
def draw(self):
print "triangle"
class rectangle(graphics):
def draw(self):
print "rectangle"
class circle(graphics):
def draw(self):
print "circle"
point_obj=graphics()
line_obj=line()
tri_obj=triangle()
rect_obj=rectangle()
circle_obj=circle()
basep=[]
basep.append(point_obj)
basep.append(line_obj)
basep.append(tri_obj)
basep.append(rect_obj)
basep.append(circle_obj)
print "Following figures are drawn with basep[i]->draw()..."
for i in range(5):
basep[i].draw()
class AbsPerson:
def Service1(self, n):
self.Service2(n)
def Service2(self, n): #pure virtual function
pass
class Person(AbsPerson):
def Service2(self, n):
print "The number of years of service:", 58-n
Father=Person()
Son=Person()
Father.Service1(50)
Son.Service2(20)
class number:
__num=int
def getdata(self):
self.__num=int(raw_input("Enter an integer number: "))
def show(self): #pure virtual function
pass
class octnum(number):
def show(self):
print "Octal equivalent of", self._number__num,"=",oct(self._number__num)
class hexnum(number):
def show(self):
print "Hexadecimal equivalent of", self._number__num,"=",hex(self._number__num)
o1=octnum()
h1=hexnum()
o1.getdata()
o1.show()
h1.getdata()
h1.show()
class Father():
__f_name=str
def __init__(self, fname):
self.__f_name=fname
def __del__(self):
del self.__f_name
print "~Father() is invoked"
def show(self):
print "Father's name:", self.__f_name
class Son(Father):
__s_name=str
def __init__(self, sname, fname):
Father.__init__(self, fname)
self.__s_name=sname
def __del__(self):
del self.__s_name
print "~Son() is invoked"
Father.__del__(self)
def show(self):
print "Father's name:", self._Father__f_name
print "Son's name:", self.__s_name
basep=[Father]
basep=Father("Eshwarappa")
print "basep points to base object..."
basep.show()
del basep
basep=Son("Rajkumar", "Eshwarappa")
print "basep points to derived object..."
basep.show()
del basep
from ctypes import Structure, c_int, sizeof
class nonvirtual(Structure):
_fields_=[('x', c_int)]
def func(self):
pass
class withvirtual(Structure):
_fields_=[('x', c_int)]
def func(self):
pass
print "sizeof( nonvirtual ) =",sizeof(nonvirtual())
print "sizeof( withvirtual ) =",sizeof(withvirtual())
class description:
__information=str
def __init__(self, info):
self.__information=info
def show(self):
print self.__information,
class sphere(description):
__radius=float
def __init__(self, info, rad):
description.__init__(self, info)
self.__radius=rad
def show(self):
print self._description__information,
print "Radius = %g" %self.__radius
class cube(description):
__edge_length=float
def __init__(self, info, edg_len):
description.__init__(self, info)
self.__edge_length=edg_len
def show(self):
print self._description__information,
print "Edge Length = %g" %self.__edge_length
small_ball=sphere("mine", 1.0)
beach_ball=sphere("plane", 24.0)
plan_toid=sphere("moon", 1e24)
crystal=cube("carbon", 1e-24)
ice=cube("party", 1.0)
box=cube("card borad", 16.0)
shapes=[]
shapes.append(small_ball)
shapes.append(beach_ball)
shapes.append(plan_toid)
shapes.append(crystal)
shapes.append(ice)
shapes.append(box)
small_ball.show()
beach_ball.show()
plan_toid.show()
crystal.show()
ice.show()
box.show()
print "Dynamic Invocation of show()..."
for i in range(len(shapes)):
shapes[i].show()
class shape:
__val1=float
__val2=float
def getdata(self, a, b):
self.__val1=a
self.__val2=b
def display_area(self):
pass
class triangle(shape):
def display_area(self):
print "Area of trianle =", 0.5*self._shape__val1*self._shape__val2
class rectangle(shape):
def display_area(self):
print "Area of rectanle =", self._shape__val1*self._shape__val2
sptr=[shape]
sptr=triangle()
sptr.getdata(4.5, 2.2)
sptr.display_area()
sptr=rectangle()
sptr.getdata(4.5, 2.2)
sptr.display_area()