class queue:
#Initialize the queue
def __init__(self):
self.__sloc=0
self.__rloc=-1
self.__q=[]
#Put an integer into the queue
def qput(self,i):
if self.__sloc==100:
print "Queue is full."
return
self.__sloc+=1
self.__q.append(i)
#Get an integer from the queue
def qget(self):
if self.__rloc==self.__sloc:
print "Queue underflow"
return
self.__rloc+=1
return self.__q[self.__rloc]
#Create two queue objects
b=queue()
a=queue()
a.qput(10)
b.qput(19)
a.qput(20)
b.qput(1)
#Result
print "Contents of queue a: ",
print a.qget(),' ',a.qget()
print "Contents of queue b: ",
print b.qget(),' ',b.qget()
class myclass:
def __init__(self):
self.__a=None #private member
self.b=None #public member
#public functions
def setlab(self,i):
self.__a=i #refer to a
self.b=i*i #refer to b
return
def geta(self):
return self.__a #refer to a
def reset(self):
#call setlab using self
self.setlab(0) #the object is already known
ob=myclass()
ob.setlab(5) #set ob.a and ob.b
print "ob after setlab(5): ",ob.geta(),' ',
print ob.b #can access b because it is public
ob.b=20 #can access b because it is public
print "ob after ob.b=20: ",
print ob.geta(),' ',ob.b
ob.reset()
print "ob after ob.reset(): ",ob.geta(),' ',ob.b
class queue:
#Constructor
def __init__(self):
self.__q=[]
self.__rloc=-1
self.__sloc=0
print "Queue initialized"
#Destructor
def __del__(self):
print ("Queue destroyed")
#Put an integer into the queue
def qput(self,i):
if self.__sloc == 100:
print "Queue is full"
return
self.__sloc+=1
self.__q.append(i)
#Get an integer from the queue
def qget(self):
if self.__rloc==self.__sloc:
print "Queue underflow"
return
self.__rloc+=1
return self.__q[self.__rloc]
#Create two queue objects
a=queue()
a.qput(10)
a.qput(20)
b=queue()
b.qput(19)
b.qput(1)
#Result
print a.qget(),' ',
print a.qget(),' ',
print b.qget(),' ',
print b.qget(),' '
class queue:
#Constructor
def __init__(self,i):
self.__q=[]
self.__rloc=-1
self.__sloc=0
self.__who=i
print "Queue ",self.__who," initialized."
#Destructor
def __del__():
print "Queue ",self.__who," destroyed"
#Put an integer into the queue
def qput(self,i):
if self.__sloc == 100:
print "Queue is full"
return
self.__sloc+=1
self.__q.append(i)
#Get an integer from the queue
def qget(self):
if self.__rloc==self.__sloc:
print "Queue underflow"
return
self.__rloc+=1
return self.__q[self.__rloc]
a=queue(1)
b=queue(2)
a.qput(10)
b.qput(19)
a.qput(20)
b.qput(1)
#Result
print a.qget(),' ',
print a.qget(),' ',
print b.qget(),' ',
print b.qget(),' '
class widget:
#Pass two arguments to the constructor
def __init__(self,a,b):
self.__i=a
self.__j=b
def put_widget(self):
print self.__i," ",self.__j
#Initializing
x=widget(10,20)
y=widget(0,0)
x.put_widget()
y.put_widget()
class myclass:
#Constructor
def __init__(self,x):
self.a=x
#To get the vale of a
def get_a(self):
return self.a
#Initializing
ob=myclass(4)
#Result
print ob.get_a()
from ctypes import *
class c1(Structure):
_fields_=[("__i", c_int)] #private member
def get_i(self): #public finctions
return self.__i
def put_i(self,j):
self.__i=j
#Variable declaration
s=c1()
s.put_i(10)
#Result
print s.get_i()
class c1:
def __init__(self):
self.__i=None #private member
def get_i(self): #public finctions
return self.__i
def put_i(self,j):
self.__i=j
#Variable declaration
s=c1()
s.put_i(10)
#Result
print s.get_i()
from ctypes import *
#Creates a union
class u_type(Union):
_fields_ = [("i",c_short),
("ch", c_char*2)]
#Constructor
def __init__(self,a):
self.i=a
#Show the characters that comprise a short int.
def showchars(self):
print self.ch[0]
print self.ch[1]
u=u_type(1000)
#Displays char of 1000
u.showchars()
class c1:
def __init__(self):
self.__i=None
def get_i(self):
return self.i
def put_i(self,j):
self.i=j
#Variable declaration
s=c1()
s.put_i(10)
#Result
print s.get_i()
class c1:
def __init__(self):
self.__i=None
def get_i(self):
return self.i
def put_i(self,j):
self.i=j
#Variable declaration
s=c1()
s.put_i(10)
#Result
print s.get_i()
class display:
def __init__(self):
width=None
height=None
res=None
def set_dim(self,w,h):
self.width=w
self.height=h
def get_dim(self):
return self.width,self.height
def set_res(self,r):
self.res=r
def get_res(self):
return self.res
#Variable decleration
names=["low","medium","high"]
(low,medium,high)=(0,1,2) #For enumeration type
w=None
h=None
display_mode=[]*3
for i in range(3):
display_mode.append(display())
#Initialize the array of objects using member functions
display_mode[0].set_res(low)
display_mode[0].set_dim(640,480)
display_mode[1].set_res(medium)
display_mode[1].set_dim(800,600)
display_mode[2].set_res(high)
display_mode[2].set_dim(1600,1200)
#Result
print "Available display modes: "
for i in range(3):
print names[display_mode[i].get_res()]," : ",
w,h=display_mode[i].get_dim()
print w," by ",h
class samp:
__a=None
def __init__(self,n):
self.__a=n
def get_a(self):
return self.__a
#Initializing the list
sampArray=[samp(-1),samp(-2),samp(-3),samp(-4)]
#Display
for i in range(4):
print sampArray[i].get_a(),' ',
class samp:
__a=None
__b=None
def __init__(self,n,m):
self.__a=n
self.__b=m
def get_a(self):
return self.__a
def get_b(self):
return self.__b
#Initializing the list
sampArray=[[samp(1,2),samp(3,4)],
[samp(5,6),samp(7,8)],
[samp(9,10),samp(11,12)],
[samp(13,14),samp(15,16)]]
#Display
for i in range(4):
print sampArray[i][0].get_a(),' ',
print sampArray[i][0].get_b()
print sampArray[i][1].get_a(),' ',
print sampArray[i][1].get_b()
from ctypes import *
class P_example(Structure):
__num=None
def set_num(self,val):
self.__num=val
def show_num(self):
print self.__num
#Variable declaration
ob=P_example() #Declare an object to the structure
p=POINTER(P_example) #Declare a pointer to the structure
ob.set_num(1) #access ob directly
ob.show_num()
p=ob #assign p the address of ob
p.show_num() #access ob using pointer
class P_example(Structure):
__num=None
def set_num(self,val):
self.__num=val
def show_num(self):
print self.__num
#Variable declaration
ob=[P_example(),P_example()] #Declare an object to the structure
p=POINTER(P_example) #Declare a pointer to the structure
ob[0].set_num(10) #access objects directly
ob[1].set_num(20)
p=ob #obtain pointer to first element
p[0].show_num() #access ob using pointer
p[1].show_num()
p[0].show_num()