It's not seperated into separate files, but I think this is the idea:
// Chapter 8 Exercise 2
// When dealing with higher-resolution devices, you might need to use a coordinate
// system that enables you to specify points as floating-point values instead of
// as simple integers (iOS uses a structure called CGRect for working with
// rectangles. All coordinates and sizes are expressed as floating point numbers
// when working with such rectangles). Modify the XYPoint and Rectangle classes
// from this chapter to deal with floating-point numbers.The rectangle’s width,
// height, area, and perimeter should all work with floating-point numbers as well.
@interface XYPoint: NSObject
@property float x, y;
-(void) setX: (float) xVal andY: (float) yVal;
@end
@implementation XYPoint
@synthesize x, y;
-(void) setX: (float) xVal andY: (float) yVal
{
x = xVal;
y = yVal;
}
@end
@interface Rectangle: XYPoint
@property float width, height;
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) setWidth: (float) w andHeight: (float) h;
-(int) area;
-(int) perimeter;
@end
@implementation Rectangle
{
XYPoint *origin;
}
@synthesize width, height;
-(void) setWidth: (float) w andHeight: (float) h
{
width = w;
height = h;
}
-(void) setOrigin: (XYPoint *) pt
{
if (! origin)
origin = [[XYPoint alloc] init];
origin.x = pt.x;
origin.y = pt.y;
}
-(int) area
{
return width * height;
}
-(int) perimeter
{
return (width + height) * 2;
}
-(XYPoint *) origin
{
return origin;
}
@end
int main (int argc, char * argv[]) {
@autoreleasepool {
Rectangle *myRect = [[Rectangle alloc] init];
[myRect setX: 100 andY: 200];
[myRect setWidth: 5.5 andHeight: 8.1];
NSLog (@"Origin at (%f, %f)", myRect.x, myRect.y);
NSLog (@"Origin at (%f, %f)", myRect.width, myRect.height);
}
return 0;
}
Output:Origin at (100.000000, 200.000000)
Origin at (5.500000, 8.100000)