Objective C (for reference: Optional)

main.m  [download]

//
//  main.m
//  shape6
// Like shape6 java example, shows simple Objective C class syntax
//

#import <Foundation/Foundation.h>
#import "MyRect.h"
#import "Circle.h"


// Just a main program for testing our objects
int main(int argc, const char * argv[]) {
    /*
	 * Data definitions
	 */
	MyRect *r1, *r2;
	Circle *c1;
    
	/*
	 * Instantiate and initialize the objects
	 * (could have done in defs above)
	 */
	r1 = [[MyRect alloc] init: 10 y:10 wid:50 ht:50];
	r2 = [[MyRect alloc] init: 80 y:10 wid:10 ht:100];
	c1 = [[Circle alloc] init: 35 y:90 rad:10];
    
	/*
	 * "Draw" them
	 */
	[r1 draw];
	[r2 draw];
	[c1 draw];
    
	/*
	 * Compute and print total area
	 */
	printf ("Total area = %f\n", ([r1 area] + [r2 area] + [c1 area]));

}

MyRect.h  [download]

//
//  MyRect.h
//  shape6
// Definition of class MyRect
// (NB Cocoa uses the name "Rect")
//

#import <Foundation/Foundation.h>

@interface MyRect : NSObject
// Ivars
{
@private
int x;
int y;
int wid;
int ht;
}

// Methods
-(id) init: (int)xin y: (int)yin wid: (int)widin ht: (int)htin;
-(void) setPos: (int)xin y: (int)yin;
-(void) setSize: (int)widin ht: (int)htin;
-(void) draw;
-(float) area;

@end

MyRect.m  [download]

//
//  MyRect.m
//  shape6
//
// Definition of class MyRect
//

#import "MyRect.h"

@implementation MyRect

-(id) init: (int)xin y: (int)yin wid: (int)widin ht: (int)htin {
	if (self = [super init]) {
		x = xin;
		y = yin;
		wid = widin;
		ht = htin;
	}
	return self;
}

-(void) setPos: (int)xin y: (int)yin {
	x = xin;
	y = yin;
}

-(void) setSize: (int)widin ht: (int)htin {
	wid = widin;
	ht = htin;
}

-(void) draw {
	printf ("Drawing rectangle at %d, %d, %d, %d\n", x, y, wid, ht);
}

-(float) area {
	return wid * ht;
}

@end

Circle.h  [download]

//
//  Circle.h
//  shape6
// Definition of class Circle
//

#import <Foundation/Foundation.h>

@interface Circle : NSObject
// Ivars
{
@private
int x;
int y;
int rad;
}

// Methods
-(id) init: (int)xin y: (int)yin rad: (int)radin;
-(void) setPos: (int)xin y: (int)yin;
-(void) setSize: (int)radin;
-(void) draw;
-(float) area;

@end

Circle.m  [download]

//
//  Circle.m
//  shape6
// Definition of class Circle
//

#import "Circle.h"

@implementation Circle

-(id) init: (int)xin y: (int)yin rad: (int)radin {
	if (self = [super init]) {
		x = xin;
		y = yin;
		rad = radin;
	}
	return self;
}

-(void) setPos: (int)xin y: (int)yin {
	x = xin;
	y = yin;
}

-(void) setSize: (int)radin {
    rad = radin;
}


-(void) draw {
	printf ("Drawing circle at %d, %d, radius = %d\n", x, y, rad);
}

-(float) area {
	return (float) (rad * rad * 3.14159);
}

@end