Chapter 13: Operator Overloading

Example 13.1, Page Number: 300

In [1]:
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()
      
    
1 , 2 , 3
10 , 10 , 10
11 , 12 , 13
22 , 24 , 26
1 , 2 , 3
1 , 2 , 3

Example 13.2, Page Number: 303

In [2]:
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()
1 , 2 , 3
10 , 10 , 10
11 , 12 , 13
22 , 24 , 26
1 , 2 , 3
1 , 2 , 3
2 , 3 , 4

Example 13.3, Page Number: 306

In [3]:
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()
1 , 2 , 3
10 , 10 , 10
11 , 12 , 13
22 , 24 , 26
1 , 2 , 3
1 , 2 , 3
2 , 3 , 4
3 , 4 , 5
4 , 5 , 6
4 , 5 , 6
4 , 5 , 6
5 , 6 , 7

Example 13.4, Page Number: 310

In [4]:
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()
1 , 2 , 3
10 , 10 , 10
11 , 12 , 13
22 , 24 , 26
1 , 2 , 3
1 , 2 , 3

Example 13.5, Page Number: 311

In [1]:
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


        
10 20 32

Example 13.6, Page Number: 314

In [6]:
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()
1 , 2 , 3
10 , 10 , 10
11 , 12 , 13
22 , 24 , 26
1 , 2 , 3
1 , 2 , 3
2 , 3 , 4
3 , 4 , 5
4 , 5 , 6
4 , 5 , 6
4 , 5 , 6
5 , 6 , 7

Example 13.7, Page Number: 318

In [4]:
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()
Freeing s
Freeing s
Hello

Example 13.8, Page Number: 321

In [6]:
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),
2

Example 13.9, Page Number: 322

In [7]:
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
2 25

Example 13.10, Page Number: 323

In [9]:
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
2 25
Index value of 44 is out of bounds.

Example 13.11, Page Number: 324

In [10]:
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()
ob1:  1 , 2 , 3
ob2:  11 , 13 , 15

Example 13.12, Page Number: 326

In [12]:
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()
Hello There
to program in because
C++ is fun to program in because C++ is fun
In [ ]: