Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <Foundation/Foundation.h>
- @interface Time : NSObject
- {
- int ticks;
- }
- -(id) init:(int) h:(int) m: (int) s;
- -(int) hour;
- -(int) min;
- -(int) sec;
- -(void) setHour:(int) h;
- -(void) setMin:(int) m;
- -(void) setSec:(int) s;
- @end
- @protocol IGround
- -(double) area;
- -(double) perimeter;
- @end
- @interface Triangle : NSObject <IGround>
- {
- double a, b, c;
- }
- -(void) initTriangle:(double) _a: (double) _b: (double) _c;
- -(double) area;
- -(double) perimeter;
- @end
- @interface Prism : NSObject
- {
- double height;
- id<IGround> ground; // Is an implementator of IGround
- }
- -(double) area; // - for instance method and + for statis
- -(double) volume;
- -(void) initPrism:(double) h: (id<IGround>) g;
- @end
- @interface Circle : NSObject
- {
- double radius;
- }
- -(double) area;
- -(double) perimeter;
- -(void) initR:(double)r;
- @end
- @interface Cylinder : Circle
- {
- @public
- double height;
- }
- -(double) area;
- -(void) initRH:(double) r: (double) h;
- -(double) volume;
- @end
- int main (int argc, const char * argv[])
- {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- Time* tm = [[Time alloc] init:12:15:57];
- tm.hour = 4;
- tm.min = 10;
- tm.sec = 36;
- printf("%i.%i.%i\n", tm.hour, tm.min, tm.sec);
- Circle* c;
- c = [[Circle alloc] init];
- [c initR:9];
- double a;
- a = [c area]; // Calling method
- double _p;
- _p = c.perimeter; // Other way of calling (nonparametrised) method
- printf("%f, %f\n", a, _p);
- Cylinder* cyl;
- cyl = [[Cylinder alloc] init];
- [cyl initRH: 2 : 3];
- printf("%f, %f\n", [cyl area], [cyl volume]);
- // Polymorphism
- c = cyl;
- printf("%f\n", [c area]);
- Prism* p = [[Prism alloc] init];
- Triangle* t = [[Triangle alloc] init];
- [t initTriangle: 3: 4: 5];
- [p initPrism: 7: t];
- printf("%f, %f", [p area], [p volume]);
- NSLog (@"hello world");
- [pool drain];
- return 0;
- }
- @implementation Circle
- -(double)area
- {
- return 3.14159 * self->radius * radius;
- }
- -(double)perimeter
- {
- return 3.14159 * 2 * self->radius;
- }
- -(void)initR: (double) r
- {
- self->radius = r;
- }
- @end
- @implementation Cylinder
- -(double) area
- {
- return 2 * [super area] + self.perimeter * self->height;
- }
- -(double) volume
- {
- return [super area] * self->height;
- }
- -(void) initRH: (double) r : (double) h
- {
- self->radius = r;
- self->height = h;
- }
- @end
- @implementation Prism
- -(double) volume {
- return [self->ground area] * self->height;
- }
- -(double) area {
- return 2 * [self->ground area] + self->height * [self->ground perimeter];
- }
- -(void) initPrism: (double) h: (id<IGround>) g
- {
- self->height = h;
- self->ground = g;
- }
- @end
- @implementation Triangle
- -(void) initTriangle:(double) _a: (double) _b: (double) _c
- {
- self->a = _a;
- self->b = _b;
- self->c = _c;
- }
- -(double) area
- {
- return self->a * self->b / 2;
- }
- -(double) perimeter
- {
- return self->a * self->b * self->c;
- }
- @end
- @implementation Time
- -(id) init:(int) h:(int) m: (int) s
- {
- self = [super init];
- if (self == 0) {
- return 0;
- }
- self->ticks = (h*60 + m)*60 + s;
- return self;
- }
- -(int) hour {
- return self->ticks / 3600;
- }
- -(int) min {
- return self->ticks / 60 % 60;
- }
- -(int) sec {
- return self->ticks % 60;
- }
- -(void) setHour:(int) h {
- self->ticks = self->ticks % 3600 + h * 3600;
- }
- -(void) setMin:(int) m {
- self->ticks = self->ticks + (m - self->ticks / 60 % 60)* 60;
- }
- -(void) setSec:(int) s {
- self->ticks = self->ticks - self->ticks % 60 + s;
- }
- @end
Add Comment
Please, Sign In to add comment