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:
1class Fraction: 2 3 def __init__(self,top,bottom): 4 5 self.num = top #the numerator is on top 6 self.den = bottom #the denominator is on the bottom 7 8 9 def __repr__(self): 10 if self.num > self.den: 11 retWhole = self.num / self.den 12 retNum = self.num - (retWhole * self.den) 13 return str(retWhole) + " " + str(retNum)+"/"+str(self.den) 14 else: 15 return str(self.num)+"/"+str(self.den) 16 17 def show(self): 18 print self.num,"/",self.den 19 20 def __add__(self,otherfraction): 21 # convert to a fraction 22 otherfraction = self.toFract(otherfraction) 23 24 newnum = self.num*otherfraction.den + self.den*otherfraction.num 25 newden = self.den * otherfraction.den 26 27 common = gcd(newnum,newden) 28 29 return Fraction(newnum/common,newden/common) 30 31 def __radd__(self,leftNum): 32 otherfraction = self.toFract(leftNum) 33 newnum = self.num*otherfraction.den + self.den*otherfraction.num 34 newden = self.den * otherfraction.den 35 36 common = gcd(newnum,newden) 37 38 return Fraction(newnum/common,newden/common) 39 40 def __cmp__(self,otherfraction): 41 42 num1 = self.num*otherfraction.den 43 num2 = self.den*otherfraction.num 44 45 if num1 < num2: 46 return -1 47 else: 48 if num1 == num2: 49 return 0 50 else: 51 return 1 52 53 def toFract(self,n): 54 if isinstance(n,int): 55 otherfraction = Fraction(n,1) 56 elif isinstance(n, float): 57 wholePart = int(n) 58 fracPart = n - wholePart 59 # convert to 100ths??? 60 fracNum = int(fracPart * 100) 61 newNum = wholePart * 100 + fracNum 62 otherfraction = Fraction(newNum,100) 63 elif isinstance(n,Fraction): 64 otherfraction = n 65 else: 66 print "Error: cannot add a fraction to a ", type(n) 67 return None 68 return otherfraction 69 70#gcd is a helper function for Fraction 71 72def gcd(m,n): 73 while m%n != 0: 74 oldm = m 75 oldn = n 76 77 m = oldn 78 n = oldm%oldn 79 80 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!