Objective-C

Contributed by Chris Rathman

File: polymorph.m

#import "Rectangle.h"
#import "Circle.h"

main() {
   int i;
   id scribble[2];
   id aRectangle;

   /* toss the different shapes into a collection */
   scribble[0] = [Rectangle x:10 y:20 width:5 height:6];
   scribble[1] = [Circle x:15 y:25 radius:8];

   /* Use the shapes polymorphically */
   for (i = 0; i < 2; i++) {
      [scribble[i] draw];
      [scribble[i] rMoveToX:100 rMoveToY:100];
      [scribble[i] draw];
   }

   /* call a rectangle specific function */
   aRectangle = [Rectangle x:0 y:0 width:15 height:15];
   [aRectangle width:30];
   [aRectangle draw];
}

File: Shape.h

#import < objc/Object.h >

interface Shape:Object {
   int x;
   int y;
}

/* get the origin x value */
- (int)x;

/* get the origin y value */
- (int)y;

/* set the origin x value */
- (int)x:(int)aNumber;

/* set the origin y value */
- (int)y:(int)aNumber;

/* shift the origin based on the (x,y) point */
- moveToX:(int)newX moveToY:(int)newY;

/* shift the origin based on the delta (x,y) point */
- rMoveToX:(int)deltaX rMoveToY:(int)deltaY;

@end

File: Shape.m

#import "Shape.h"

@implementation Shape

/* get the origin x value */
- (int)x {
   return x;
}

/* get the origin y value */
- (int)y {
   return y;
}

/* set the origin x value */
- (int)x:(int)aNumber {
   x = aNumber;
   return x;
}

/* set the origin y value */
- (int)y:(int)aNumber {
   y = aNumber;
   return y;
}

/* shift the origin based on the (x,y) point */
- moveToX:(int)newX moveToY:(int)newY {
   [self x: newX];
   [self y: newY];
}
/* shift the origin based on the delta (x,y) point */
- rMoveToX:(int)deltaX rMoveToY:(int)deltaY {
   [self x:([self x] + deltaX)];
   [self y:([self y] + deltaY)];
}

@end

File: Rectangle.h

#import "Shape.h"

@interface Rectangle:Shape {
   int height;
   int width;
}

/* class method to construct a rectangle */
+ (id)x:(int)xValue y:(int)yValue width:(int)aWidth height:(int)aHeight;

/* get the height of the rectangle */
- (int)height;

/* get the width of the rectangle */
- (int)width;

/* set the height of the rectangle */
- (int)height:(int)aNumber;

/* set the width of the rectangle */
- (int)width:(int)aNumber;

/* draw the rectangle */
- draw;

@end

File: Rectangle.m

#import "Rectangle.h"

@implementation Rectangle

/* class method to construct a rectangle */
+ (id)x:(int)xValue y:(int)yValue width:(int)aWidth height:(int)aHeight {
   id newInstance = [[Rectangle alloc] init];

   [newInstance x:xValue];
   [newInstance y:yValue];
   [newInstance width:aWidth];
   [newInstance height:aHeight];
   return newInstance;
}

/* get the height of the rectangle */
- (int)height {
   return height;
}

/* get the width of the rectangle */
- (int)width {
   return width;
}

/* set the height of the rectangle */
- (int)height:(int)aNumber {
   height = aNumber;
   return height;
}

/* set the width of the rectangle */
- (int)width:(int)aNumber {
   width = aNumber;
   return width;
}

/* draw the rectangle */
- draw {
   printf("Drawing a Rectangle at:(%d,%d), width %d, height %d\n",
      [self x], [self y], [self width], [self height]);
}

@end

File: Circle.h

#import "Shape.h"

@interface Circle:Shape {
   int radius;
}

/* class method to construct a circle */
+ (id)x:(int)xValue y:(int)yValue radius:(int)aRadius;

/* get the radius value */
- (int)radius;

/* set the radius of the circle */
- (int)radius:(int)aNumber;

/* draw the circle */
- draw;

@end

File: Circle.m

#import "Circle.h"

@implementation Circle

/* class method to construct a circle */
+ (id)x:(int)xValue y:(int)yValue radius:(int)aRadius {
   id newInstance = [[Circle alloc] init];
   [newInstance x:xValue];
   [newInstance y:yValue];
   [newInstance radius:aRadius];
   return newInstance;
}

/* get the radius value */
- (int)radius {
   return radius;
}

/* set the radius of the circle */
- (int)radius:(int)aNumber {
   radius = aNumber;
   return radius;
}

/* draw the circle */
- draw {
   printf("Drawing a Circle at:(%d,%d), radius %d\n",
      [self x], [self y], [self radius]);
}
 @end

Output

Drawing a Rectangle at:(10,20), width 5, height 6
Drawing a Rectangle at:(110,120), width 5, height 6
Drawing a Circle at:(15,25), radius 8
Drawing a Circle at:(115,125), radius 8
Drawing a Rectangle at:(0,0), width 30, height 15

Chris Rathman / Chris.Rathman@tx.rr.com