Skip to content

Instantly share code, notes, and snippets.

@zeayes
Created March 27, 2015 09:26
Show Gist options
  • Save zeayes/9b3e5bd679545110e27b to your computer and use it in GitHub Desktop.
Save zeayes/9b3e5bd679545110e27b to your computer and use it in GitHub Desktop.
dynamic binding
#import <Foundation/Foundation.h>
@interface Square : NSObject
{
float area;
}
- (void)calculateAreaOfSide: (CGFloat)side;
- (void)printArea;
@end
@implementation Square
- (void)calculateAreaOfSide: (CGFloat)side
{
area = side * side;
}
- (void)printArea
{
NSLog(@"The area of square is %f", area);
}
@end
@interface Rectangle : Square
- (void)calculateAreaOfLength: (CGFloat)length andBreadth: (CGFloat)breadth;
- (void)printArea;
@end
@implementation Rectangle
- (void)calculateAreaOfLength: (CGFloat)length andBreadth: (CGFloat)breadth
{
area = length * breadth;
}
- (void)printArea
{
NSLog(@"The area of Rectangle is %f", area);
}
@end
int main(int argc, char *argv[]) {
Square *square = [[Square alloc]init];
[square calculateAreaOfSide:10.0];
Rectangle *rectangle = [[Rectangle alloc]init];
[rectangle calculateAreaOfLength: 10.0 andBreadth:5.0];
NSArray *shapes = [[NSArray alloc]initWithObjects: square, rectangle, nil];
id object1 = [shapes objectAtIndex:0];
[object1 printArea];
id object2 = [shapes objectAtIndex:1];
[object2 printArea];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment