In classical inheritance, the programmer writes a class, which defines an object. Multiple objects can be instantiated from the same class, so you have code in one place which describes several objects in your program. Classes can then be organized into a hierarchy, furthering code reuse. More general code is stored in a higher-level class, from which lower level classes inherit. This means that an object is sharing code with other objects of the same class, as well as with its parent classes.
Given a numerator and a denominator create a new Fraction.
When a fraction is printed it should be simplified.
Two fractions can be added or subtracted
Two fractions can be multiplied or divided
Two fractions can be compared
A fraction and an integer can be added together.
Given a list of Fractions that list should be sortable by the default sorting function.
Here is a mostly complete implementation of a Fraction class in Python that we will refer to throughout this section:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
class Fraction: def __init__(self,top,bottom): self.num = top #the numerator is on top self.den = bottom #the denominator is on the bottom def __repr__(self): if self.num > self.den: retWhole = self.num / self.den retNum = self.num - (retWhole * self.den) return str(retWhole) + " " + str(retNum)+"/"+str(self.den) else: return str(self.num)+"/"+str(self.den) def show(self): print self.num,"/",self.den def __add__(self,otherfraction): # convert to a fraction otherfraction = self.toFract(otherfraction) newnum = self.num*otherfraction.den + self.den*otherfraction.num newden = self.den * otherfraction.den common = gcd(newnum,newden) return Fraction(newnum/common,newden/common) def __radd__(self,leftNum): otherfraction = self.toFract(leftNum) newnum = self.num*otherfraction.den + self.den*otherfraction.num newden = self.den * otherfraction.den common = gcd(newnum,newden) return Fraction(newnum/common,newden/common) def __cmp__(self,otherfraction): num1 = self.num*otherfraction.den num2 = self.den*otherfraction.num if num1 < num2: return -1 else: if num1 == num2: return 0 else: return 1 def toFract(self,n): if isinstance(n,int): otherfraction = Fraction(n,1) elif isinstance(n, float): wholePart = int(n) fracPart = n - wholePart # convert to 100ths??? fracNum = int(fracPart * 100) newNum = wholePart * 100 + fracNum otherfraction = Fraction(newNum,100) elif isinstance(n,Fraction): otherfraction = n else: print "Error: cannot add a fraction to a ", type(n) return None return otherfraction #gcd is a helper function for Fraction def gcd(m,n): while m%n != 0: oldm = m oldn = n m = oldn n = oldm%oldn return n
The instance variables (data members) we will need for our fraction
class are the numerator and denominator. Of course in Python we can add
instance variables to a class at any time by simply assigning a value to
3.1.1. Methods or Member Functions¶
Adding methods to your class is easy and the syntax is very clean. But it may feel a little strange to not have the word function or def in front of the method name. In all method the word
this refers to the current object. Just like
self does in Python. You will notice that you do NOT have to supply this as the first parameter to each method!