diff options
author | Jovina Dsouza | 2014-07-08 17:19:20 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-07-08 17:19:20 +0530 |
commit | 84eaa5fbfb3090e912ebe12de1906b7c0bdde908 (patch) | |
tree | f98362fb5e4e43848c150834c3937dc4bde1e176 /C++_from_the_Ground/Chapter_15.ipynb | |
parent | 80751050da776de062000a7d2a5b4e045bfbc9f8 (diff) | |
download | Python-Textbook-Companions-84eaa5fbfb3090e912ebe12de1906b7c0bdde908.tar.gz Python-Textbook-Companions-84eaa5fbfb3090e912ebe12de1906b7c0bdde908.tar.bz2 Python-Textbook-Companions-84eaa5fbfb3090e912ebe12de1906b7c0bdde908.zip |
adding book
Diffstat (limited to 'C++_from_the_Ground/Chapter_15.ipynb')
-rw-r--r-- | C++_from_the_Ground/Chapter_15.ipynb | 427 |
1 files changed, 427 insertions, 0 deletions
diff --git a/C++_from_the_Ground/Chapter_15.ipynb b/C++_from_the_Ground/Chapter_15.ipynb new file mode 100644 index 00000000..211b1c7d --- /dev/null +++ b/C++_from_the_Ground/Chapter_15.ipynb @@ -0,0 +1,427 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:1198072630a182533bf651767f275df9ee5758e0d781322254bb408a8b4cffaa" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h1>Chapter 15: Virtual Functions and Polymorphism<h1>" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 15.1, Page Number: 358<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "\n", + "class B_class:\n", + " def __init__(self):\n", + " self.author=None\n", + " def put_author(self,s):\n", + " self.author=s\n", + " def show_author(self):\n", + " print self.author\n", + " \n", + "class D_class(B_class):\n", + " def __init__(self):\n", + " self.title=None\n", + " def put_title(self,num):\n", + " self.title=num\n", + " def show_title(self):\n", + " print \"Title:\",self.title\n", + " \n", + "#Variable declaration\n", + "p=[B_class()] #acts as a pointer to B_class type\n", + "B_ob=B_class()\n", + "\n", + "dp=[D_class()] #acts as a pointer to D_class type\n", + "D_ob=D_class()\n", + "\n", + "p[0]=B_ob #assigning p to object of base\n", + "\n", + "\n", + "#Access B_class via pointer\n", + "p[0].put_author(\"Tom Clancy\")\n", + "\n", + "#Access D_class via base pointer\n", + "p[0]=D_ob\n", + "p[0].put_author(\"William Shakespeare\")\n", + "\n", + "#Show that each author went into proper object\n", + "B_ob.show_author()\n", + "D_ob.show_author()\n", + "print \"\\n\"\n", + "\n", + "#Since put_title() and show_title() are not part of the base class, \n", + "#they are not accessible via the base pointer p and must be accessed \n", + "#either directly, or, as shown here, through a pointer to the \n", + "#derived type\n", + "dp[0]=D_ob\n", + "dp[0].put_title(\"The Tempest\")\n", + "p[0].show_author()\n", + "dp[0].show_title()\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Tom Clancy\n", + "William Shakespeare\n", + "\n", + "\n", + "William Shakespeare\n", + "Title: The Tempest\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 15.2, Page Number: 361<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "class base:\n", + " def who(self): #virtual function\n", + " print \"Base\"\n", + "\n", + "class first_d(base):\n", + " def who(self): #redifine who() relative to first_d\n", + " print \"First derivation\"\n", + " \n", + "class second_d(base):\n", + " def who(self): #redifine who() relative to second_d\n", + " print \"Second derivation\"\n", + " \n", + " \n", + "#Variable declaration\n", + "base_obj=base()\n", + "p=[base()]\n", + "first_obj=first_d()\n", + "second_obj=second_d()\n", + "\n", + "p[0]=base_obj\n", + "p[0].who() #access base's who\n", + "\n", + "p[0]=first_obj\n", + "p[0].who() #access first_d's who\n", + "\n", + "p[0]=second_obj\n", + "p[0].who() #access second_d's who\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Base\n", + "First derivation\n", + "Second derivation\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 15.3, Page Number: 363<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "class base:\n", + " def who(self): #virtual function\n", + " print \"Base\"\n", + "\n", + "class first_d(base):\n", + " def who(self): #redifine who() relative to first_d\n", + " print \"First derivation\"\n", + " \n", + "class second_d(base):\n", + " #who not defined\n", + " pass\n", + " \n", + " \n", + "#Variable declaration\n", + "base_obj=base()\n", + "p=[base()]\n", + "first_obj=first_d()\n", + "second_obj=second_d()\n", + "\n", + "p[0]=base_obj\n", + "p[0].who() #access base's who\n", + "\n", + "p[0]=first_obj\n", + "p[0].who() #access first_d's who\n", + "\n", + "p[0]=second_obj\n", + "p[0].who() #access base's who because\n", + " #second_d does not redefine it.\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Base\n", + "First derivation\n", + "Base\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 15.4, Page Number: 364<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "class base:\n", + " def who(self): #virtual function\n", + " print \"Base\"\n", + "\n", + "class first_d(base):\n", + " def who(self): #redifine who() relative to first_d\n", + " print \"First derivation\"\n", + " \n", + "#second_d now inherited first_d -- not base\n", + "class second_d(first_d):\n", + " #who not defined\n", + " pass\n", + " \n", + " \n", + "#Variable declaration\n", + "base_obj=base()\n", + "p=[base()]\n", + "first_obj=first_d()\n", + "second_obj=second_d()\n", + "\n", + "p[0]=base_obj\n", + "p[0].who() #access base's who\n", + "\n", + "p[0]=first_obj\n", + "p[0].who() #access first_d's who\n", + "\n", + "p[0]=second_obj\n", + "p[0].who() #access first_d's who because\n", + " #second_d does not redefine it.\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Base\n", + "First derivation\n", + "First derivation\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 15.5, Page Number: 366<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class figure:\n", + " def __init__(self):\n", + " self._x=None\n", + " self._y=None\n", + " def set_dim(self,i,j):\n", + " self._x=i\n", + " self._y=j\n", + " def show_area(self):\n", + " print \"No area computation defined\",\n", + " print \"for this class.\"\n", + " \n", + "class triangle(figure):\n", + " def show_area(self):\n", + " print \"Triangle with height\",\n", + " print self._x,\"and base\",self._y,\n", + " print \"has an area of\",\n", + " print self._x*0.5*self._y,\".\"\n", + " \n", + "class rectangle(figure):\n", + " def show_area(self):\n", + " print \"Rectangle with dimensions\",\n", + " print self._x,\"x\",self._y,\n", + " print \"has an area of\",\n", + " print self._x*self._y,\".\"\n", + " \n", + "#Variable declaration\n", + "p=[figure()] #pointer to base type\n", + "t=triangle() #objects of derived type\n", + "r=rectangle()\n", + "\n", + "p[0]=t\n", + "p[0].set_dim(10.0,5.0)\n", + "p[0].show_area()\n", + "\n", + "p[0]=r\n", + "p[0].set_dim(10.0,5.0)\n", + "p[0].show_area()\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Triangle with height 10.0 and base 5.0 has an area of 25.0 .\n", + "Rectangle with dimensions 10.0 x 5.0 has an area of 50.0 .\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 15.6, Page Number: 368<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class figure:\n", + " def __init__(self):\n", + " self._x=None\n", + " self._y=None\n", + " def set_dim(self,i,j=0):\n", + " self._x=i\n", + " self._y=j\n", + " def show_area(self):\n", + " print \"No area computation defined\",\n", + " print \"for this class.\"\n", + " \n", + "class triangle(figure):\n", + " def show_area(self):\n", + " print \"Triangle with height\",\n", + " print self._x,\"and base\",self._y,\n", + " print \"has an area of\",\n", + " print self._x*0.5*self._y,\".\"\n", + " \n", + "class rectangle(figure):\n", + " def show_area(self):\n", + " print \"Rectangle with dimensions\",\n", + " print self._x,\"x\",self._y,\n", + " print \"has an area of\",\n", + " print self._x*self._y,\".\"\n", + " \n", + "class circle(figure):\n", + " def show_area(self):\n", + " print \"Circle with radius\",\n", + " print self._x,\n", + " print \"has an area of\",\n", + " print 3.14*self._x*self._x,\".\"\n", + " \n", + " \n", + "#Variable declaration\n", + "p=[figure()] #pointer to base type\n", + "t=triangle() #objects of derived type\n", + "r=rectangle()\n", + "c=circle()\n", + "\n", + "p[0]=t\n", + "p[0].set_dim(10.0,5.0)\n", + "p[0].show_area()\n", + "\n", + "p[0]=r\n", + "p[0].set_dim(10.0,5.0)\n", + "p[0].show_area()\n", + "\n", + "p[0]=c\n", + "p[0].set_dim(9.0)\n", + "p[0].show_area()\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Triangle with height 10.0 and base 5.0 has an area of 25.0 .\n", + "Rectangle with dimensions 10.0 x 5.0 has an area of 50.0 .\n", + "Circle with radius 9.0 has an area of 254.34 .\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |