Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 09:09:13 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: One more question on 7.5  (Read 1482 times)
susomora
Newbie
*
Posts: 10


« on: August 11, 2010, 10:35:45 PM »

Hi everybody , this is my first post ... so there I go.
What would be the side effect (if any ) of implementing the add method in this way ? (obviously after adapting main to the new implementation).

-(Fraction *) add: (Fraction *) f
{
   Fraction *result=[[Fraction alloc] init];
   
   result.numerator= ((numerator*f.denominator)+(denominator*f.numerator));
   result.denominator= denominator*f.denominator;
   
   [result reduce];
   return result;
}


The main routine would be like this :

#import "Fraction.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   Fraction *aFraction = [[Fraction alloc] init];
   Fraction *bFraction = [[Fraction alloc] init];
   
   Fraction *resultFraction;
   
   //set aFraction to 3/4
   //set bFraction to 5/8
   
    [aFraction setTo:3 over:4];
   [bFraction setTo:5 over:8];
   
   //display the fraction
   resultFraction = [aFraction add:bFraction];
   
    [aFraction print];
   NSLog(@"+");
   [bFraction print];
   NSLog (@"=");
   [resultFraction  print];
   
   [aFraction release];
   [bFraction release];
   [resultFraction release];
   
   
   [pool drain];
    return 0;
}


thanx in advance.
SUSO
« Last Edit: August 11, 2010, 10:44:36 PM by susomora » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: August 14, 2010, 08:13:18 AM »

This version of the add: method is implemented later in the chapter.

Cheers,

Steve Kochan
Logged
susomora
Newbie
*
Posts: 10


« Reply #2 on: August 14, 2010, 12:51:36 PM »

Do you mean later in this same chapter ? . I must be having an Objective-C OD ... I can't find it  Huh . I'd better take a break .

Cheers.
Suso.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #3 on: August 15, 2010, 10:58:34 AM »

Oops!  Sorry.  I thought you meant implementing a version that returns the result, not one that assigns the results directly to the fraction's numerator and denominator.

Yes, your version is fine.  In fact, I am likely to use that version in the third edition of the book.

Cheers,

Steve
Logged
susomora
Newbie
*
Posts: 10


« Reply #4 on: August 18, 2010, 01:29:28 AM »

OK , thanks Steve.

Suso.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.