Skip to content

Commit

Permalink
learn objective-c
Browse files Browse the repository at this point in the history
  • Loading branch information
halida committed Sep 27, 2011
1 parent 649119c commit 0e7cee4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions objective-c/hello/hello.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#import <stdio.h>

int main(int argc, const char *argv[]){
printf("hello\n");
return 0;
}
13 changes: 13 additions & 0 deletions objective-c/interface/Fraction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import <Foundation/NSObject.h>

@interface Fraction: NSObject {
int numerator;
int denominator;
}

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
24 changes: 24 additions & 0 deletions objective-c/interface/Fraction.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#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
21 changes: 21 additions & 0 deletions objective-c/interface/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#import <stdio.h>
#import "Fraction.h"

int main( int argc, const char *argv[] ) {
// create a new instance
Fraction *frac = [[Fraction alloc] init];

// 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;
}
4 changes: 4 additions & 0 deletions objective-c/interface/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
run:
gcc -c Fraction.m -I /usr/include/GNUstep/
gcc main.m Fraction.o -o s -I /usr/include/GNUstep/
./s

0 comments on commit 0e7cee4

Please sign in to comment.