Sunday, August 30, 2009

working with class and creating object

working with classes  wrong in objective c we are working with interface that is similarly like classes in  java and c++

*************************************************************************

//creating a class sorry! interface implementation and object

 

#import <Foundation/Foundation.h>
//---- @interface section ----
@interface Fraction: NSObject
{
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
@end

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

@implementation Fraction
-(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[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *myFraction;
// Create an instance of a Fraction
myFraction = [Fraction alloc];
myFraction = [myFraction init];
// Set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];
// Display the fraction using the print method
NSLog (@"The value of myFraction is:");
[myFraction print];
[myFraction release];
[pool drain];
return 0;
}

*******************************************************************************

creating file as  prog301.m using vi or ascii text editor

create anather file called   GNUmakefile without extension

##########################################

include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME = LogTest
LogTest_OBJC_FILES = prog301.m
include $(GNUSTEP_MAKEFILES)/tool.make

############################################

LogTest_OBJC_FILES = prog301.m specify the file that is to be compiled.

compile the file

$ make

running the command

$ ./obj/LogTest

 

sourcecode:

  http://sites.google.com/site/phoneprogramming/objc_prog301.zip

No comments:

Post a Comment