#import @interface Limb : NSObject @end // Limb @implementation Limb - (NSString *) description { return (@"A limb."); } // description @end // Limb @interface Arm : Limb @end // Arm @implementation Arm - (NSString *) description { return (@"An arm."); } // description @end // Arm @interface Brain : NSObject @end // Brain @implementation Brain - (NSString *) description { return (@"A brain.!"); } // description @end // Brain @interface MadScientist : Brain @end // MadScientist @implementation MadScientist - (NSString *) description { return (@"A Mad Scientist's Brain"); } // description @end @interface TheMonster : NSObject { Limb *limbs[4]; Brain *brain; } - (void) setBrain: (Brain *) newBrain; - (Brain *) brain; - (void) setLimb: (Limb *) limb atIndex: (int) index; - (Limb *) limbAtIndex: (int) index; - (void) print; @end // TheMonster @implementation TheMonster - (id) init { if (self = [super init]) { brain = [Brain new]; limbs[0] = [Limb new]; limbs[1] = [Limb new]; limbs[2] = [Limb new]; limbs[3] = [Limb new]; } return (self); } // init - (Brain *) brain { return (brain); } // brain - (void) setBrain: (Brain *) newBrain { brain = newBrain; } // setBrain - (void) setLimb: (Limb *) limb atIndex: (int) index { if (index < 0) { NSLog (@"Can't attach a negative limb!", index); exit (1); } if (index > 3) { NSLog (@"Can't attach more than four limbs!", index); exit (1); } limbs[index] = limb; } // setLimb:atIndex: - (Limb *) limbAtIndex: (int) index { if (index < 0 ) { NSLog (@"Can't request a negative limb!", index); exit (1); } if (index > 3) { NSLog (@"bad index (%d) in limbAtIndex:", index); exit (1); } return (limbs[index]); } // limbAtIndex: - (void) print { NSLog (@"Our Newly Created Monster is composed of:"); NSLog (@"%@", brain); NSLog (@"%@", limbs[0]); NSLog (@"%@", limbs[1]); NSLog (@"%@", limbs[2]); NSLog (@"%@", limbs[3]); } // print @end // TheMonster int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; TheMonster *theMonster = [TheMonster new]; int i; for (i = 0; i < 2; i++) { Limb *limb = [Arm new]; [theMonster setLimb: limb atIndex: i]; } for (i = 2; i < 4; i++) { Limb *limb = [Limb new]; [theMonster setLimb: limb atIndex: i]; } Brain *brain = [MadScientist new]; [theMonster setBrain: brain]; [theMonster print]; return (0); [pool drain]; return 0; }