Re: Interface and Implementation File Placements
Re: Interface and Implementation File Placements
- Subject: Re: Interface and Implementation File Placements
- From: "Ryan Chapman" <email@hidden>
- Date: Sun, 8 Jun 2008 14:20:27 -0400
Chapter 7?
Kochan is talking about taking the Fraction class and placing the interface in Fraction.h and the implementation in Fraction.m, leaving only the main() function in main.m. If you were really sloppy, you could put it all in main.m:
main.m (sloppy):
#import <stdio.h>
#import <objc/Object.h>
@interface Fraction: Object
{
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator:(int)n;
-(void) setDenominator:(int)d;
-(int) numerator;
-(int) denominator;
-(double) convertToNum;
@end
@implementation Fraction;
-(void) print
{
printf(" %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
int main(int argc, char *argv[])
{
Fraction *myFraction = [[Fraction alloc] init];
[myFraction setNumerator:1];
[myFraction setDenominator:3];
printf("The value of myFraction is:");
[myFraction print];
printf("\n");
[myFraction free];
return 0;
}
Now, separating the interface and implementation from main.m:
main.m:
#import <stdio.h>
int main(int argc, char *argv[])
{
Fraction *myFraction = [[Fraction alloc] init];
[myFraction setNumerator:1];
[myFraction setDenominator:3];
printf("The value of myFraction is:");
[myFraction print];
printf("\n");
[myFraction free];
return 0;
}
Fraction.h:#import <objc/Object.h>
@interface Fraction: Object
{
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator:(int)n;
-(void) setDenominator:(int)d;
-(int) numerator;
-(int) denominator;
-(double) convertToNum;
@end
Fraction.m:
#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) numerator
{
return numerator;
}
-(int) denominator
{
return denominator;
}
-(double) convertToNum
{
if(denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}
@end
On Sat, Jun 7, 2008 at 9:46 PM, Stephen Fahy <
email@hidden> wrote:
I am using X-Code 10.5. I am a new user. I am following Stephen Kochan's Programming in Objective-C. I am writing a program that I want to separate the Interface and Implementation files into their own file, rather than have them in the main.m file. Where should they be placed?
Steve Fahy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden