diff options
author | kinitrupti | 2015-10-15 17:40:30 +0530 |
---|---|---|
committer | kinitrupti | 2015-10-15 17:40:30 +0530 |
commit | 8e4f9ec414f553746330423a974684bbac2163a8 (patch) | |
tree | 7d91d4997a3962c3ad62355ad369f7b6b7738ddb /Schaum's_Outline/ch12.ipynb | |
parent | 79c8f29f70cc014d5fd8b9900d3c19309134bc1d (diff) | |
download | Python-Textbook-Companions-8e4f9ec414f553746330423a974684bbac2163a8.tar.gz Python-Textbook-Companions-8e4f9ec414f553746330423a974684bbac2163a8.tar.bz2 Python-Textbook-Companions-8e4f9ec414f553746330423a974684bbac2163a8.zip |
solved errors
Diffstat (limited to 'Schaum's_Outline/ch12.ipynb')
-rwxr-xr-x | Schaum's_Outline/ch12.ipynb | 869 |
1 files changed, 869 insertions, 0 deletions
diff --git a/Schaum's_Outline/ch12.ipynb b/Schaum's_Outline/ch12.ipynb new file mode 100755 index 00000000..2743b6a5 --- /dev/null +++ b/Schaum's_Outline/ch12.ipynb @@ -0,0 +1,869 @@ +{
+ "metadata": {
+ "name": "ch12"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 12: Composition and Inheritance"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.1, Page no: 273"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",nat=\"U.S.A.\",s=1):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ "\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " \n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ "\n",
+ "creator = Person(\"Bjarne Stroustrup\", \"Denmark\")\n",
+ "print \"The creator of C++ was \" ,\n",
+ "creator.printName() \n",
+ "print \", who was born in \" ,\n",
+ "creator.printNationality() \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The creator of C++ was Bjarne Stroustrup , who was born in Denmark\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.2, Page no: 274"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "peace = Date(11,11,1918)\n",
+ "print \"World War I ended on \" ,\n",
+ "peace.print_()\n",
+ "peace.setDate(8,14,1945)\n",
+ "print \"World War II ended on \" ,\n",
+ "peace.print_()\n",
+ "print \"Enter month, day, and year: \"\n",
+ "date = Date()\n",
+ "date.input()\n",
+ "print \"The date is \" , \n",
+ "date.print_()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "World War I ended on November 11 , 1918\n",
+ "World War II ended on August 14 , 1945\n",
+ "Enter month, day, and year: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1976\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The date is July 4 , 1976\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.3, Page no: 274"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ "\n",
+ "author = Person(\"Thomas Jefferson\", 1)\n",
+ "author.setDOB(4,13,1743)\n",
+ "author.setDOD(7,4,1826)\n",
+ "print \"The author of the Declaration of Independence was \",\n",
+ "author.printName()\n",
+ "print \".\\nHe was born on \",\n",
+ "author.printDOB()\n",
+ "print \" and died on \",\n",
+ "author.printDOD()\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The author of the Declaration of Independence was Thomas Jefferson .\n",
+ "He was born on April 13 , 1743\n",
+ " and died on July 4 , 1826\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.4, Page no: 276"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,n,s=0,i=\"\"):\n",
+ " Person.__init__(self,n,s)\n",
+ " self.id = i\n",
+ " self.credits = 0\n",
+ " self.gpa = 0\n",
+ " self.dom = Date()\n",
+ "\n",
+ " def setDOM(self,m,d,y):\n",
+ " self.dom.setDate(m, d, y)\n",
+ " def printDOM(self):\n",
+ " self.dom.print_()\n",
+ "\n",
+ "x = Student(\"Ann Jones\", 0, \"219360061\")\n",
+ "x.setDOB(5, 13, 1977)\n",
+ "x.setDOM(8, 29, 1995)\n",
+ "x.printName()\n",
+ "print \"\\n\\t Born: \" ,\n",
+ "x.printDOB()\n",
+ "print \"\\n\\tMatriculated: \",\n",
+ "x.printDOM()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Ann Jones \n",
+ "\t Born: May 13 , 1977\n",
+ "\n",
+ "\tMatriculated: August 29 , 1995\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.5, Page no: 276"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,n,s=0,i=\"\"):\n",
+ " Person.__init__(self,n,s)\n",
+ " self.id = i\n",
+ " self.credits = 0\n",
+ " self.gpa = 0\n",
+ " self.dom = Date()\n",
+ "\n",
+ " def setDOM(self,m,d,y):\n",
+ " self.dom.setDate(m, d, y)\n",
+ " def printDOM(self):\n",
+ " self.dom.print_()\n",
+ " def printSex(self):\n",
+ " if self.sex == 1:\n",
+ " print \"male\"\n",
+ " else:\n",
+ " print 'female'\n",
+ "\n",
+ "x = Student(\"Ann Jones\", 0, \"219360061\")\n",
+ "x.setDOB(5, 13, 1977)\n",
+ "x.setDOM(8, 29, 1995)\n",
+ "x.setDOD(7,4,1826)\n",
+ "x.printName()\n",
+ "print \"\\n\\t Born: \" , \n",
+ "x.printDOB()\n",
+ "print \"\\n\\t Sex: \" ,\n",
+ "x.printSex()\n",
+ "print \"\\n\\tMatriculated: \",\n",
+ "x.printDOM()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Ann Jones \n",
+ "\t Born: May 13 , 1977\n",
+ "\n",
+ "\t Sex: female\n",
+ "\n",
+ "\tMatriculated: August 29 , 1995\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.6, Page no: 279"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class X:\n",
+ " def __init__(self):\n",
+ " self.a = 0\n",
+ " def f(self):\n",
+ " print \"X::f() executing\"\n",
+ "class Y(X):\n",
+ " def __init__(self):\n",
+ " self.a = 0\n",
+ " def f(self):\n",
+ " print \"Y::f() executing\"\n",
+ "x = X()\n",
+ "x.a = 22\n",
+ "x.f()\n",
+ "print \"x.a = \" , x.a\n",
+ "y = Y()\n",
+ "y.a = 44\n",
+ "# assigns 44 to the a defined in Y\n",
+ "y._X__a = 66\n",
+ "# assigns 66 to the a defined in X\n",
+ "y.f()\n",
+ "# invokes the f() defined in Y\n",
+ "X.f(x)\n",
+ "# invokes the f() defined in X\n",
+ "print \"y.a = \" , y.a \n",
+ "print \"y._X__a = \" , y._X__a \n",
+ "z = y\n",
+ "print \"z.a = \" , z._X__a \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "X::f() executing\n",
+ "x.a = 22\n",
+ "Y::f() executing\n",
+ "X::f() executing\n",
+ "y.a = 44\n",
+ "y._X__a = 66\n",
+ "z.a = 66\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.7, Page no: 280"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "Note : Python destuctor is called when program goes exit. So output may be differ than c.\n",
+ "'''\n",
+ "class X:\n",
+ " def __init__(self):\n",
+ " print \"X::X() constructor executing \"\n",
+ " def __del__(self):\n",
+ " print \"X::X() destructor executing \"\n",
+ "\n",
+ "class Y(X):\n",
+ " def __init__(self):\n",
+ " X.__init__(self)\n",
+ " print \"Y::Y() constructor executing \"\n",
+ " def __del__(self):\n",
+ " print \"Y::Y() destructor executing \"\n",
+ "\n",
+ "class Z(Y):\n",
+ " def __init__(self,i):\n",
+ " Y.__init__(self)\n",
+ " print \"Z::Z(\" , i , \") constructor executing \"\n",
+ " def __del__(self):\n",
+ " print \"Z::Z() destructor executing \"\n",
+ " \n",
+ "\n",
+ "Z = Z(44)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "X::X() constructor executing \n",
+ "Y::Y() constructor executing \n",
+ "Z::Z( 44 ) constructor executing \n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.8, Page no: 281"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "Note : Python destuctor is called when program goes exit. So output may be differ than c.\n",
+ "'''\n",
+ "class Person:\n",
+ " def __init__(self,s):\n",
+ " self.name = s\n",
+ " def __del__(self):\n",
+ " pass\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,s,m):\n",
+ " Person.__init__(self,s)\n",
+ " self.major = m\n",
+ " def __del__(self):\n",
+ " pass\n",
+ "x = Person(\"Bob\")\n",
+ "y = Student(\"Sarah\", \"Biology\")\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.9, Page no: 282"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ " def setHSgraduate(self,g):\n",
+ " self.hs = g\n",
+ " def isHSgraduate(self):\n",
+ " return hs\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.10, Page no: 283"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "Note : By default all methods in python are virtual. so output would be differ than c.\n",
+ "'''\n",
+ "class X:\n",
+ " def f(self):\n",
+ " print \"X::f() executing\"\n",
+ "class Y(X):\n",
+ " def f(self):\n",
+ " print \"Y::f() executing\"\n",
+ "x = X()\n",
+ "y = Y()\n",
+ "p = x\n",
+ "p.f()\n",
+ "p = y\n",
+ "p.f()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "X::f() executing\n",
+ "Y::f() executing\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.11, Page no: 284"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n):\n",
+ " self.name = n\n",
+ " def print_(self):\n",
+ " print 'My name is' , self.name\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,s,g):\n",
+ " Person.__init__(self,s)\n",
+ " self.gpa = g\n",
+ " def print_(self):\n",
+ " print 'My name is ',self.name, ' and my G.P.A. is', self.gpa\n",
+ "\n",
+ "class Professor(Person):\n",
+ " def __init__(self,s,n):\n",
+ " Person.__init__(self,s)\n",
+ " self.publs = n\n",
+ " def print_(self):\n",
+ " print 'My name is ', self.name,' and i have ' , self.publs,' publications.'\n",
+ "\n",
+ "x = Person(\"Bob\")\n",
+ "p = x\n",
+ "p.print_()\n",
+ "y = Student(\"Tom\", 3.47)\n",
+ "p = y\n",
+ "p.print_()\n",
+ "z = Professor(\"Ann\", 7)\n",
+ "p = z\n",
+ "p.print_()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "My name is Bob\n",
+ "My name is Tom and my G.P.A. is 3.47\n",
+ "My name is Ann and i have 7 publications.\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.12, Page no: 285"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "This program is similar to Example 12.6:\n",
+ "'''\n",
+ "class X:\n",
+ " def __init__(self):\n",
+ " self.p = [0,0]\n",
+ " print 'X().',\n",
+ " def __del__(self):\n",
+ " print '~X().'\n",
+ "\n",
+ "class Y(X):\n",
+ " def __init__(self):\n",
+ " X.__init__(self)\n",
+ " self.q = []\n",
+ " for i in range(1023):\n",
+ " self.q.append(0)\n",
+ " print 'Y() : Y::q = ', hex(id(self.q)) ,'.',\n",
+ " def __del__(self):\n",
+ " print '~Y().'\n",
+ "\n",
+ "for i in range(8):\n",
+ " r = Y()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n",
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n",
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n",
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.13, Page no: 288"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Media:\n",
+ " def __init__(self):\n",
+ " self.title = ''\n",
+ " def print_(self):\n",
+ " pass\n",
+ " def id(self):\n",
+ " pass\n",
+ "\n",
+ "class Book(Media):\n",
+ " def __init__(self,a='',t=\"\",p=\"\",i=\"\"):\n",
+ " self.author = a\n",
+ " self.publisher = p\n",
+ " self.isbn = i\n",
+ " self.title = t\n",
+ " def print_(self):\n",
+ " print self.title , \" by \" , self.author\n",
+ " def id(self):\n",
+ " return self.isbn\n",
+ "\n",
+ "class CD(Media):\n",
+ " def __init__(self,t=\"\",c=\"\",m=\"\",n=\"\"):\n",
+ " self.composer = c\n",
+ " self.make = m\n",
+ " self.number = n\n",
+ " self.title = t\n",
+ " def print_(self):\n",
+ " print self.title , \", \" , self.composer\n",
+ " def id(self):\n",
+ " s = str(self.make) + ' ' + str(self.number)\n",
+ " return s\n",
+ "\n",
+ "class Magazine(Media):\n",
+ " def __init__(self,t=\"\",i=\"\",v=0, n=0):\n",
+ " self.issn = i\n",
+ " self.volume = v\n",
+ " self.number = n\n",
+ " self.title = t\n",
+ " def print_(self):\n",
+ " print self.title , \" Magazine, Vol. \", self.volume , \", No.\" , self.number\n",
+ " def id(self):\n",
+ " return self.issn\n",
+ "\n",
+ "book = Book(\"Bjarne Stroustrup\", \"The C++ Programming Language\",\"Addison-Wesley\", \"0-201-53992-6\")\n",
+ "magazine = Magazine(\"TIME\", \"0040-781X\", 145, 23)\n",
+ "cd = CD(\"BACH CANTATAS\", \"Johann Sebastian Bach\",\"ARCHIV\", \"D120541\")\n",
+ "book.print_()\n",
+ "print \"\\tid: \" , book.id() \n",
+ "magazine.print_()\n",
+ "print \"\\tid: \" , magazine.id() \n",
+ "cd.print_()\n",
+ "print \"\\tid: \" , cd.id()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The C++ Programming Language by Bjarne Stroustrup\n",
+ "\tid: 0-201-53992-6\n",
+ "TIME Magazine, Vol. 145 , No. 23\n",
+ "\tid: 0040-781X\n",
+ "BACH CANTATAS , Johann Sebastian Bach\n",
+ "\tid: ARCHIV D120541\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |