I believe this is the way it was intended to be done.
Rectangle.m#import "Rectangle.h"
@implementation Rectangle
@synthesize width, height;
-(void) setWidth: (int) w andHeight: (int) h
{
width = w;
height = h;
}
-(int) area
{
return width * height;
}
-(int) perimeter
{
return (width + height) * 2;
}
@end
Square.m#import "Square.h"
#import "Rectangle.h"
@implementation Square
Rectangle *rect;
@synthesize side;
-(Square *) initWithSide: (int) s
{
self = [super init];
if (self)
rect = [[Rectangle alloc] init];
[rect setWidth: s andHeight: s];
[self setSide: s];
return self;
}
-(void) setSide: (int) s
{
side = s;
}
-(int) area
{
return [rect area];
}
-(int) perimeter
{
return [rect perimeter];
}
@end
main.m#import "Square.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Square *a = [[Square alloc] initWithSide: 10];
NSLog(@"Side is: %i", a.side);
NSLog(@"Area is: %i", a.area);
NSLog(@"Perimeter is: %i", a.perimeter);
[pool drain];
return 0;
}