Sunday, August 30, 2009

working with different file

 

we are going to use three different file

 

Fraction.h for creating interface

Fraction.m for creating implementation

FractionTest.m  contain main method to implement the Fraction interface

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

Fraction.m

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

//
// Fraction.h
// FractionTest
//
// Created by Steve Kochan on 7/5/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
// The Fraction class
@interface Fraction : NSObject
{
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
-(double) convertToNum;
@end

 

 

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

fraction.m

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

//
// Fraction.m
// FractionTest
//
// Created by Steve Kochan on 7/5/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
-(void) print
{
NSLog (@"%i/%i", numerator, denominator);
}
-(void) setNumerator: (int) n
{
numerator = n;
}
-(void) setDenominator: (int) d
{
denominator = d;
}
-(int) numerator
{
return numerator;
}
-(int) denominator
{
return denominator;
}
-(double) convertToNum
{

if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}
@end

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

FractionTest.m

#import "fraction.h"
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *myFraction = [[Fraction alloc] init];
// set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];
// display the fraction
NSLog (@"The value of myFraction is:");
[myFraction print];
[myFraction release];
[pool drain];
return 0;
}

 

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

GNUmakefile

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

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

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

//to run in MAC Ox

gcc –framework Foundation Fraction.m FractionTest.m –o FractionTest

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

 

source code download

No comments:

Post a Comment