Operations on Fractions
Let’s continue to work with the Fraction
class. First, you write a method that enables you to add one fraction to another. You name the method add:
, and you have it take a fraction as an argument. Here’s the declaration for the new method:
-(void) add: (Fraction *) f;
Note the declaration for the argument f
:
(Fraction *) f
This says that the argument to the add:
method is a reference to an object from the Fraction
class. The asterisk is necessary, so the following declaration is not correct:
(Fraction) f
You will be passing one fraction as an argument to your add:
method, and you’ll have the method add it to the receiver of the message; the following message expression adds the Fraction bFraction
to the Fraction aFraction ...
Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.