Created
June 18, 2015 17:18
-
-
Save imack/a51af84dd80274f60e51 to your computer and use it in GitHub Desktop.
W7D4 - Object Oriented Design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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