Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
August 18, 2018, 09:24:55 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Exercise 5  (Read 7617 times)
kotyara
Newbie
*
Posts: 15


« on: December 14, 2014, 02:27:25 PM »

//  Rectangle.h

#import <Foundation/Foundation.h>

@interface Rectangle : NSObject
@property int width, height;
-(void) setWidth: (int) w andHeight:(int) h;
-(int) area;
-(int) perimeter;
@end

//  Rectangle.m

#import "Rectangle.h"

@implementation Rectangle

@synthesize width, height;
-(void)setWidth:(int)w andHeight:(int)h{
    width = w;
    height = h;
}
-(int)area{
    return width * height;
}
-(int)perimeter{
    return (width + height) * 2;
}
@end

//  Square.h

#import <Foundation/Foundation.h>

@interface Square : NSObject
@property int side;
-(instancetype) initWithSide: (int) s;
-(int) area;
-(int) perimeter;
@end

//  Square.m

#import "Square.h"
#import "Rectangle.h"

@implementation Square
{
    Rectangle *rect;
}
@synthesize side;
- (instancetype)initWithSide: (int) s
{
    self = [super init];
    if (self) {
        self.side = s;
        rect = [[Rectangle alloc]init];
        [rect setWidth:s andHeight:s];
    }
    return self;
}
- (instancetype)init
{
    return [self initWithSide:0];
}
-(int)area{
    return [rect area];
}
-(int)perimeter{
    return [rect perimeter];
}
@end

//  main.m

#import <Foundation/Foundation.h>
#import "Square.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Square * mySquare = [[Square alloc]initWithSide:10];
        NSLog(@"Side: %i; area: %i; perimeter: %i", [mySquare side], [mySquare area], [mySquare perimeter]);
    }
    return 0;
}

output:
2014-12-15 00:23:03.991 q05[8733:392132] Side: 10; area: 100; perimeter: 40
Program ended with exit code: 0
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.