Amazon.com Widgets exercise 17-03
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 11:42:57 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 17
| | | |-+  exercise 17-03
Pages: [1]   Go Down
Print
Author Topic: exercise 17-03  (Read 716 times)
mdziedzic
Newbie
*
Posts: 41



WWW




« on: January 14, 2010, 11:08:06 AM »

Just the changed parts of the Fraction class:

Code: (Objective-C)
@implementation Fraction (MathOps)

-(Fraction *) add: (Fraction *) f
{
// To add two fractions
// a/b + c/d = ((a*d) + (b*c)) / (b*d)

// result will store the results of the addition
Fraction *result = [[[Fraction alloc] init] autorelease];
int resultNum, resultDenom;


resultNum = numerator * f.denominator + denominator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
[result reduce];

return result;
}



-(Fraction *) subtract: (Fraction *) f
{
Fraction *result = [[[Fraction alloc] init] autorelease];
int resultNum, resultDenom;

resultNum = numerator * f.denominator - denominator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
[result reduce];

return result;
}



-(Fraction *) multiply: (Fraction *) f
{
Fraction *result = [[[Fraction alloc] init] autorelease];
int resultNum, resultDenom;

resultNum = numerator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
[result reduce];

return result;
}



-(Fraction *) divide: (Fraction *) f
{
Fraction *result = [[[Fraction alloc] init] autorelease];
int resultNum, resultDenom;

resultNum = numerator * f.denominator;
resultDenom = denominator * f.numerator;

[result setTo: resultNum over: resultDenom];
[result reduce];

return result;
}

@end


Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Fraction.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Fraction *fractionA = [[[Fraction alloc] init] autorelease];
Fraction *fractionB = [[[Fraction alloc] init] autorelease];

[fractionA setTo: 3 over: 4];
[fractionB setTo: 1 over: 8];

[[fractionA add: fractionB] print];
   
[pool drain];
    return 0;
}



7/8



In main, both fractionA and fractionB were added to the autorelease pool when created, and will be destroyed when the autorelease pool drains at the end of main. No need to release them explicitly.

In the four methods of Fraction (MathOps), result was added to the autorelease pool when it was created and is returned when the method is invoked, for example: [fractionA add: fractionB]. As this returned Fraction object was marked for autorelease before being returned, it too will be destroyed when the autorelease pool drains at the end of main. Again, no need to release it explicitly.

Indeed, explicitly releasing either fractionA and fractionB, or any of the result objects in Fraction (MathOps) would cause the program to crash, as they would no longer be around for destruction by the autorelease pool.

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



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.