class Array:
def __init__(self,size=0): #constructor being called
print 'Constructor called'
self._length=size
if size>0:
self._arr=[]*self._length #size intialised
else:
self._length=0
self._arr=[]
def __del__(self): #destructor being called
print 'Destructor called'
#del self._arr
#Implementing the [] overloading
def op1(self,index):
if (index>=0 and index<self._length):
return self._arr[index]
else:
print 'Error: out of range'
return -1
#variable declaration
numbers = ["zero", "one", "two", "three", "four"]
integers=Array(5)
charptrs=Array(5)
integers=[None]*5
charptrs=[None]*5
for i in range(5):
integers[i]=i
charptrs[i]=numbers[i]
print 'The integers object has: ',
for i in range(5):
print integers[i],
print ''
print 'The Charptrs object has:',
for i in range(5):
print charptrs[i],
print ''
class Array:
def __init__(self,size=0): #constructor being called
print 'Constructor called'
self._length=size
if size>0:
self._arr=[]*self._length #size intialised
else:
self._length=0
self._arr=[]
def __del__(self): #destructor being called
print 'Destructor called'
#del self._arr
#Implementing the [] overloading
def op1(self,index):
if (index>=0 and index<self._length):
return self._arr[index]
else:
print 'Error: out of range'
return -1
def swap(a,b): #function defined
temp=a
a=b
b=temp
return a,b
def bubblesort(arrayobj,size): #function bubblesort
for i in range(size-1):
for j in range(i+1,size,+1):
if arrayobj[i] < arrayobj[j]:
arrayobj[i],arrayobj[j]=swap(arrayobj[i],arrayobj[j])
return arrayobj
#variable declaration
numbers = ["zero", "one", "two", "three", "four"]
integers=Array(5)
charptrs=Array(5)
integers=[None]*5
charptrs=[None]*5
for i in range(5):
integers[i]=i
charptrs[i]=numbers[i]
print 'The integers object has (before sorting): '
for i in range(5):
print integers[i],
print ''
print 'The Charptrs object has (before sorting):'
for i in range(5):
print charptrs[i],
print ''
bubblesort(integers,5)
charptrs=bubblesort(charptrs,5)
print ''
print 'The integers object has (after sorting): '
for i in range(5):
print integers[i],
print ''
print 'The Charptrs object has (after sorting): '
for i in range(5):
print charptrs[i],
class Array:
def __init__(self,size=0): #constructor being called
print 'Constructor called'
self._length=size
if size>0:
self._arr=[]*self._length #size intialised
else:
self._length=0
self._arr=[]
def __del__(self): #destructor being called
print 'Destructor called'
#del self._arr
#Implementing the [] overloading
def op1(self,index):
if (index>=0 and index<self._length):
return self._arr[index]
else:
print 'Error: out of range'
return -1
def less(a,b):
if a<b:
return True
else:
return false
def swap(a,b): #swap function
temp=a
a=b
b=temp
return a,b
def bubblesort(arrayobj,size): #bubblesort sort
for i in range(size-1):
for j in range(i+1,size,+1):
if arrayobj[i] < arrayobj[j]:
arrayobj[i],arrayobj[j]=swap(arrayobj[i],arrayobj[j])
return arrayobj
#variable declaration
numbers = ["zero", "one", "two", "three", "four"]
integers=Array(5)
charptrs=Array(5)
integers=[None]*5
charptrs=[None]*5
for i in range(5):
integers[i]=i
charptrs[i]=numbers[i]
print 'The integers object has (before sorting): '
for i in range(5):
print integers[i],
print ''
print 'The Charptrs object has (before sorting):'
for i in range(5):
print charptrs[i],
print ''
integrs=bubblesort(integers,5)
charptrs=bubblesort(charptrs,5)
print ''
print 'The integers object has (after sorting): '
for i in range(5):
print integers[i],
print ''
print 'The Charptrs object has (after sorting): '
for i in range(5):
print charptrs[i],
class Array:
def __init__(self,size=0): #constructor being called
print 'Constructor called'
self._length=size
if size>0:
self._arr=[]*self._length #size intialised
else:
self._length=0
self._arr=[]
def __del__(self): #destructor being called
print 'Destructor called'
#del self._arr
#Implementing the [] overloading
def op1(self,index):
if (index>=0 and index<self._length):
return self._arr[index]
else:
print 'Error: out of range'
return -1
#variable declaration
numbers = ["zero", "one", "two", "three", "four"]
integers=Array(5)
charptrs=Array(5)
integers=[None]*5
charptrs=[None]*5
for i in range(5):
integers[i]=i
charptrs[i]=numbers[i]
print 'The integers object has: ',
for i in range(5):
print integers[i],
print ''
print 'The Charptrs object has:',
for i in range(5):
print charptrs[i],
print ''
class Array:
def __init__(self,size=0): #constructor being called
self._length=size
if size>0:
self._arr=[]*self._length #size intialised
else:
self._length=0
self._arr=[]
def __del__(self): #destructor being called
del self._arr
#Implementing the [] overloading
def op1(self,index):
if (index>=0 and index<self._length):
return self._arr[index]
else:
print 'Error: out of range'
return -1
def op2(self,value):
self._arr.append(value)
def print1(self): #print function
for i in range(self._length):
print self._arr[i],
print ''
def bubblesort(self): #bubblesort function
for i in range(self._length-1):
for j in range(i+1,self._length,1):
if self._arr[i]<self._arr[j]:
self._arr[i],self._arr[j]=swap(self._arr[i],self._arr[j])
return self._arr
def swap(a,b): #function defined
temp=a
a=b
b=temp
return a,b
def change1(arr,size):
a=Array(size)
for j in range(size):
if arr.op1(0)=="zero":
a.op2(0)
if arr.op1(1)=="one":
a.op2(1)
if arr.op1(2)=="two":
a.op2(2)
if arr.op1(3)=="three":
a.op2(3)
if arr.op1(4)=="four":
a.op2(4)
return a
def change2(arr,size):
a=["" for j in range(size)]
for j in range(size):
if arr[4]==0:
a[4]="zero"
if arr[3]==1:
a[3]="one"
if arr[2]==2:
a[2]="two"
if arr[1]==3:
a[1]="three"
if arr[0]==4:
a[0]="four"
return a
#variable declaration
numbers = ["zero","one","two","three","four"]
integers=Array(5)
floats=Array(5)
charptrs=Array(5)
for i in range(5):
integers.op2(i)
floats.op2(i+0.5)
charptrs.op2(numbers[i])
Message="before sorting"
print "The integers(",Message,")...",
integers.print1()
print "The floats(",Message,")...",
floats.print1()
print "The charptrs(",Message,")...",
charptrs.print1()
print ''
Message ="being sorted"
print "The integers(",Message,")...",
print "The charptrs(",Message,")...",
print "The floats(",Message,")...",
print ''
print ''
Message ="after sorting"
print "The integers(",Message,")...",
integers=integers.bubblesort()
for j in range(5):
print integers[j],
print ''
print "The floats(",Message,")...",
floats=floats.bubblesort()
for j in range(5):
print floats[j],
print ''
print "The charptrs(",Message,")...",
charptrs=change1(charptrs,5)
charptrs=charptrs.bubblesort()
charptrs=change2(charptrs,5)
for j in range(5):
print charptrs[j],
(FALSE,TRUE)=(0,1)
def less(a,b):
if a<b:
return TRUE
else:
return FALSE
class Array:
def __init__(self,size=0): #constructor being called
self._length=size
if size>0:
self._arr=[]*self._length #size intialised
else:
self._length=0
self._arr=[]
def __del__(self): #destructor being called
del self._arr
#Implementing the [] overloading
def op1(self,index):
if (index>=0 and index<self._length):
return self._arr[index]
else:
print 'Error: out of range'
return -1
def op2(self,value):
self._arr.append(value)
def print1(self): #print function
for i in range(self._length):
print self._arr[i],
print ''
def bubblesort(self): #bubblesort function
for i in range(self._length-1):
for j in range(i+1,self._length,1):
if self._arr[i]<self._arr[j]:
self._arr[i],self._arr[j]=swap(self._arr[i],self._arr[j])
return self._arr
def swap(a,b): #function defined
temp=a
a=b
b=temp
return a,b
def change1(arr,size):
a=Array(size)
for j in range(size):
if arr.op1(0)=="zero":
a.op2(0)
if arr.op1(1)=="one":
a.op2(1)
if arr.op1(2)=="two":
a.op2(2)
if arr.op1(3)=="three":
a.op2(3)
if arr.op1(4)=="four":
a.op2(4)
return a
def change2(arr,size):
a=["" for j in range(size)]
for j in range(size):
if arr[4]==0:
a[4]="zero"
if arr[3]==1:
a[3]="one"
if arr[2]==2:
a[2]="two"
if arr[1]==3:
a[1]="three"
if arr[0]==4:
a[0]="four"
return a
#variable declarartion
numbers = ["zero","one","two","three","four"]
integers=Array(5)
floats=Array(5)
charptrs=Array(5)
for i in range(5):
integers.op2(i)
floats.op2(i+0.5)
charptrs.op2(numbers[i])
Message="before sorting"
print "The integers(",Message,")...",
integers.print1()
print "The floats(",Message,")...",
floats.print1()
print "The charptrs(",Message,")...",
charptrs.print1()
print ''
Message ="being sorted"
print "The integers(",Message,")...",
print "The charptrs(",Message,")...",
print "The floats(",Message,")...",
print ''
print ''
Message ="after sorting"
print "The integers(",Message,")...",
integers=integers.bubblesort()
for j in range(5):
print integers[j],
print ''
print "The floats(",Message,")...",
floats=floats.bubblesort()
for j in range(5):
print floats[j],
print ''
print "The charptrs(",Message,")...",
charptrs=change1(charptrs,5)
charptrs=charptrs.bubblesort()
charptrs=change2(charptrs,5)
for j in range(5):
print charptrs[j],
class Array:
def __init__(self,size=0): #constructor being called
print 'Constructor called'
self._length=size
if size>0:
self._arr=[]*self._length #size intialised
else:
self._length=0
self._arr=[]
def __del__(self): #desstructor being called
print 'Destructor called'
#del self._arr
#Implementing the [] overloading
def op1(self,index):
if (index>=0 and index<self._length):
return self._arr[index]
else:
print 'Error: out of range'
return -1
class stack(Array): #derived class
def __init__(self,Size): #constructor
Array.__init__(self,Size)
self.__sp=-1
self.__Maxsize=Size
def __del__(self): #destructor
pass
def push(self,data): #push function
if self.__sp==self.__Maxsize:
return False
self.__sp+=1
self._arr.append(data)
#return
def pop(self): #pop function
if self.__sp==-1:
return False
data=self._arr[self.__sp]
self.__sp-=1
return True
def print1(self):
if self.__sp==-1:
print 'stack empty'
for i in range(self.__sp+1):
print self._arr[i],
#variable declarartion
numbers = ["zero", "one", "two", "three", "four"]
integers=stack(5)
floats=stack(5)
charptrs=stack(5)
for i in range(5):
integers.push(i)
floats.push(i+0.5)
charptrs.push(numbers[i])
print 'the contents of integers stack(after five pushes):',
integers.print1()
print ''
print 'the contents of integers stack(after five pushes):',
floats.print1()
print ''
print 'the contents of integers stack(after five pushes):',
charptrs.print1()
print ''
integers.pop();integers.pop();integers.pop()
floats.pop();floats.pop();charptrs.pop()
print ''
print 'the contents of integers stack(after three pops):',
integers.print1()
print ''
print 'the contents of integers stack(after two pops):',
floats.print1()
print ''
print 'the contents of integers stack(after one pop):',
charptrs.print1()
print ''
MAXLENGTH=30 #size initialised
class MyException: #class declared
__Error=[None]*MAXLENGTH
def __init__(self,a,arg=None): #constructor
if arg==None:
print 'MyException constructor called'
#__Error[MAXLENGTH-1]='\0'
else:
print 'myException copy constructor called'
__Error=arg.__Error
def __del__(self): #destructor
print 'MyException desstructor called '
def __ShowError(self):
return __Error
class TestException: #class declared
def __init__(self):
print 'TestException constructor called '
def __del__(self):
print 'TestException desstructor called '
def TestFunc():
e=TestException()
print 'In TestFunc(). Throwing exception object MyException '
raise MyException('TestFunc(): Thrown exception')
print 'main program started... try block about to start '
try: #try block
print 'Inside main functions try block... calling TestFunc() '
TestFunc()
print 'Inside main functions try block... raising exception'
raise exception(1)
except MyException as E: #catch block
print 'Inside first catch handler: caught Myexception object '
#print E.__ShowError()
except Exception as code: #catch block
print 'Inside second catch handler: caught exception code '
print 'Main program returning.. outside try. catch blocks'
#enumeration type
(E, D, C, B, A) = (0,1,2,3,4)
def printGrade(arg): #function defined
if(arg==A):
print 'Grade A'
elif(arg==B):
print 'Grade B'
elif(arg==B):
print 'Grade C'
elif(arg==B):
print 'Grade D'
elif(arg==B):
print 'Grade E'
#variable declaration
X=100
Y=3
g=Y #static_cast
XbyY1=X/Y
XbyY2=(X)/Y #static_cast
print 'X/Y= ',XbyY1
print 'static cast<double> (X)/Y= ',XbyY2
print ''
print 'static cast<Grade> (',Y,')=>',
printGrade(g)
class X: #polymorphic class
def __init__(self):
self.__x=0
def __del__(self): #virtual destructor
pass
def vf1(): #virtual function
pass
class Y(X): #derived class
def __init__(self):
X.__init__(self)
def __del__(self):
pass
#try: #start a try block
try:
px1=[X()]
px2=[X()]
px1.append(X())
px2.append(X())
py1=[Y()]
py2=[Y()]
print 'Initially: px1= ',id(px1),
print ', px2= ',id(px2)
print ''
py1=px1
py2=px2
print 'Result of dynamic cast: '
print 'py1= px1 gets ',id(py1), ', py2=px2 gets ',id(py2)
print ''
py1=px1
py2=px2
print 'Result of static cast: '
print 'py1= px1 gets ',id(py1), ', py2=px2 gets ',id(py2)
except Exception,e: #catch block
print "Allocation failure."
class X: #base class
def __init__(self):
self.__x=0
def __del__(self):
pass
class Y(X): #derived class
def __init__(self):
X.__init__(self)
def __del__(self):
pass
xobj=X()
yobj=Y()
pxobj1=[X()]
pxobj2=[X()]
pxobj1[0]=X()
pxobj2[0]=X()
print 'type of xobj= ',xobj.__class__.__name__ #typeid(xobj).name()
print 'type of yobj= ',yobj.__class__.__name__ #typeid(yobj).name()
print 'type of pxobj1= ',pxobj1[0].__class__.__name__ #typeid(pxobj1).name()
print 'type of pxobj2= ',pxobj2[0].__class__.__name__ #typeid(pxobj2).name()