from ctypes import Structure, POINTER
class someclass(Structure):
data1=int
data2=chr
def __init__(self):
print 'Constructor someclass() is invoked'
self.data1=1
self.data2='A'
def __del__(self):
print 'Destructor ~someclass() is invoked'
def show(self):
print 'data1 =', self.data1,
print 'data2 =', self.data2
object1=someclass() #object of class someclass
ptr=POINTER(someclass) #pointer of type class someclass
ptr=object1 #pointer pointing to object of class someclass
print "Accessing object through object1.show()..."
object1.show()
print "Accessing object through ptr->show()..."
ptr.show()
class someclass(Structure):
data1=int
data2=chr
def __init__(self):
print 'Constructor someclass() is invoked'
self.data1=1
self.data2='A'
def __del__(self):
print 'Destructor ~someclass() is invoked'
def show(self):
print 'data1 =', self.data1,
print 'data2 =', self.data2
object1=someclass()
ptr=POINTER(someclass)
ptr=object1
print "Accessing object through object1.show()..."
ptr.show()
print "Destroying dynamic object..."
del ptr
from ctypes import POINTER, c_int
t1=POINTER(c_int)
t1=c_int(5)
t3=c_int(5)
t2=c_int(10)
t1.value=t1.value+t2.value
print "Sum of", t3.value,
print "and", t2.value,
print "is:", t1.value
from ctypes import Structure
class student(Structure):
__roll_no=int
__name=str
def setdata(self, roll_no_in, name_in):
self.__roll_no=roll_no_in
self.__name=name_in
def outdata(self):
print "Roll No =", self.__roll_no
print "Name =", self.__name
s1=student()
s1.setdata(1, "Savithri")
s1.outdata()
s2=student()
s2.setdata(2, "Bhavani")
s2.outdata()
s3=student()
s3.setdata(3, "Vani")
s4=s3
s3.outdata()
s4.outdata()
class student:
__roll_no=int
__name=str
def __init__(self, roll_no_in=None, name_in=None):
if isinstance(roll_no_in, int):
self.__roll_no=roll_no_in
if isinstance(name_in, str):
self.__name=name_in
else:
flag=raw_input("Do you want to initialize the object (y/n): ")
if flag=='y' or flag=='Y':
self.__roll_no=int(raw_input("Enter Roll no. of student: "))
Str=raw_input("Enter Name of student: ")
self.__name=Str
else:
self.__roll_no=0
self.__name=None
def __del__(self):
if isinstance(self.__name, str):
del self.__name
def Set(self):
student(roll_no_in, name_in)
def show(self):
if self.__roll_no:
print "Roll No:", self.__roll_no
else:
print "Roll No: (not initialized)"
if isinstance(self.__name, str):
print "Name: ", self.__name
else:
print "Name: (not initialized)"
s1=student()
s2=student()
s3=student(1)
s4=student(2, "Bhavani")
print "Live objects contents..."
s1.show()
s2.show()
s3.show()
s4.show()
del s1
del s2
del s3
del s4
class student:
__roll_no=int
__name=str
def setdata(self, roll_no_in, name_in):
self.__roll_no=roll_no_in
self.__name=name_in
def outdata(self):
print "Roll No =", self.__roll_no
print "Name =", self.__name
s=[]*10 #array of objects
count=0
for i in range(10):
s.append(student())
for i in range(10):
response=raw_input("Initialize student object (y/n): ")
if response=='y' or response=='Y':
roll_no=int(raw_input("Enter a Roll no. of student: "))
name=raw_input("Enter name of student: ")
s[i].setdata(roll_no, name)
count+=1
else:
break
print "Student Details..."
for i in range(count):
s[i].outdata()
class student(Structure):
__roll_no=int
__name=str
def setdata(self, roll_no_in, name_in):
self.__roll_no=roll_no_in
self.__name=name_in
def outdata(self):
print "Roll No =", self.__roll_no
print "Name =", self.__name
temp=[]*10
s=POINTER(student)
count=0
for i in range(10):
temp.append(student())
s=temp #pointer to array of objects
for i in range(10):
response=raw_input("Create student object (y/n): ")
if response=='y' or response=='Y':
roll_no=int(raw_input("Enter a Roll no. of student: "))
name=raw_input("Enter name of student: ")
s[i].setdata(roll_no, name)
count+=1
else:
break
print "Student Details..."
for i in range(count):
s[i].outdata()
class X:
__y=int
a=int
b=int
def init(self, z):
self.a=z
return z
obj=X()
ip=X.a #pointer to data member
obj.ip=10 #access through object
print "a in obj after obj.*ip = 10 is", obj.ip
pobj=[obj] #pointer to object of class X
pobj[0].ip=10 #access through object pointer
print "a in obj after pobj->*ip = 10 is", pobj[0].ip
ptr_init=X.init #pointer to member function
ptr_init(obj,5) #access through object
print "a in obj after (obj.*ptr_init)(5) =", obj.a
ptr_init(pobj[0],5) #access through object pointer
print "a in obj after (pobj->*ptr_init)(5) =", obj.a
class X():
__a=int
__b=int
def __init__(self):
self.__a=0
self.__b=0
def SetMembers(self, a1, b1):
self.__a=a1
self.__b=b1
def sum(objx):
pa=[X._X__a]
pb=[X._X__b]
objx.pa=objx._X__a
objx.pb=objx._X__b
return objx.pa+objx.pb
objx=X()
pfunc=X.SetMembers
pfunc(objx, 5, 6)
print "Sum =", sum(objx)
pobjx=[objx]
pfunc(pobjx[0], 7, 8)
print "Sum =", sum(objx)
from ctypes import pointer, c_int
def out_of_memory():
print "Memory exhausted, cannot allocate"
ip=pointer(c_int())
total_allocated=0L
print "Ok, allocating..."
while(1):
ip=[int]*100
total_allocated+=100L
print "Now got a total of", total_allocated, "bytes"
if total_allocated==29900L:
break
class Test:
__a=int
def setdata(self, init_a):
self.__a=init_a
print "Address of my object, this in setdata():", hex(id(self))
self.__a=init_a
def showdata(self):
print "Data accessed in normal way: ", self.__a
print "Address of my object, this in showdata(): ", hex(id(self))
print "Data accessed through this->a: ", self.__a
my=Test()
my.setdata(25)
my.showdata()
class List():
def __init__(self, dat=None):
if isinstance(dat, int):
self.__data=dat
else:
self.__data=0
self.__Next=None
def __del__(self):
pass
def get(self):
return self.__data
def insert(self, node):
last=List()
last=self
while(last.__Next!=None):
last=last.__Next
last.__Next=node
def display(first):
traverse=List()
print "List traversal yields:",
traverse=first
while(1):
print traverse._List__data, ",",
if traverse._List__Next==None:
break
traverse=traverse._List__Next
print ""
first=List()
first=None
while(1):
print "Linked List...\n1.Insert\n2.Display\n3.Quit\nEnter Choice: ",
choice=int(raw_input())
if choice==1:
data=int(raw_input("Enter data: "))
node=List(data)
if first==None:
first=node
else:
first.insert(node)
elif choice==2:
display(first)
elif choice==3:
break
else:
print "Bad Option Selected"
class dll:
def __init__(self, data_in=None):
if isinstance(data_in, int):
self.__data=data_in
else:
self.__data=0
self.__prev=None
self.__Next=None
def __del__(self):
pass
def get(self):
return self.__data
def insert(self, node):
last=dll()
last=self
while(last._dll__Next!=None):
last=last._dll__Next
node._dll__prev=last
node._dll__Next=None
last._dll__Next=node
def FreeAllNodes(self):
print "Freeing the node with data:",
first=dll()
first=self
while(1):
temp= dll()
temp=first
print "->", first._dll__data,
del temp
first=first._dll__Next
if first==None:
break
def display(first):
traverse=dll()
traverse=first
if first==None:
print "Nothing to display !"
return
else:
print "Processing with forward -> pointer:",
while(1):
print "->", traverse._dll__data,
if traverse._dll__Next==None:
break
traverse=traverse._dll__Next
print "\nProcessing with backward <- pointer:",
while(1):
print "->", traverse._dll__data,
if traverse._dll__prev==None:
break
traverse=traverse._dll__prev
print ""
def InsertNode(first, data):
node=dll(data)
if first==None:
first=node
else:
first.insert(node)
return first
first=dll()
first=None
print "Double Linked List Manipulation..."
while(1):
choice=int(raw_input("Enter Choice ([1] Insert, [2] Display, [3]Quit: "))
if choice==1:
data=int(raw_input("Enter data: "))
first=InsertNode(first, data)
elif choice==2:
display(first)
elif choice==3:
first.FreeAllNodes()
break
else:
print "Bad Option Selected"
class A:
__a1=int
def Set(self, val):
self.__a1=val
class B:
__b1=int
def Set(self, val):
self.__b1=val
def add(x, y):
return x._A__a1+y._B__b1
ObjA=A()
ObjB=B()
ObjA.Set(9)
ObjB.Set(10)
print "Sum of objects A and B using friend function =", add(ObjA, ObjB)
class test:
__data=int
def func(self, val):
self.__data=val
t1=test()
testptr={}
testptr[0]=test.func #pointer to member function
print "Initializing test class object t1 using pointer..."
testptr[0](t1, 10)
print "Object initialized successfully"