{ "metadata": { "name": "", "signature": "sha256:be0df45a07fa8844875025463a4211b5faab9834d4e1dd155b475b36ae6ea27e" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 13: Operator Overloading

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.1, Page Number: 300

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class three_d:\n", " def __init__(self,i=None,j=None,k=None):\n", " if i==None:\n", " self.x=self.y=self.z=0\n", " else:\n", " self.x=i\n", " self.y=j\n", " self.z=k\n", " #Overload +\n", " def __add__(self,op2):\n", " temp=three_d()\n", " temp.x=self.x + op2.x #These are integer additions\n", " temp.y=self.y + op2.y #and the + retains its original\n", " temp.z=self.z + op2.z #meaning relative to them.\n", " return temp\n", " #Overload assignment\n", " def __assign__(self,op2):\n", " self.x=op2.x #These are integer assignments\n", " self.y=op2.y #and the = retains its original \n", " self.z=op2.z #meaning relative to them\n", " return self\n", " #Show x,y,z coordinates\n", " def show(self):\n", " print self.x,\",\",self.y,\",\",self.z\n", " \n", "#Variable declaration\n", "a=three_d(1,2,3)\n", "b=three_d(10,10,10)\n", "c=three_d()\n", "\n", "a.show()\n", "b.show()\n", "\n", "#add a and b together\n", "c=a+b\n", "c.show()\n", "\n", "#add a,b and c together\n", "c=a+b+c\n", "c.show()\n", "\n", "#demonstrate multiple assignment\n", "c=b=a\n", "c.show()\n", "b.show()\n", " \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 , 2 , 3\n", "10 , 10 , 10\n", "11 , 12 , 13\n", "22 , 24 , 26\n", "1 , 2 , 3\n", "1 , 2 , 3\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.2, Page Number: 303

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class three_d:\n", " def __init__(self,i=None,j=None,k=None):\n", " if i==None:\n", " self.x=self.y=self.z=0\n", " else:\n", " self.x=i\n", " self.y=j\n", " self.z=k\n", " #Overload +\n", " def __add__(self,op2):\n", " temp=three_d()\n", " temp.x=self.x + op2.x #These are integer additions\n", " temp.y=self.y + op2.y #and the + retains its original\n", " temp.z=self.z + op2.z #meaning relative to them.\n", " return temp\n", " #Overload assignment\n", " def __assign__(self,op2):\n", " self.x=op2.x #These are integer assignments\n", " self.y=op2.y #and the = retains its original \n", " self.z=op2.z #meaning relative to them\n", " return self\n", " #Overload the increment operator\n", " def __iadd__(self,op2):\n", " self.x+=op2\n", " self.y+=op2\n", " self.z+=op2\n", " return self\n", " #Show x,y,z coordinates\n", " def show(self):\n", " print self.x,\",\",self.y,\",\",self.z\n", " \n", "a=three_d(1,2,3)\n", "b=three_d(10,10,10)\n", "c=three_d()\n", "\n", "a.show()\n", "b.show()\n", "\n", "#add a and b together\n", "c=a+b\n", "c.show()\n", "\n", "#add a,b and c together\n", "c=a+b+c\n", "c.show()\n", "\n", "#demonstrate multiple assignment\n", "c=b=a\n", "c.show()\n", "b.show()\n", " \n", "#Increment c\n", "c+=1\n", "c.show()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 , 2 , 3\n", "10 , 10 , 10\n", "11 , 12 , 13\n", "22 , 24 , 26\n", "1 , 2 , 3\n", "1 , 2 , 3\n", "2 , 3 , 4\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.3, Page Number: 306

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class three_d:\n", " def __init__(self,i=None,j=None,k=None):\n", " if i==None:\n", " self.x=self.y=self.z=0\n", " else:\n", " self.x=i\n", " self.y=j\n", " self.z=k\n", " #Overload +\n", " def __add__(self,op2):\n", " temp=three_d()\n", " temp.x=self.x + op2.x #These are integer additions\n", " temp.y=self.y + op2.y #and the + retains its original\n", " temp.z=self.z + op2.z #meaning relative to them.\n", " return temp\n", " #Overload assignment\n", " def __assign__(self,op2):\n", " self.x=op2.x #These are integer assignments\n", " self.y=op2.y #and the = retains its original \n", " self.z=op2.z #meaning relative to them\n", " return self\n", " #Overload the increment operator\n", " def __iadd__(self,op2):\n", " self.x+=op2\n", " self.y+=op2\n", " self.z+=op2\n", " return self\n", " #Show x,y,z coordinates\n", " def show(self):\n", " print self.x,\",\",self.y,\",\",self.z\n", " \n", "a=three_d(1,2,3)\n", "b=three_d(10,10,10)\n", "c=three_d()\n", "\n", "a.show()\n", "b.show()\n", "\n", "#add a and b together\n", "c=a+b\n", "c.show()\n", "\n", "#add a,b and c together\n", "c=a+b+c\n", "c.show()\n", "\n", "#demonstrate multiple assignment\n", "c=b=a\n", "c.show()\n", "b.show()\n", " \n", "#Increment c (prefix)\n", "c+=1\n", "c.show()\n", "\n", "#Increment c (postfix)\n", "c+=1\n", "c.show()\n", "\n", "#Implementing prefix\n", "c+=1\n", "a=c\n", "a.show()\n", "c.show()\n", "\n", "#Implementing postfix\n", "a=c\n", "a.show()\n", "c+=1\n", "c.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 , 2 , 3\n", "10 , 10 , 10\n", "11 , 12 , 13\n", "22 , 24 , 26\n", "1 , 2 , 3\n", "1 , 2 , 3\n", "2 , 3 , 4\n", "3 , 4 , 5\n", "4 , 5 , 6\n", "4 , 5 , 6\n", "4 , 5 , 6\n", "5 , 6 , 7\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.4, Page Number: 310

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class three_d:\n", " def __init__(self,i=None,j=None,k=None):\n", " if i==None:\n", " self.x=self.y=self.z=0\n", " else:\n", " self.x=i\n", " self.y=j\n", " self.z=k\n", " #Overload +\n", " def __add__(self,op2):\n", " return add(self,op2)\n", " #Overload assignment\n", " def __assign__(self,op2):\n", " self.x=op2.x #These are integer assignments\n", " self.y=op2.y #and the = retains its original \n", " self.z=op2.z #meaning relative to them\n", " return self\n", " #Show x,y,z coordinates\n", " def show(self):\n", " print self.x,\",\",self.y,\",\",self.z\n", " \n", "#friending the funcion\n", "def add(op1,op2):\n", " temp=three_d()\n", " temp.x=op1.x + op2.x #These are integer additions\n", " temp.y=op1.y + op2.y #and the + retains its original\n", " temp.z=op1.z + op2.z #meaning relative to them.\n", " return temp\n", "\n", "a=three_d(1,2,3)\n", "b=three_d(10,10,10)\n", "c=three_d()\n", "\n", "a.show()\n", "b.show()\n", "\n", "#add a and b together\n", "c=a+b\n", "c.show()\n", "\n", "#add a,b and c together\n", "c=a+b+c\n", "c.show()\n", "\n", "#demonstrate multiple assignment\n", "c=b=a\n", "c.show()\n", "b.show()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 , 2 , 3\n", "10 , 10 , 10\n", "11 , 12 , 13\n", "22 , 24 , 26\n", "1 , 2 , 3\n", "1 , 2 , 3\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.5, Page Number: 311

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class CL:\n", " def __init__(self):\n", " self.count=0\n", " def __assign__(self,obj):\n", " self.count=obj.count\n", " return self\n", " def __add__(self,i): \n", " return add(self,i)\n", " def __radd__(self,i):\n", " return radd(self,i)\n", "\n", "#This handles ob + int\n", "def add(ob,i):\n", " temp=CL()\n", " temp.count=ob.count+i\n", " return temp\n", " \n", "#This handles int + ob \n", "def radd(ob,i):\n", " temp=CL()\n", " temp.count=i+ob.count\n", " return temp\n", "\n", "#Variable declaration\n", "o=CL()\n", "o.count = 10\n", "\n", "#Result\n", "print o.count, #outputs 10\n", "o=10+o\n", "print o.count, #outputs 20\n", "o=o+12\n", "print o.count #outputs 32\n", "\n", "\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 20 32\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.6, Page Number: 314

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class three_d:\n", " def __init__(self,i=None,j=None,k=None):\n", " if i==None:\n", " self.x=self.y=self.z=0\n", " else:\n", " self.x=i\n", " self.y=j\n", " self.z=k\n", " #Overload +\n", " def __add__(self,op2):\n", " return add(self,op2)\n", " #Overload assignment\n", " def __assign__(self,op2):\n", " self.x=op2.x #These are integer assignments\n", " self.y=op2.y #and the = retains its original \n", " self.z=op2.z #meaning relative to them\n", " return self\n", " #Overload the increment operator\n", " def __iadd__(self,op2):\n", " return iadd(self,op2)\n", " #Show x,y,z coordinates\n", " def show(self):\n", " print self.x,\",\",self.y,\",\",self.z\n", "\n", "#friending the funcion\n", "def add(op1,op2):\n", " temp=three_d()\n", " temp.x=op1.x + op2.x #These are integer additions\n", " temp.y=op1.y + op2.y #and the + retains its original\n", " temp.z=op1.z + op2.z #meaning relative to them.\n", " return temp\n", "def iadd(op1,op2):\n", " op1.x+=op2\n", " op1.y+=op2\n", " op1.z+=op2\n", " return op1\n", " \n", "a=three_d(1,2,3)\n", "b=three_d(10,10,10)\n", "c=three_d()\n", "\n", "a.show()\n", "b.show()\n", "\n", "#add a and b together\n", "c=a+b\n", "c.show()\n", "\n", "#add a,b and c together\n", "c=a+b+c\n", "c.show()\n", "\n", "#demonstrate multiple assignment\n", "c=b=a\n", "c.show()\n", "b.show()\n", " \n", "#Increment c (prefix)\n", "c+=1\n", "c.show()\n", "\n", "#Increment c (postfix)\n", "c+=1\n", "c.show()\n", "\n", "#Implementing prefix\n", "c+=1\n", "a=c\n", "a.show()\n", "c.show()\n", "\n", "#Implementing postfix\n", "a=c\n", "a.show()\n", "c+=1\n", "c.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 , 2 , 3\n", "10 , 10 , 10\n", "11 , 12 , 13\n", "22 , 24 , 26\n", "1 , 2 , 3\n", "1 , 2 , 3\n", "2 , 3 , 4\n", "3 , 4 , 5\n", "4 , 5 , 6\n", "4 , 5 , 6\n", "4 , 5 , 6\n", "5 , 6 , 7\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.7, Page Number: 318

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class sample:\n", " def __init__(self,ob=0):\n", " if isinstance(ob,int):\n", " #Normal constructor\n", " self.__s=\"\"\n", " return\n", " else:\n", " #Copy constructor\n", " self.__s=obj._sample__s\n", " return\n", " def __del__(self):\n", " print \"Freeing s\"\n", " def show(self):\n", " print self.__s\n", " def set(self,str):\n", " self.__s=str\n", " def __assign__(self,ob): #Overload assignment\n", " self.s=ob._sample__s\n", " return self\n", " \n", "def input():\n", " str=sample()\n", " instr=\"Hello\" #User input\n", " str.set(instr)\n", " return str\n", "\n", "ob=sample()\n", "\n", "#assign returned object to ob\n", "ob=input()\n", "\n", "#Result\n", "ob.show()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Freeing s\n", "Freeing s\n", "Hello\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.8, Page Number: 321

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class atype:\n", " def __init__(self):\n", " self.__a=[]\n", " for i in range(SIZE):\n", " self.__a.append(i)\n", " def a(self,i):\n", " return self.__a[i]\n", " \n", "#Variable declaration\n", "SIZE=3\n", "ob=atype()\n", "\n", "#Result\n", "print ob.a(2),\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.9, Page Number: 322

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class atype:\n", " def __init__(self):\n", " self.__a=[]\n", " for i in range(SIZE):\n", " self.__a.append(i)\n", " def a(self,i,j=None):\n", " if j==None:\n", " return self.__a[i]\n", " else:\n", " self.__a[i]=j\n", " \n", "#Variable declaration\n", "SIZE=3 \n", "ob=atype()\n", "\n", "print ob.a(2), #displays 2\n", "\n", "ob.a(2,25)\n", "\n", "print ob.a(2) #now displays 25" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2 25\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.10, Page Number: 323

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class atype:\n", " def __init__(self):\n", " self.__a=[]\n", " for i in range(SIZE):\n", " self.__a.append(i)\n", " def a(self,i,j=None):\n", " if (i<0 or i>SIZE-1):\n", " print \"Index value of\",\n", " print i,\"is out of bounds.\"\n", " return\n", " if j==None:\n", " return self.__a[i]\n", " else:\n", " self.__a[i]=j\n", " \n", "#Variable declaration\n", "SIZE=3 \n", "ob=atype()\n", "\n", "print ob.a(2), #displays 2\n", "\n", "ob.a(2,25)\n", "\n", "print ob.a(2) #now displays 25\n", "\n", "ob.a(44,3) #generates runtime error, 3 out of bounds" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2 25\n", "Index value of 44 is out of bounds.\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.11, Page Number: 324

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class three_d:\n", " def __init__(self,i=None,j=None,k=None):\n", " if i==None:\n", " self.x=self.y=self.z=0 #3-D coordinates\n", " else:\n", " self.x=i\n", " self.y=j\n", " self.z=k\n", " #Show X,Y,Z coordinates\n", " def show(self):\n", " print self.x,\",\",self.y,\",\",self.z\n", " #Overload ()\n", " def a(self,a,b,c):\n", " temp = three_d()\n", " temp.x=self.x+a\n", " temp.y=self.y+b\n", " temp.z=self.z+c\n", " return temp\n", " \n", "#Variable declaration\n", "ob1=three_d(1,2,3)\n", "\n", "ob2=ob1.a(10,11,12) #invoke operator ()\n", "\n", "#Result\n", "print \"ob1: \",\n", "ob1.show()\n", "print \"ob2: \",\n", "ob2.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ob1: 1 , 2 , 3\n", "ob2: 11 , 13 , 15\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 13.12, Page Number: 326

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class str_type:\n", " def __init__(self,str=\"\"):\n", " self.__string=str\n", " #String concatenation\n", " def __add__(self,str):\n", " temp=str_type()\n", " if isinstance(str,str_type):\n", " temp.__string=self.__string+str.__string\n", " else:\n", " temp.__string=self.__string+str\n", " return temp\n", " #String copy\n", " def __assign__(self,str):\n", " if isinstance(str,str_type):\n", " self.__string=str.__string\n", " else:\n", " self.__string=str\n", " return self\n", " def show_str(self):\n", " print self.__string\n", " \n", "a=str_type(\"Hello \")\n", "b=str_type(\"There\")\n", "c=a+b\n", "c.show_str()\n", "\n", "a=str_type(\"to program in because\")\n", "a.show_str()\n", "\n", "b=c=str_type(\"C++ is fun\")\n", "\n", "c=c+\" \"+a+\" \"+b\n", "c.show_str()\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello There\n", "to program in because\n", "C++ is fun to program in because C++ is fun\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }