#import #import #import @interface Rectangle: Object { int width; int height; } -(Rectangle*) initWithWidth: (int) w height: (int) h; -(void) setWidth: (int) w; -(void) setHeight: (int) h; -(void) setWidth: (int) w height: (int) h; -(int) width; -(int) height; -(void) print; @end //////////////////////////////////////////////////////// @implementation Rectangle -(Rectangle*) initWithWidth: (int) w height: (int) h { self = [super init]; if ( self ) { [self setWidth: w height: h]; } return self; } -(void) setWidth: (int) w { width = w; } -(void) setHeight: (int) h { height = h; } -(void) setWidth: (int) w height: (int) h { width = w; height = h; } -(int) width { return width; } -(int) height { return height; } -(void) print { printf( "width = %i, height = %i", width, height ); } @end @interface Square: Rectangle -(Square*) initWithSize: (int) s; -(void) setSize: (int) s; -(int) size; @end //////////////////////////////////////////////////////// @implementation Square -(Square*) initWithSize: (int) s { self = [super init]; if ( self ) { [self setSize: s]; } return self; } -(void) setSize: (int) s { width = s; height = s; } -(int) size { return width; } -(void) setWidth: (int) w { [self setSize: w]; } -(void) setHeight: (int) h { [self setSize: h]; } @end //////////////////////////////////////////////////////// int main(int argc, const char *argv[]) { Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20]; Square *sq = [[Square alloc] initWithSize: 15]; // print em printf( "Rectangle: " ); [rec print]; printf( "\n" ); printf( "Square: " ); [sq print]; printf( "\n" ); // update square [sq setWidth: 20]; printf( "Square after change: " ); [sq print]; printf( "\n" ); // free memory [rec release]; [sq release]; return 0; }