Skip to content

Instantly share code, notes, and snippets.

@imack
Created June 18, 2015 17:18
Show Gist options
  • Save imack/a51af84dd80274f60e51 to your computer and use it in GitHub Desktop.
Save imack/a51af84dd80274f60e51 to your computer and use it in GitHub Desktop.
W7D4 - Object Oriented Design
//
// Circle.h
// Shapes
//
// Created by Ian MacKinnon on 2015-06-18.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Shape.h"
@interface Circle : Shape
@property(nonatomic, assign)float radius;
@end
//
// Circle.m
// Shapes
//
// Created by Ian MacKinnon on 2015-06-18.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import "Circle.h"
@implementation Circle
-(float) area{
return pow(self.radius, 2) * M_PI ;
}
@end
//
// main.m
// Shapes
//
// Created by Ian MacKinnon on 2015-06-18.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "Circle.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
Rectangle *rect1 = [[Rectangle alloc] init];
rect1.length = 2;
rect1.width = 1;
Rectangle *rect2 = [[Rectangle alloc] init];
rect2.length = 4;
rect2.width = 5;
Circle *circ1 = [[Circle alloc] init];
circ1.radius = 7;
NSArray *shapeArray = @[rect1, rect2, circ1];
float sum = 0.0;
for (Shape *shape in shapeArray){
sum = + [shape area];
}
NSLog(@"sum of area of shapes is: %f", sum);
}
return 0;
}
//
// Rectangle.h
// Shapes
//
// Created by Ian MacKinnon on 2015-06-18.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Shape.h"
@interface Rectangle : Shape
@property(nonatomic, assign) float length;
@property(nonatomic, assign) float width;
@end
//
// Rectangle.m
// Shapes
//
// Created by Ian MacKinnon on 2015-06-18.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import "Rectangle.h"
@implementation Rectangle
-(float) area{
return self.length * self.width;
}
@end
//
// Shape.h
// Shapes
//
// Created by Ian MacKinnon on 2015-06-18.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Shape : NSObject
-(float) area;
@end
//
// Shape.m
// Shapes
//
// Created by Ian MacKinnon on 2015-06-18.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import "Shape.h"
@implementation Shape
-(float)area{
[NSException raise:@"Invalid Method invocation" format:@"You called an abstract method, but Obj-C can't enforce that at compile time, so tossing this exception is our next best way of enforcing"];
return 0.0;
}
@end
//
// Square.h
// Shapes
//
// Created by Ian MacKinnon on 2015-06-18.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import "Rectangle.h"
@interface Square : Rectangle
@end
//
// Square.m
// Shapes
//
// Created by Ian MacKinnon on 2015-06-18.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import "Square.h"
@implementation Square
-(float) area{
return self.length *self.length;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment