"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Object with reference to constructor. \n",
"\n",
"import sys\n",
"\n",
"#Class definition\n",
"class num:\n",
" #There is no overloading concept in python\n",
" def __init__(self,k=None,j=None):\n",
" if k is not None:\n",
" self.n = k\n",
" return\n",
" else:\n",
" if j is not None:\n",
" self.n = j.n\n",
" \n",
" def show(self):\n",
" sys.stdout.write(\"%d\"%(self.n))\n",
" \n",
"#Object creation\n",
"J = num(50)\n",
"K = num(J.n)\n",
"L = num()\n",
"L = J\n",
"M = num()\n",
"M = J\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nObject J Value of n : \")\n",
"J.show()\n",
"sys.stdout.write(\"\\nObject K Value of n : \")\n",
"K.show()\n",
"sys.stdout.write(\"\\nObject L Value of n : \")\n",
"L.show()\n",
"sys.stdout.write(\"\\nObject M Value of n : \")\n",
"M.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Object J Value of n : 50\n",
"Object K Value of n : 50\n",
"Object L Value of n : 50\n",
"Object M Value of n : 50"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 16.25, Page number: 576
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Destructors\n",
"\n",
"import sys\n",
"\n",
"c = 0\n",
"\n",
"#Class definition\n",
"class num:\n",
" def __init__(self,c):\n",
" c = c + 1\n",
" sys.stdout.write(\"\\nObject Created : Object(%d)\"%(c))\n",
" \n",
" \n",
" def __exit__(self, type, value, traceback,c):\n",
" self.package_obj.cleanup()\n",
" sys.stdout.write(\"\\nObject Released : Object(%d)\"%(c))\n",
" c = c - 1\n",
" \n",
" \n",
"#Object creation\n",
"sys.stdout.write(\"\\nIn main()\\n\")\n",
"a = num(c)\n",
"b = num(c)\n",
"\n",
"sys.stdout.write(\"\\nIn Block A()\\n\")\n",
"c = num(c)\n",
"\n",
"sys.stdout.write(\"\\n\\nAgain in main()\\n\")\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"In main()\n",
"\n",
"Object Created : Object(1)\n",
"Object Created : Object(1)\n",
"In Block A()\n",
"\n",
"Object Created : Object(1)\n",
"\n",
"Again in main()\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 16.26, Page number: 579
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Overload unary ++ operator\n",
"\n",
"import sys\n",
"\n",
"#Class definition\n",
"class num:\n",
" def input1(self):\n",
" self.a = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" self.b = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" self.c = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" self.d = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" \n",
" def show(self):\n",
" sys.stdout.write(\"\\nA = %d B = %d C = %d D = %d\"%(self.a,self.b,self.c,self.d))\n",
" \n",
" def operatorplusplus(self):\n",
" self.a = self.a + 1\n",
" self.b = self.b + 1\n",
" self.c = self.c + 1\n",
" self.d = self.d + 1\n",
" \n",
"#Object creation\n",
"X = num()\n",
"\n",
"X.input1()\n",
"sys.stdout.write(\"\\nBefore Overloading X : \")\n",
"X.show()\n",
"\n",
"#There is no operator overloading in python\n",
"X.operatorplusplus()\n",
"sys.stdout.write(\"\\nAfter Overloading X : \")\n",
"X.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 9\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Before Overloading X : \n",
"A = 4 B = 5 C = 8 D = 9\n",
"After Overloading X : \n",
"A = 5 B = 6 C = 9 D = 10"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 16.27, Page number: 581
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Overload + binary operator\n",
"\n",
"import sys\n",
"\n",
"#Class definition\n",
"class num:\n",
" def input1(self):\n",
" self.a = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" self.b = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" self.c = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" self.d = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" \n",
" def show(self):\n",
" sys.stdout.write(\"A = %d B = %d C = %d D = %d\"%(self.a,self.b,self.c,self.d))\n",
" \n",
" def operatorplus(self,t):\n",
" tmp = num()\n",
" tmp.a = self.a + t.a\n",
" tmp.b = self.b + t.b\n",
" tmp.c = self.c + t.c\n",
" tmp.d = self.d + t.d\n",
" return tmp\n",
" \n",
"#Object creation\n",
"X = num()\n",
"Y = num()\n",
"Z = num()\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nObject X \")\n",
"X.input1()\n",
"sys.stdout.write(\"\\nObject Y \")\n",
"Y.input1()\n",
"\n",
"Z = X.operatorplus(Y)\n",
"\n",
"sys.stdout.write(\"\\nX : \")\n",
"X.show()\n",
"sys.stdout.write(\"\\nY : \")\n",
"Y.show()\n",
"sys.stdout.write(\"\\nZ : \")\n",
"Z.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Object X "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 1\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Object Y "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 2\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"X : A = 1 B = 4 C = 2 D = 1\n",
"Y : A = 2 B = 5 C = 4 D = 2\n",
"Z : A = 3 B = 9 C = 6 D = 3"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 16.28, Page number: 583
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Multiplication using an integer and object. \n",
"\n",
"import sys\n",
"\n",
"#Class definition\n",
"class num:\n",
" def input1(self):\n",
" self.a = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" self.b = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" self.c = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" self.d = int(raw_input(\"Enter Values for a,b,c and d : \"))\n",
" \n",
" def show(self):\n",
" sys.stdout.write(\"A = %d B = %d C = %d D = %d\"%(self.a,self.b,self.c,self.d))\n",
" \n",
"def operatormul(a,t):\n",
" tmp = num()\n",
" tmp.a = a * t.a\n",
" tmp.b = a * t.b\n",
" tmp.c = a * t.c\n",
" tmp.d = a * t.d\n",
" return tmp\n",
"\n",
"#Result\n",
"X = num()\n",
"Z = num()\n",
"\n",
"sys.stdout.write(\"\\nObject X \")\n",
"X.input1()\n",
"\n",
"Z = operatormul(3,X)\n",
"sys.stdout.write(\"\\nX : \")\n",
"X.show()\n",
"sys.stdout.write(\"\\nZ : \")\n",
"Z.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Object X "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a,b,c and d : 3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"X : A = 1 B = 2 C = 2 D = 3\n",
"Z : A = 3 B = 6 C = 6 D = 9"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"