a13x612

Feb 6, 2012, 07:17 PM

This is my first post on here and i just want to start off by saying hi! Now onto my problem...

Below is the code that I put into Xcode 4.2 on my 32-bit macbook running snow leopard:

#import <Foundation/Foundation.h>

// ---- @interface section ----

@interface Fraction: NSObject

-(void) print;

-(void) setNumerator: (int) n;

-(void) setDenominator: (int) d;

@end

// ---- @implementation section ----

@implementation Fraction

{

int numerator;

int denominator;

}

-(void) print

{

NSLog(@"%i/%i", numerator, denominator);

}

-(void) setNumerator: (int) n

{

numerator = n;

}

-(void) setDenominator: (int) d

{

denominator = d;

}

@end

// ---- program section ----

int main (int argc, char * argv[])

{

@autoreleasepool {

Fraction *frac1 = [[Fraction alloc] init];

Fraction *frac2 = [[Fraction alloc] init];

// Set 1st fraction to 2/3

[frac1 setNumerator: 2];

[frac1 setDenominator: 3];

//Set 2nd fraction to 3/7

[frac2 setNumerator: 3];

[frac2 setDenominator: 7];

//Display the fractions

NSLog (@"First fraction is:");

[frac1 print];

NSLog (@"Second fraction is:");

[frac2 print];

//Issue here is the last values in setNumerator/setDenominator overwrite all.

}

return 0;

}

When trying to build/run, I get a lot of errors (semantic errors, parse error). So I messed around with the code, took out the curly braces around the member declarations under @implementation and it worked (sort of). Although the program ran, This was the output:

First fraction is 3/7

Second fraction is 3/7

Can anyone shed a little light on why this is? I am getting the feeling that the program is just overwriting whatever is stored for frac1.

Thanks!

Below is the code that I put into Xcode 4.2 on my 32-bit macbook running snow leopard:

#import <Foundation/Foundation.h>

// ---- @interface section ----

@interface Fraction: NSObject

-(void) print;

-(void) setNumerator: (int) n;

-(void) setDenominator: (int) d;

@end

// ---- @implementation section ----

@implementation Fraction

{

int numerator;

int denominator;

}

-(void) print

{

NSLog(@"%i/%i", numerator, denominator);

}

-(void) setNumerator: (int) n

{

numerator = n;

}

-(void) setDenominator: (int) d

{

denominator = d;

}

@end

// ---- program section ----

int main (int argc, char * argv[])

{

@autoreleasepool {

Fraction *frac1 = [[Fraction alloc] init];

Fraction *frac2 = [[Fraction alloc] init];

// Set 1st fraction to 2/3

[frac1 setNumerator: 2];

[frac1 setDenominator: 3];

//Set 2nd fraction to 3/7

[frac2 setNumerator: 3];

[frac2 setDenominator: 7];

//Display the fractions

NSLog (@"First fraction is:");

[frac1 print];

NSLog (@"Second fraction is:");

[frac2 print];

//Issue here is the last values in setNumerator/setDenominator overwrite all.

}

return 0;

}

When trying to build/run, I get a lot of errors (semantic errors, parse error). So I messed around with the code, took out the curly braces around the member declarations under @implementation and it worked (sort of). Although the program ran, This was the output:

First fraction is 3/7

Second fraction is 3/7

Can anyone shed a little light on why this is? I am getting the feeling that the program is just overwriting whatever is stored for frac1.

Thanks!