class myclass:
__a=None
__b=None
def __init__(self,i,j):
self.__a=i
self.__b=j
def sum(self,x): #Friend function
return sum1(x)
def sum1(x):
return x._myclass__a +x._myclass__b #accessing private members
#Variable declaration
n=myclass(3,4)
#Result
print n.sum(n)
class c1:
__status=None
def set_status(self,state):
self.__status=state
class c2:
__status=None
def set_status(self,state):
self.status=state
#Friend function
def idle(a,b):
if a._c1__status or b._c2__status :
return 0
else:
return 1
#variable declarations
def IDLE(): #Constants
return 0
def INUSE():
return 1
x=c1()
y=c2()
x.set_status(IDLE())
y.set_status(IDLE())
if idle(x,y):
print "Screen Can Be Used."
x.set_status(INUSE())
if idle(x,y):
print "Screen Can Be Used."
else:
print "Pop-up In Use."
def IDLE():
return 0
def INUSE():
return 1
class c1:
__status=None
def set_status(self,state):
self.__status=state
def idle(self,b): #now a member of c1
if self.__status or b._c2__status :
return 0
else:
return 1
class c2:
__status=None #IDLE if off INUSE if on screen
def set_status(self,state):
self.status=state
#Variable declarations
x=c1()
y=c2()
x.set_status(IDLE())
y.set_status(IDLE())
if idle(x,y):
print "Screen Can Be Used."
x.set_status(INUSE())
if idle(x,y):
print "Screen Can Be Used."
else:
print "Pop-up In Use."
import time,string
class timer:
__seconds=None
def __init__(self,t1,t2=None):
if t2==None:
if isinstance(t1,int): #seconds specified as an integer
self.__seconds=t1
else: #seconds specified as a string
self.__seconds=string.atoi(t1)
else: #time in minutes and seconds
self.__seconds=t1*60+t2
def run(self):
t1=time.clock()
while (time.clock()-t1)<self.__seconds:
a=10
print time.clock()-t1
a=timer(10)
b=timer("20")
c=timer(1,10)
a.run() #count 10 seconds
b.run() #count 20 seconds
c.run() #count 1 minute,10 seconds
import time,string
class timer:
__seconds=None
def __init__(self,t1,t2=None):
if t2==None:
if isinstance(t1,int): #seconds specified as an integer
self.__seconds=t1
else: #seconds specified as a string
self.__seconds=string.atoi(t1)
else: #time in minutes and seconds
self.__seconds=t1*60+t2
def run(self):
t1=time.clock()
while (time.clock()-t1)<self.__seconds:
a=10
print time.clock()-t1
a=timer(10)
a.run()
print "Enter number of seconds: "
str="20"
b=timer(str) #initialize at the run time
c.run()
print "Enter minutes and seconds: "
min=1
sec=10
c=timer(min,sec) #initialize at the run time
c.run()
class myclass:
__a=None #private members
__b=None
def setab(self,i,j): #publc functons
self.__a=i
self.__b=j
def showab(self):
print "a is ",self.__a
print "b is ",self.__b
#Variable declaration
ob1 = myclass()
ob2 = myclass()
#Intalizing
ob1.setab(10,20)
ob2.setab(0,0)
print "ob1 before assignment: "
ob1.showab()
print "ob2 before assignment: "
ob2.showab()
ob2 = ob1 #assign ob1 to ob2
#Result
print "ob1 after assignment: "
ob1.showab()
print "ob2 after assignment: "
ob2.showab()
from copy import deepcopy
class OBJ:
def set_i(self,x):
self.__i=x
def out_i(self):
print self.__i,
def f(x):
x=deepcopy(x)
x.out_i() #outputs 10
x.set_i(100) #this affects only local copy
x.out_i() #outputs 100
#Variable declaration
o=OBJ()
o.set_i(10)
f(o)
o.out_i() #still outputs 10, value of i unchanged
class myclass:
def __init__(self,i):
self.__val=i
print "Constructing"
def __del__(self):
print "Destructing"
def getval(self):
return self.__val
def display(ob):
print ob.getval()
#Varable declaration
a=myclass(10)
display(a)
from ctypes import *
class myclass:
def __init__(self,i):
print "Allocating p"
self.p=pointer(c_int(i))
def __del__(self):
print "Freeing p"
def getval(self):
return self.p[0]
def display(ob):
print ob.getval()
#Variable declaration
a=myclass(10)
display(a)
from ctypes import *
class myclass:
def __init__(self,i):
print "Allocating p"
self.p=pointer(c_int(i))
def __del__(self):
print "Freeing p"
def getval(self):
return self.p[0]
def display(ob):
print ob[0].getval()
#Variable declaration
a=[]
a.append(myclass(10))
display(a)
class sample:
__s=None
def show(self):
print self.__s
def set(self,str):
self.__s=str
#Return an object of type sample
def input():
str=sample()
instr = "Hello" #User input
str.set(instr)
return str
#Variable declaration
ob=sample()
#assign returned object to ob
ob=input()
#Result
ob.show()
class sample:
__s=None
def __init__(self):
self.__s=0
def __del__(self):
print "Freeing p"
def show(self):
print self.__s
def set(self,str):
self.__s=str
#This function takes one object parameter
def input():
str=sample()
instr="Hello" #User input
str.set(instr)
return str
#Variable declaration
ob=sample()
#assign returned object to ob
ob=input()
#Result
ob.show()
class myclass:
__p=None
def __init__(self,i):
if isinstance(i,int):
print "Allocating p"
self.__p=i
else:
print "Copy constructor called"
self.__p=i.getval()
def __del__(self):
print "Freeing p"
def getval(self):
return self.__p
#This function takes one object parameter
def display(ob):
print ob.getval()
#Variable declaration
ob=myclass(10)
#Result
display(ob)
class myclass:
__p=None
def __init__(self,i):
if isinstance(i,int):
print "Allocating p"
self.__p=i
else:
print "Copy constructor called"
self.__p=i.getval()
def __del__(self):
print "Freeing p"
def getval(self):
return self.__p
#Variable declaration
a=myclass(10) #calls normal constructor
b=myclass(a) #calls copy constructor
class myclass:
def __init__(self,i=0):
if isinstance(i,int):
print "Normal constructor"
else:
print "Copy constructor"
#Variable declaration
a=myclass() #calls normal constructor
f=myclass()
a=myclass(f) #Invoke copyconstructor
class c1:
def __init__(self):
self.__i=None
def load_i(self,val):
self.__i=val
def get_i(self):
return self.__i
#Variable declaration
o=c1()
o.load_i(100)
#Result
print o.get_i()