class three_d:
def __init__(self,i=None,j=None,k=None):
if i==None:
self.x=self.y=self.z=0
else:
self.x=i
self.y=j
self.z=k
#Overload +
def __add__(self,op2):
temp=three_d()
temp.x=self.x + op2.x #These are integer additions
temp.y=self.y + op2.y #and the + retains its original
temp.z=self.z + op2.z #meaning relative to them.
return temp
#Overload assignment
def __assign__(self,op2):
self.x=op2.x #These are integer assignments
self.y=op2.y #and the = retains its original
self.z=op2.z #meaning relative to them
return self
#Show x,y,z coordinates
def show(self):
print self.x,",",self.y,",",self.z
#Variable declaration
a=three_d(1,2,3)
b=three_d(10,10,10)
c=three_d()
a.show()
b.show()
#add a and b together
c=a+b
c.show()
#add a,b and c together
c=a+b+c
c.show()
#demonstrate multiple assignment
c=b=a
c.show()
b.show()
class three_d:
def __init__(self,i=None,j=None,k=None):
if i==None:
self.x=self.y=self.z=0
else:
self.x=i
self.y=j
self.z=k
#Overload +
def __add__(self,op2):
temp=three_d()
temp.x=self.x + op2.x #These are integer additions
temp.y=self.y + op2.y #and the + retains its original
temp.z=self.z + op2.z #meaning relative to them.
return temp
#Overload assignment
def __assign__(self,op2):
self.x=op2.x #These are integer assignments
self.y=op2.y #and the = retains its original
self.z=op2.z #meaning relative to them
return self
#Overload the increment operator
def __iadd__(self,op2):
self.x+=op2
self.y+=op2
self.z+=op2
return self
#Show x,y,z coordinates
def show(self):
print self.x,",",self.y,",",self.z
a=three_d(1,2,3)
b=three_d(10,10,10)
c=three_d()
a.show()
b.show()
#add a and b together
c=a+b
c.show()
#add a,b and c together
c=a+b+c
c.show()
#demonstrate multiple assignment
c=b=a
c.show()
b.show()
#Increment c
c+=1
c.show()
class three_d:
def __init__(self,i=None,j=None,k=None):
if i==None:
self.x=self.y=self.z=0
else:
self.x=i
self.y=j
self.z=k
#Overload +
def __add__(self,op2):
temp=three_d()
temp.x=self.x + op2.x #These are integer additions
temp.y=self.y + op2.y #and the + retains its original
temp.z=self.z + op2.z #meaning relative to them.
return temp
#Overload assignment
def __assign__(self,op2):
self.x=op2.x #These are integer assignments
self.y=op2.y #and the = retains its original
self.z=op2.z #meaning relative to them
return self
#Overload the increment operator
def __iadd__(self,op2):
self.x+=op2
self.y+=op2
self.z+=op2
return self
#Show x,y,z coordinates
def show(self):
print self.x,",",self.y,",",self.z
a=three_d(1,2,3)
b=three_d(10,10,10)
c=three_d()
a.show()
b.show()
#add a and b together
c=a+b
c.show()
#add a,b and c together
c=a+b+c
c.show()
#demonstrate multiple assignment
c=b=a
c.show()
b.show()
#Increment c (prefix)
c+=1
c.show()
#Increment c (postfix)
c+=1
c.show()
#Implementing prefix
c+=1
a=c
a.show()
c.show()
#Implementing postfix
a=c
a.show()
c+=1
c.show()
class three_d:
def __init__(self,i=None,j=None,k=None):
if i==None:
self.x=self.y=self.z=0
else:
self.x=i
self.y=j
self.z=k
#Overload +
def __add__(self,op2):
return add(self,op2)
#Overload assignment
def __assign__(self,op2):
self.x=op2.x #These are integer assignments
self.y=op2.y #and the = retains its original
self.z=op2.z #meaning relative to them
return self
#Show x,y,z coordinates
def show(self):
print self.x,",",self.y,",",self.z
#friending the funcion
def add(op1,op2):
temp=three_d()
temp.x=op1.x + op2.x #These are integer additions
temp.y=op1.y + op2.y #and the + retains its original
temp.z=op1.z + op2.z #meaning relative to them.
return temp
a=three_d(1,2,3)
b=three_d(10,10,10)
c=three_d()
a.show()
b.show()
#add a and b together
c=a+b
c.show()
#add a,b and c together
c=a+b+c
c.show()
#demonstrate multiple assignment
c=b=a
c.show()
b.show()
class CL:
def __init__(self):
self.count=0
def __assign__(self,obj):
self.count=obj.count
return self
def __add__(self,i):
return add(self,i)
def __radd__(self,i):
return radd(self,i)
#This handles ob + int
def add(ob,i):
temp=CL()
temp.count=ob.count+i
return temp
#This handles int + ob
def radd(ob,i):
temp=CL()
temp.count=i+ob.count
return temp
#Variable declaration
o=CL()
o.count = 10
#Result
print o.count, #outputs 10
o=10+o
print o.count, #outputs 20
o=o+12
print o.count #outputs 32
class three_d:
def __init__(self,i=None,j=None,k=None):
if i==None:
self.x=self.y=self.z=0
else:
self.x=i
self.y=j
self.z=k
#Overload +
def __add__(self,op2):
return add(self,op2)
#Overload assignment
def __assign__(self,op2):
self.x=op2.x #These are integer assignments
self.y=op2.y #and the = retains its original
self.z=op2.z #meaning relative to them
return self
#Overload the increment operator
def __iadd__(self,op2):
return iadd(self,op2)
#Show x,y,z coordinates
def show(self):
print self.x,",",self.y,",",self.z
#friending the funcion
def add(op1,op2):
temp=three_d()
temp.x=op1.x + op2.x #These are integer additions
temp.y=op1.y + op2.y #and the + retains its original
temp.z=op1.z + op2.z #meaning relative to them.
return temp
def iadd(op1,op2):
op1.x+=op2
op1.y+=op2
op1.z+=op2
return op1
a=three_d(1,2,3)
b=three_d(10,10,10)
c=three_d()
a.show()
b.show()
#add a and b together
c=a+b
c.show()
#add a,b and c together
c=a+b+c
c.show()
#demonstrate multiple assignment
c=b=a
c.show()
b.show()
#Increment c (prefix)
c+=1
c.show()
#Increment c (postfix)
c+=1
c.show()
#Implementing prefix
c+=1
a=c
a.show()
c.show()
#Implementing postfix
a=c
a.show()
c+=1
c.show()
class sample:
def __init__(self,ob=0):
if isinstance(ob,int):
#Normal constructor
self.__s=""
return
else:
#Copy constructor
self.__s=obj._sample__s
return
def __del__(self):
print "Freeing s"
def show(self):
print self.__s
def set(self,str):
self.__s=str
def __assign__(self,ob): #Overload assignment
self.s=ob._sample__s
return self
def input():
str=sample()
instr="Hello" #User input
str.set(instr)
return str
ob=sample()
#assign returned object to ob
ob=input()
#Result
ob.show()
class atype:
def __init__(self):
self.__a=[]
for i in range(SIZE):
self.__a.append(i)
def a(self,i):
return self.__a[i]
#Variable declaration
SIZE=3
ob=atype()
#Result
print ob.a(2),
class atype:
def __init__(self):
self.__a=[]
for i in range(SIZE):
self.__a.append(i)
def a(self,i,j=None):
if j==None:
return self.__a[i]
else:
self.__a[i]=j
#Variable declaration
SIZE=3
ob=atype()
print ob.a(2), #displays 2
ob.a(2,25)
print ob.a(2) #now displays 25
class atype:
def __init__(self):
self.__a=[]
for i in range(SIZE):
self.__a.append(i)
def a(self,i,j=None):
if (i<0 or i>SIZE-1):
print "Index value of",
print i,"is out of bounds."
return
if j==None:
return self.__a[i]
else:
self.__a[i]=j
#Variable declaration
SIZE=3
ob=atype()
print ob.a(2), #displays 2
ob.a(2,25)
print ob.a(2) #now displays 25
ob.a(44,3) #generates runtime error, 3 out of bounds
class three_d:
def __init__(self,i=None,j=None,k=None):
if i==None:
self.x=self.y=self.z=0 #3-D coordinates
else:
self.x=i
self.y=j
self.z=k
#Show X,Y,Z coordinates
def show(self):
print self.x,",",self.y,",",self.z
#Overload ()
def a(self,a,b,c):
temp = three_d()
temp.x=self.x+a
temp.y=self.y+b
temp.z=self.z+c
return temp
#Variable declaration
ob1=three_d(1,2,3)
ob2=ob1.a(10,11,12) #invoke operator ()
#Result
print "ob1: ",
ob1.show()
print "ob2: ",
ob2.show()
class str_type:
def __init__(self,str=""):
self.__string=str
#String concatenation
def __add__(self,str):
temp=str_type()
if isinstance(str,str_type):
temp.__string=self.__string+str.__string
else:
temp.__string=self.__string+str
return temp
#String copy
def __assign__(self,str):
if isinstance(str,str_type):
self.__string=str.__string
else:
self.__string=str
return self
def show_str(self):
print self.__string
a=str_type("Hello ")
b=str_type("There")
c=a+b
c.show_str()
a=str_type("to program in because")
a.show_str()
b=c=str_type("C++ is fun")
c=c+" "+a+" "+b
c.show_str()