diff options
author | debashisdeb | 2014-06-20 15:42:42 +0530 |
---|---|---|
committer | debashisdeb | 2014-06-20 15:42:42 +0530 |
commit | 83c1bfceb1b681b4bb7253b47491be2d8b2014a1 (patch) | |
tree | f54eab21dd3d725d64a495fcd47c00d37abed004 /C++_from_the_Ground/Chapter_19(1).ipynb | |
parent | a78126bbe4443e9526a64df9d8245c4af8843044 (diff) | |
download | Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.gz Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.bz2 Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.zip |
removing problem statements
Diffstat (limited to 'C++_from_the_Ground/Chapter_19(1).ipynb')
-rw-r--r-- | C++_from_the_Ground/Chapter_19(1).ipynb | 551 |
1 files changed, 515 insertions, 36 deletions
diff --git a/C++_from_the_Ground/Chapter_19(1).ipynb b/C++_from_the_Ground/Chapter_19(1).ipynb index 318b8aaa..3c9ecd17 100644 --- a/C++_from_the_Ground/Chapter_19(1).ipynb +++ b/C++_from_the_Ground/Chapter_19(1).ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "Chapter 19" + "name": "", + "signature": "sha256:d2c5aefc81786f6e7a83b5f07eeb250789c73d2ab2ad23662758aa0cdec50617" }, "nbformat": 3, "nbformat_minor": 0, @@ -10,24 +11,56 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h1>Chapter 19: Run-Time Type ID and the Casting Operators<h1>" + "source": [ + "<h1>Chapter 19: Run-Time Type ID and the Casting Operators<h1>" + ] }, { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 19.1, Page Number: 453<h3>" + "source": [ + "<h3>Example 19.1, Page Number: 453<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''A simple example taht uses typeid'''\n\nclass myclass:\n pass\n\n#Variable declaration\ni=j=0\nf=0.0\nob=myclass()\n\nprint \"The type of i is:\",type(i).__name__\nprint \"The type of f is:\",type(f).__name__\nprint \"The type of ob is:\",ob.__class__.__name__\nprint \"\\n\"\n\nif type(i)==type(j):\n print \"The types of i and j are the same\"\n \nif not(type(i)==type(f)):\n print \"The types of i and f are not the same\"", + "input": [ + "\n", + "class myclass:\n", + " pass\n", + "\n", + "#Variable declaration\n", + "i=j=0\n", + "f=0.0\n", + "ob=myclass()\n", + "\n", + "print \"The type of i is:\",type(i).__name__\n", + "print \"The type of f is:\",type(f).__name__\n", + "print \"The type of ob is:\",ob.__class__.__name__\n", + "print \"\\n\"\n", + "\n", + "if type(i)==type(j):\n", + " print \"The types of i and j are the same\"\n", + " \n", + "if not(type(i)==type(f)):\n", + " print \"The types of i and f are not the same\"" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "The type of i is: int\nThe type of f is: float\nThe type of ob is: myclass\n\n\nThe types of i and j are the same\nThe types of i and f are not the same\n" + "text": [ + "The type of i is: int\n", + "The type of f is: float\n", + "The type of ob is: myclass\n", + "\n", + "\n", + "The types of i and j are the same\n", + "The types of i and f are not the same\n" + ] } ], "prompt_number": 2 @@ -35,19 +68,52 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 19.2, Page Number: 454<h3>" + "source": [ + "<h3>Example 19.2, Page Number: 454<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''An example that uses typeid on a polymorphic class hierarchy'''\n\nclass Base:\n pass\nclass Derived1(Base):\n pass\nclass Derived2(Base):\n pass\n\n#Variable declaration\nbaseob=Base()\np=[Base()]\nob1=Derived1()\nob2=Derived2()\n\n\np[0]=baseob\nprint \"p is pointing to an object of type\",\nprint p[0].__class__.__name__\n\np[0]=ob1\nprint \"p is pointing to an object of type\",\nprint p[0].__class__.__name__\n\np[0]=ob2\nprint \"p is pointing to an object of type\",\nprint p[0].__class__.__name__", + "input": [ + "\n", + "class Base:\n", + " pass\n", + "class Derived1(Base):\n", + " pass\n", + "class Derived2(Base):\n", + " pass\n", + "\n", + "#Variable declaration\n", + "baseob=Base()\n", + "p=[Base()]\n", + "ob1=Derived1()\n", + "ob2=Derived2()\n", + "\n", + "\n", + "p[0]=baseob\n", + "print \"p is pointing to an object of type\",\n", + "print p[0].__class__.__name__\n", + "\n", + "p[0]=ob1\n", + "print \"p is pointing to an object of type\",\n", + "print p[0].__class__.__name__\n", + "\n", + "p[0]=ob2\n", + "print \"p is pointing to an object of type\",\n", + "print p[0].__class__.__name__" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "p is pointing to an object of type Base\np is pointing to an object of type Derived1\np is pointing to an object of type Derived2\n" + "text": [ + "p is pointing to an object of type Base\n", + "p is pointing to an object of type Derived1\n", + "p is pointing to an object of type Derived2\n" + ] } ], "prompt_number": 3 @@ -55,19 +121,48 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 19.3, Page Number: 455<h3>" + "source": [ + "<h3>Example 19.3, Page Number: 455<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''An example that uses typeid on a polymorphic class hierarchy'''\n\nclass Base:\n pass\nclass Derived1(Base):\n pass\nclass Derived2(Base):\n pass\n\ndef WhatType(ob):\n print \"ob is referencing an object of type\",\n print ob.__class__.__name__\n \n \n#Variable declaration\nbaseob=Base()\np=[Base()]\nob1=Derived1()\nob2=Derived2()\n\nWhatType(baseob)\nWhatType(ob1)\nWhatType(ob2)", + "input": [ + "\n", + "class Base:\n", + " pass\n", + "class Derived1(Base):\n", + " pass\n", + "class Derived2(Base):\n", + " pass\n", + "\n", + "def WhatType(ob):\n", + " print \"ob is referencing an object of type\",\n", + " print ob.__class__.__name__\n", + " \n", + " \n", + "#Variable declaration\n", + "baseob=Base()\n", + "p=[Base()]\n", + "ob1=Derived1()\n", + "ob2=Derived2()\n", + "\n", + "WhatType(baseob)\n", + "WhatType(ob1)\n", + "WhatType(ob2)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "ob is referencing an object of type Base\nob is referencing an object of type Derived1\nob is referencing an object of type Derived2\n" + "text": [ + "ob is referencing an object of type Base\n", + "ob is referencing an object of type Derived1\n", + "ob is referencing an object of type Derived2\n" + ] } ], "prompt_number": 4 @@ -75,19 +170,94 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 19.4, Page Number: 456<h3>" + "source": [ + "<h3>Example 19.4, Page Number: 456<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Demonstrate run-time type id'''\n\nimport random\n\nclass figure:\n def __init__(self,i,j):\n self._x=i\n self._y=j\n \nclass triangle(figure):\n def __init__(self,i,j):\n figure.__init__(self,i,j)\n def area(self):\n return self._x*0.5*self._y\n \nclass rectangle(figure):\n def __init__(self,i,j):\n figure.__init__(self,i,j)\n def area(self):\n return self._x*self._y\n \nclass circle(figure):\n def __init__(self,i,j=0):\n figure.__init__(self,i,j)\n def area(self):\n return self._x*self._x*3.14\n \ndef factory():\n i=random.randint(0,2)\n if i==0:\n return circle(10.0)\n elif i==1:\n return triangle(10.1,5.3)\n elif i==2:\n return rectangle(4.3,5.7)\n \n\nt=c=r=0 \np=[None]\n\n#generate and count objects\nfor i in range(10):\n p[0]=factory() #generate an object\n print \"Object is \",p[0].__class__.__name__,\". \",\n #count it\n if p[0].__class__.__name__==triangle.__name__:\n t+=1\n if p[0].__class__.__name__==rectangle.__name__:\n r+=1\n if p[0].__class__.__name__==circle.__name__:\n c+=1\n #display its area\n print \"Area is\",p[0].area()\n\nprint \"Objects generated:\"\nprint \"Triangles:\",t\nprint \"Rectangles:\",r\nprint \"Circles:\",c", + "input": [ + "\n", + "import random\n", + "\n", + "class figure:\n", + " def __init__(self,i,j):\n", + " self._x=i\n", + " self._y=j\n", + " \n", + "class triangle(figure):\n", + " def __init__(self,i,j):\n", + " figure.__init__(self,i,j)\n", + " def area(self):\n", + " return self._x*0.5*self._y\n", + " \n", + "class rectangle(figure):\n", + " def __init__(self,i,j):\n", + " figure.__init__(self,i,j)\n", + " def area(self):\n", + " return self._x*self._y\n", + " \n", + "class circle(figure):\n", + " def __init__(self,i,j=0):\n", + " figure.__init__(self,i,j)\n", + " def area(self):\n", + " return self._x*self._x*3.14\n", + " \n", + "def factory():\n", + " i=random.randint(0,2)\n", + " if i==0:\n", + " return circle(10.0)\n", + " elif i==1:\n", + " return triangle(10.1,5.3)\n", + " elif i==2:\n", + " return rectangle(4.3,5.7)\n", + " \n", + "\n", + "t=c=r=0 \n", + "p=[None]\n", + "\n", + "#generate and count objects\n", + "for i in range(10):\n", + " p[0]=factory() #generate an object\n", + " print \"Object is \",p[0].__class__.__name__,\". \",\n", + " #count it\n", + " if p[0].__class__.__name__==triangle.__name__:\n", + " t+=1\n", + " if p[0].__class__.__name__==rectangle.__name__:\n", + " r+=1\n", + " if p[0].__class__.__name__==circle.__name__:\n", + " c+=1\n", + " #display its area\n", + " print \"Area is\",p[0].area()\n", + "\n", + "print \"Objects generated:\"\n", + "print \"Triangles:\",t\n", + "print \"Rectangles:\",r\n", + "print \"Circles:\",c" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Object is circle . Area is 314.0\nObject is rectangle . Area is 24.51\nObject is rectangle . Area is 24.51\nObject is circle . Area is 314.0\nObject is rectangle . Area is 24.51\nObject is circle . Area is 314.0\nObject is rectangle . Area is 24.51\nObject is triangle . Area is 26.765\nObject is rectangle . Area is 24.51\nObject is circle . Area is 314.0\nObjects generated:\nTriangles: 1\nRectangles: 5\nCircles: 4\n" + "text": [ + "Object is circle . Area is 314.0\n", + "Object is rectangle . Area is 24.51\n", + "Object is rectangle . Area is 24.51\n", + "Object is circle . Area is 314.0\n", + "Object is rectangle . Area is 24.51\n", + "Object is circle . Area is 314.0\n", + "Object is rectangle . Area is 24.51\n", + "Object is triangle . Area is 26.765\n", + "Object is rectangle . Area is 24.51\n", + "Object is circle . Area is 314.0\n", + "Objects generated:\n", + "Triangles: 1\n", + "Rectangles: 5\n", + "Circles: 4\n" + ] } ], "prompt_number": 5 @@ -95,19 +265,56 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 19.5, Page Number: 456<h3>" + "source": [ + "<h3>Example 19.5, Page Number: 456<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Using typeid with templates'''\n#Since python does not have templates\n#it will give a different output.\n\n#Template class\nclass myclass:\n def __init__(self,i):\n self.__a=i\n \no1=myclass(10)\no2=myclass(9)\no3=myclass(7.2)\n\nprint \"Type of o1 is\",o1.__class__.__name__\n\nprint \"Type of o2 is\",o2.__class__.__name__\n\nprint \"Type of o3 is\",o3.__class__.__name__\n\nprint\n\nif o1.__class__.__name__==o2.__class__.__name__:\n print \"o1 and o2 are the same type\"\n \nif o1.__class__.__name__==o3.__class__.__name__:\n print \"Error\"\nelse:\n print \"o1 and o3 are different types\"\n\n#This prints error because python doesnt use templates.", + "input": [ + "\n", + "#Template class\n", + "class myclass:\n", + " def __init__(self,i):\n", + " self.__a=i\n", + " \n", + "o1=myclass(10)\n", + "o2=myclass(9)\n", + "o3=myclass(7.2)\n", + "\n", + "print \"Type of o1 is\",o1.__class__.__name__\n", + "\n", + "print \"Type of o2 is\",o2.__class__.__name__\n", + "\n", + "print \"Type of o3 is\",o3.__class__.__name__\n", + "\n", + "print\n", + "\n", + "if o1.__class__.__name__==o2.__class__.__name__:\n", + " print \"o1 and o2 are the same type\"\n", + " \n", + "if o1.__class__.__name__==o3.__class__.__name__:\n", + " print \"Error\"\n", + "else:\n", + " print \"o1 and o3 are different types\"\n", + "\n", + "#This prints error because python doesnt use templates." + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Type of o1 is myclass\nType of o2 is myclass\nType of o3 is myclass\n\no1 and o2 are the same type\nError\n" + "text": [ + "Type of o1 is myclass\n", + "Type of o2 is myclass\n", + "Type of o3 is myclass\n", + "\n", + "o1 and o2 are the same type\n", + "Error\n" + ] } ], "prompt_number": 6 @@ -115,19 +322,94 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 19.6, Page Number: 460<h3>" + "source": [ + "<h3>Example 19.6, Page Number: 460<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Template version of figure hierarchy'''\n#Python classes are effectively template classes.\n\nimport random\n\nclass figure:\n def __init__(self,i,j):\n self._x=i\n self._y=j\n \nclass triangle(figure):\n def __init__(self,i,j):\n figure.__init__(self,i,j)\n def area(self):\n return self._x*0.5*self._y\n \nclass rectangle(figure):\n def __init__(self,i,j):\n figure.__init__(self,i,j)\n def area(self):\n return self._x*self._y\n \nclass circle(figure):\n def __init__(self,i,j=0):\n figure.__init__(self,i,j)\n def area(self):\n return self._x*self._x*3.14\n \ndef factory():\n i=random.randint(0,2)\n if i==0:\n return circle(10.0)\n elif i==1:\n return triangle(10.1,5.3)\n elif i==2:\n return rectangle(4.3,5.7)\n \n\nt=c=r=0 \np=[None]\n\n#generate and count objects\nfor i in range(10):\n p[0]=factory() #generate an object\n print \"Object is \",p[0].__class__.__name__,\". \",\n #count it\n if p[0].__class__.__name__==triangle.__name__:\n t+=1\n if p[0].__class__.__name__==rectangle.__name__:\n r+=1\n if p[0].__class__.__name__==circle.__name__:\n c+=1\n #display its area\n print \"Area is\",p[0].area()\n\nprint \"Objects generated:\"\nprint \"Triangles:\",t\nprint \"Rectangles:\",r\nprint \"Circles:\",c", + "input": [ + "\n", + "import random\n", + "\n", + "class figure:\n", + " def __init__(self,i,j):\n", + " self._x=i\n", + " self._y=j\n", + " \n", + "class triangle(figure):\n", + " def __init__(self,i,j):\n", + " figure.__init__(self,i,j)\n", + " def area(self):\n", + " return self._x*0.5*self._y\n", + " \n", + "class rectangle(figure):\n", + " def __init__(self,i,j):\n", + " figure.__init__(self,i,j)\n", + " def area(self):\n", + " return self._x*self._y\n", + " \n", + "class circle(figure):\n", + " def __init__(self,i,j=0):\n", + " figure.__init__(self,i,j)\n", + " def area(self):\n", + " return self._x*self._x*3.14\n", + " \n", + "def factory():\n", + " i=random.randint(0,2)\n", + " if i==0:\n", + " return circle(10.0)\n", + " elif i==1:\n", + " return triangle(10.1,5.3)\n", + " elif i==2:\n", + " return rectangle(4.3,5.7)\n", + " \n", + "\n", + "t=c=r=0 \n", + "p=[None]\n", + "\n", + "#generate and count objects\n", + "for i in range(10):\n", + " p[0]=factory() #generate an object\n", + " print \"Object is \",p[0].__class__.__name__,\". \",\n", + " #count it\n", + " if p[0].__class__.__name__==triangle.__name__:\n", + " t+=1\n", + " if p[0].__class__.__name__==rectangle.__name__:\n", + " r+=1\n", + " if p[0].__class__.__name__==circle.__name__:\n", + " c+=1\n", + " #display its area\n", + " print \"Area is\",p[0].area()\n", + "\n", + "print \"Objects generated:\"\n", + "print \"Triangles:\",t\n", + "print \"Rectangles:\",r\n", + "print \"Circles:\",c" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Object is triangle . Area is 26.765\nObject is circle . Area is 314.0\nObject is circle . Area is 314.0\nObject is rectangle . Area is 24.51\nObject is rectangle . Area is 24.51\nObject is rectangle . Area is 24.51\nObject is circle . Area is 314.0\nObject is circle . Area is 314.0\nObject is rectangle . Area is 24.51\nObject is triangle . Area is 26.765\nObjects generated:\nTriangles: 2\nRectangles: 4\nCircles: 4\n" + "text": [ + "Object is triangle . Area is 26.765\n", + "Object is circle . Area is 314.0\n", + "Object is circle . Area is 314.0\n", + "Object is rectangle . Area is 24.51\n", + "Object is rectangle . Area is 24.51\n", + "Object is rectangle . Area is 24.51\n", + "Object is circle . Area is 314.0\n", + "Object is circle . Area is 314.0\n", + "Object is rectangle . Area is 24.51\n", + "Object is triangle . Area is 26.765\n", + "Objects generated:\n", + "Triangles: 2\n", + "Rectangles: 4\n", + "Circles: 4\n" + ] } ], "prompt_number": 7 @@ -135,19 +417,119 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 19.7, Page Number: 463<h3>" + "source": [ + "<h3>Example 19.7, Page Number: 463<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implement dynamic casting'''\n#Dynamic casting is not needed in python \n#Also, the output will be different in python since \n#here since pointers are implemented using lists,\n\nclass Base:\n def f(self):\n print \"Inside Base\"\n \nclass Derived(Base):\n def f(self):\n print \"Inside Derived\"\n \nbp=[Base()] #pointer to base\nb_ob=Base() \ndp=[Derived()] #pointer to derived\nd_ob=Derived()\n\ndp[0]=d_ob\nif dp[0]:\n print \"Cast from Derived * to Derived * OK.\"\n dp[0].f()\nelse:\n print \"Error\"\nprint\n\nbp[0]=d_ob\nif bp[0]:\n print \"Cast from Derived * to Base * OK.\"\n bp[0].f()\nelse:\n print \"Error\"\nprint\n\nbp[0]=b_ob\nif bp[0]:\n print \"Cast from Base * to Base * OK.\"\n bp[0].f()\nelse:\n print \"Error\"\nprint\n\ndp[0]=b_ob\nif dp[0]:\n print \"Error\"\nelse:\n print \"Cast from Base * to Derived * not OK.\"\nprint\n\nbp[0]=d_ob #bp points to Derived object\ndp[0]=bp[0]\nif dp[0]:\n print \"Cast bp to a Derived * OK.\"\n print \"because bp is really pointing\\n\",\n print \"to a Derived object.\"\n dp[0].f()\nelse:\n print \"Error\"\nprint\n\nbp[0]=b_ob #bp points to Base object\ndp[0]=bp[0]\nif dp[0]:\n print \"Error\"\nelse:\n print \"Now casting bp to a Derived *\\n\",\n print \"is not OK because bp is really\\n\",\n print \" pointing to a Base object.\"\nprint\n\ndp[0]=d_ob #dp points to Derived object\nbp[0]=dp[0]\nif bp[0]:\n print \"Casting dp to a Base * is OK.\"\n bp[0].f()\nelse:\n print \"Error\"\nprint\n", + "input": [ + "\n", + "\n", + "class Base:\n", + " def f(self):\n", + " print \"Inside Base\"\n", + " \n", + "class Derived(Base):\n", + " def f(self):\n", + " print \"Inside Derived\"\n", + " \n", + "bp=[Base()] #pointer to base\n", + "b_ob=Base() \n", + "dp=[Derived()] #pointer to derived\n", + "d_ob=Derived()\n", + "\n", + "dp[0]=d_ob\n", + "if dp[0]:\n", + " print \"Cast from Derived * to Derived * OK.\"\n", + " dp[0].f()\n", + "else:\n", + " print \"Error\"\n", + "print\n", + "\n", + "bp[0]=d_ob\n", + "if bp[0]:\n", + " print \"Cast from Derived * to Base * OK.\"\n", + " bp[0].f()\n", + "else:\n", + " print \"Error\"\n", + "print\n", + "\n", + "bp[0]=b_ob\n", + "if bp[0]:\n", + " print \"Cast from Base * to Base * OK.\"\n", + " bp[0].f()\n", + "else:\n", + " print \"Error\"\n", + "print\n", + "\n", + "dp[0]=b_ob\n", + "if dp[0]:\n", + " print \"Error\"\n", + "else:\n", + " print \"Cast from Base * to Derived * not OK.\"\n", + "print\n", + "\n", + "bp[0]=d_ob #bp points to Derived object\n", + "dp[0]=bp[0]\n", + "if dp[0]:\n", + " print \"Cast bp to a Derived * OK.\"\n", + " print \"because bp is really pointing\\n\",\n", + " print \"to a Derived object.\"\n", + " dp[0].f()\n", + "else:\n", + " print \"Error\"\n", + "print\n", + "\n", + "bp[0]=b_ob #bp points to Base object\n", + "dp[0]=bp[0]\n", + "if dp[0]:\n", + " print \"Error\"\n", + "else:\n", + " print \"Now casting bp to a Derived *\\n\",\n", + " print \"is not OK because bp is really\\n\",\n", + " print \" pointing to a Base object.\"\n", + "print\n", + "\n", + "dp[0]=d_ob #dp points to Derived object\n", + "bp[0]=dp[0]\n", + "if bp[0]:\n", + " print \"Casting dp to a Base * is OK.\"\n", + " bp[0].f()\n", + "else:\n", + " print \"Error\"\n", + "print\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Cast from Derived * to Derived * OK.\nInside Derived\n\nCast from Derived * to Base * OK.\nInside Derived\n\nCast from Base * to Base * OK.\nInside Base\n\nError\n\nCast bp to a Derived * OK.\nbecause bp is really pointing\nto a Derived object.\nInside Derived\n\nError\n\nCasting dp to a Base * is OK.\nInside Derived\n\n" + "text": [ + "Cast from Derived * to Derived * OK.\n", + "Inside Derived\n", + "\n", + "Cast from Derived * to Base * OK.\n", + "Inside Derived\n", + "\n", + "Cast from Base * to Base * OK.\n", + "Inside Base\n", + "\n", + "Error\n", + "\n", + "Cast bp to a Derived * OK.\n", + "because bp is really pointing\n", + "to a Derived object.\n", + "Inside Derived\n", + "\n", + "Error\n", + "\n", + "Casting dp to a Base * is OK.\n", + "Inside Derived\n", + "\n" + ] } ], "prompt_number": 8 @@ -155,19 +537,74 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 19.8, Page Number: 465<h3>" + "source": [ + "<h3>Example 19.8, Page Number: 465<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implement dynamic casting'''\n#Dynamic casting is not needed in python \n#Also, the output will be different in python since \n#here since pointers are implemented using lists,\n\nclass Base:\n def f(self):\n pass\n \nclass Derived(Base):\n def derivedOnly(self):\n print \"Is a Derived Object\"\n \nbp=[Base()] #pointer to base\nb_ob=Base() \ndp=[Derived()] #pointer to derived\nd_ob=Derived()\n\n#Use typeid\n\nbp[0]=b_ob\nif bp[0].__class__.__name__==Derived.__name__:\n dp[0]=bp[0]\n dp[0].derivedOnly()\nelse:\n print \"Cast from Base to Derived failed.\"\n \nbp[0]=d_ob\nif bp[0].__class__.__name__==Derived.__name__:\n dp[0]=bp[0]\n dp[0].derivedOnly()\nelse:\n print \"Error, cast should work!\"\n \n#Use dynamic_cast\n\nbp[0]=b_ob\ndp[0]=bp[0]\nif dp[0].__class__.__name__==Derived.__name__:\n dp[0].derivedOnly()\nelse:\n print \"Cast from Base to Derived failed.\"\n \nbp[0]=d_ob\ndp[0]=bp[0]\nif dp:\n dp[0].derivedOnly()\nelse:\n print \"Error, cast should work!\"\n ", + "input": [ + "\n", + "\n", + "class Base:\n", + " def f(self):\n", + " pass\n", + " \n", + "class Derived(Base):\n", + " def derivedOnly(self):\n", + " print \"Is a Derived Object\"\n", + " \n", + "bp=[Base()] #pointer to base\n", + "b_ob=Base() \n", + "dp=[Derived()] #pointer to derived\n", + "d_ob=Derived()\n", + "\n", + "#Use typeid\n", + "\n", + "bp[0]=b_ob\n", + "if bp[0].__class__.__name__==Derived.__name__:\n", + " dp[0]=bp[0]\n", + " dp[0].derivedOnly()\n", + "else:\n", + " print \"Cast from Base to Derived failed.\"\n", + " \n", + "bp[0]=d_ob\n", + "if bp[0].__class__.__name__==Derived.__name__:\n", + " dp[0]=bp[0]\n", + " dp[0].derivedOnly()\n", + "else:\n", + " print \"Error, cast should work!\"\n", + " \n", + "#Use dynamic_cast\n", + "\n", + "bp[0]=b_ob\n", + "dp[0]=bp[0]\n", + "if dp[0].__class__.__name__==Derived.__name__:\n", + " dp[0].derivedOnly()\n", + "else:\n", + " print \"Cast from Base to Derived failed.\"\n", + " \n", + "bp[0]=d_ob\n", + "dp[0]=bp[0]\n", + "if dp:\n", + " dp[0].derivedOnly()\n", + "else:\n", + " print \"Error, cast should work!\"\n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Cast from Base to Derived failed.\nIs a Derived Object\nCast from Base to Derived failed.\nIs a Derived Object\n" + "text": [ + "Cast from Base to Derived failed.\n", + "Is a Derived Object\n", + "Cast from Base to Derived failed.\n", + "Is a Derived Object\n" + ] } ], "prompt_number": 9 @@ -175,19 +612,37 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 19.9, Page Number: 467<h3>" + "source": [ + "<h3>Example 19.9, Page Number: 467<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementation of const_cast'''\n#Since python does not have constants, const_cast is not needed\n\ndef f(p):\n v=p\n v[0]=100\n \n#Variable declaration\nx=[]\n\nx.append(99)\nprint \"x before call:\",x[0]\nf(x)\nprint \"x after call:\",x[0]\n", + "input": [ + "\n", + "def f(p):\n", + " v=p\n", + " v[0]=100\n", + " \n", + "#Variable declaration\n", + "x=[]\n", + "\n", + "x.append(99)\n", + "print \"x before call:\",x[0]\n", + "f(x)\n", + "print \"x after call:\",x[0]\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "x before call: 99\nx after call: 100\n" + "text": [ + "x before call: 99\n", + "x after call: 100\n" + ] } ], "prompt_number": 10 @@ -195,19 +650,30 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 19.10, Page Number: 468<h3>" + "source": [ + "<h3>Example 19.10, Page Number: 468<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implement static_cast'''\n#Since python does not have static variables,\n#static_cast is not needed\n\nf=199.22\n\ni=f\nprint i\n", + "input": [ + "\n", + "\n", + "f=199.22\n", + "\n", + "i=f\n", + "print i\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "199.22\n" + "text": [ + "199.22\n" + ] } ], "prompt_number": 11 @@ -215,19 +681,32 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 19.11, Page Number: 469<h3>" + "source": [ + "<h3>Example 19.11, Page Number: 469<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''An example of reinterpret_cast'''\n#python does not have specific data type variables \n#hence reinterpretation is not required\n\n#Variable Declaration\ni=0 #int\np=\"This is a string\"\n\ni=p #cast pointer to integer \n\n#Result\nprint i\n", + "input": [ + "\n", + "i=0 #int\n", + "p=\"This is a string\"\n", + "\n", + "i=p #cast pointer to integer \n", + "\n", + "#Result\n", + "print i\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "This is a string\n" + "text": [ + "This is a string\n" + ] } ], "prompt_number": 12 @@ -235,7 +714,7 @@ { "cell_type": "code", "collapsed": false, - "input": "", + "input": [], "language": "python", "metadata": {}, "outputs": [] |