December 2012
Beginner
552 pages
12h 58m
English
We noted that the add: method changes the value of the object that is receiving the message. Let’s create a new version of add: that instead makes a new fraction to store the result of the addition. In this case, we need to return the new Fraction to the message sender. Here is the definition for the new add: method:
-(Fraction *) add: (Fraction *) f{ // To add two fractions: // a/b + c/d = ((a*d) + (b*c)) / (b * d) // result will store the result of the addition Fraction *result = [[Fraction alloc] init]; result.numerator = numerator * f.denominator + denominator * f.numerator; result.denominator = denominator * f.denominator; [result reduce]; return result; ...
Read now
Unlock full access