class myclass:
pass
#Variable declaration
i=j=0
f=0.0
ob=myclass()
print "The type of i is:",type(i).__name__
print "The type of f is:",type(f).__name__
print "The type of ob is:",ob.__class__.__name__
print "\n"
if type(i)==type(j):
print "The types of i and j are the same"
if not(type(i)==type(f)):
print "The types of i and f are not the same"
class Base:
pass
class Derived1(Base):
pass
class Derived2(Base):
pass
#Variable declaration
baseob=Base()
p=[Base()]
ob1=Derived1()
ob2=Derived2()
p[0]=baseob
print "p is pointing to an object of type",
print p[0].__class__.__name__
p[0]=ob1
print "p is pointing to an object of type",
print p[0].__class__.__name__
p[0]=ob2
print "p is pointing to an object of type",
print p[0].__class__.__name__
class Base:
pass
class Derived1(Base):
pass
class Derived2(Base):
pass
def WhatType(ob):
print "ob is referencing an object of type",
print ob.__class__.__name__
#Variable declaration
baseob=Base()
p=[Base()]
ob1=Derived1()
ob2=Derived2()
WhatType(baseob)
WhatType(ob1)
WhatType(ob2)
import random
class figure:
def __init__(self,i,j):
self._x=i
self._y=j
class triangle(figure):
def __init__(self,i,j):
figure.__init__(self,i,j)
def area(self):
return self._x*0.5*self._y
class rectangle(figure):
def __init__(self,i,j):
figure.__init__(self,i,j)
def area(self):
return self._x*self._y
class circle(figure):
def __init__(self,i,j=0):
figure.__init__(self,i,j)
def area(self):
return self._x*self._x*3.14
def factory():
i=random.randint(0,2)
if i==0:
return circle(10.0)
elif i==1:
return triangle(10.1,5.3)
elif i==2:
return rectangle(4.3,5.7)
t=c=r=0
p=[None]
#generate and count objects
for i in range(10):
p[0]=factory() #generate an object
print "Object is ",p[0].__class__.__name__,". ",
#count it
if p[0].__class__.__name__==triangle.__name__:
t+=1
if p[0].__class__.__name__==rectangle.__name__:
r+=1
if p[0].__class__.__name__==circle.__name__:
c+=1
#display its area
print "Area is",p[0].area()
print "Objects generated:"
print "Triangles:",t
print "Rectangles:",r
print "Circles:",c
class myclass:
def __init__(self,i):
self.__a=i
o1=myclass(10)
o2=myclass(9)
o3=myclass(7.2)
print "Type of o1 is",o1.__class__.__name__
print "Type of o2 is",o2.__class__.__name__
print "Type of o3 is",o3.__class__.__name__
print
if o1.__class__.__name__==o2.__class__.__name__:
print "o1 and o2 are the same type"
if o1.__class__.__name__==o3.__class__.__name__:
print "Error"
else:
print "o1 and o3 are different types"
#This prints error because python doesnt use templates.
import random
class figure:
def __init__(self,i,j):
self._x=i
self._y=j
class triangle(figure):
def __init__(self,i,j):
figure.__init__(self,i,j)
def area(self):
return self._x*0.5*self._y
class rectangle(figure):
def __init__(self,i,j):
figure.__init__(self,i,j)
def area(self):
return self._x*self._y
class circle(figure):
def __init__(self,i,j=0):
figure.__init__(self,i,j)
def area(self):
return self._x*self._x*3.14
def factory():
i=random.randint(0,2)
if i==0:
return circle(10.0)
elif i==1:
return triangle(10.1,5.3)
elif i==2:
return rectangle(4.3,5.7)
t=c=r=0
p=[None]
#generate and count objects
for i in range(10):
p[0]=factory() #generate an object
print "Object is ",p[0].__class__.__name__,". ",
#count it
if p[0].__class__.__name__==triangle.__name__:
t+=1
if p[0].__class__.__name__==rectangle.__name__:
r+=1
if p[0].__class__.__name__==circle.__name__:
c+=1
#display its area
print "Area is",p[0].area()
print "Objects generated:"
print "Triangles:",t
print "Rectangles:",r
print "Circles:",c
class Base:
def f(self):
print "Inside Base"
class Derived(Base):
def f(self):
print "Inside Derived"
bp=[Base()] #pointer to base
b_ob=Base()
dp=[Derived()] #pointer to derived
d_ob=Derived()
dp[0]=d_ob
if dp[0]:
print "Cast from Derived * to Derived * OK."
dp[0].f()
else:
print "Error"
print
bp[0]=d_ob
if bp[0]:
print "Cast from Derived * to Base * OK."
bp[0].f()
else:
print "Error"
print
bp[0]=b_ob
if bp[0]:
print "Cast from Base * to Base * OK."
bp[0].f()
else:
print "Error"
print
dp[0]=b_ob
if dp[0]:
print "Error"
else:
print "Cast from Base * to Derived * not OK."
print
bp[0]=d_ob #bp points to Derived object
dp[0]=bp[0]
if dp[0]:
print "Cast bp to a Derived * OK."
print "because bp is really pointing\n",
print "to a Derived object."
dp[0].f()
else:
print "Error"
print
bp[0]=b_ob #bp points to Base object
dp[0]=bp[0]
if dp[0]:
print "Error"
else:
print "Now casting bp to a Derived *\n",
print "is not OK because bp is really\n",
print " pointing to a Base object."
print
dp[0]=d_ob #dp points to Derived object
bp[0]=dp[0]
if bp[0]:
print "Casting dp to a Base * is OK."
bp[0].f()
else:
print "Error"
print
class Base:
def f(self):
pass
class Derived(Base):
def derivedOnly(self):
print "Is a Derived Object"
bp=[Base()] #pointer to base
b_ob=Base()
dp=[Derived()] #pointer to derived
d_ob=Derived()
#Use typeid
bp[0]=b_ob
if bp[0].__class__.__name__==Derived.__name__:
dp[0]=bp[0]
dp[0].derivedOnly()
else:
print "Cast from Base to Derived failed."
bp[0]=d_ob
if bp[0].__class__.__name__==Derived.__name__:
dp[0]=bp[0]
dp[0].derivedOnly()
else:
print "Error, cast should work!"
#Use dynamic_cast
bp[0]=b_ob
dp[0]=bp[0]
if dp[0].__class__.__name__==Derived.__name__:
dp[0].derivedOnly()
else:
print "Cast from Base to Derived failed."
bp[0]=d_ob
dp[0]=bp[0]
if dp:
dp[0].derivedOnly()
else:
print "Error, cast should work!"
def f(p):
v=p
v[0]=100
#Variable declaration
x=[]
x.append(99)
print "x before call:",x[0]
f(x)
print "x after call:",x[0]
f=199.22
i=f
print i
i=0 #int
p="This is a string"
i=p #cast pointer to integer
#Result
print i