Created
June 14, 2014 23:43
-
-
Save dgutov/889fc5fc534e082862e8 to your computer and use it in GitHub Desktop.
Objective-C sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/NSObject.h> | |
@interface Fraction: NSObject { | |
int numerator; | |
int denominator; | |
} | |
-(void) print; | |
-(void) setNumerator: (int) n; | |
-(void) setDenominator: (int) d; | |
-(void) setNumerator: (int) n andDenominator: (int) d; | |
-(int) numerator; | |
-(int) denominator; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Fraction.h" | |
#import <stdio.h> | |
@implementation Fraction | |
-(void) print { | |
printf( "%i/%i", numerator, denominator ); | |
} | |
-(void) setNumerator: (int) n { | |
numerator = n; | |
} | |
-(void) setDenominator: (int) d { | |
denominator = d; | |
} | |
-(int) denominator { | |
return denominator; | |
} | |
-(int) numerator { | |
return numerator; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <stdio.h> | |
#import "Fraction.h" | |
int main( int argc, const char *argv[] ) { | |
// create a new instance | |
Fraction *frac = [[Fraction alloc] init]; | |
[frac set]; | |
// set the values | |
[frac setNumerator: 1]; | |
[frac setDenominator: 3]; | |
// print it | |
printf( "The fraction is: " ); | |
[frac print]; | |
printf( "\n" ); | |
// free memory | |
[frac release]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment