2013년 2월 1일 금요일

Objective-C Chapter 4.4


// 4.5 연습문제 7번

#import <Foundation/Foundation.h>

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

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

int main(int argc, char *argv[])
{
    @autoreleasepool {
        Rectangle *rectangleEx7 = [[Rectangle alloc] init];
        [rectangleEx7 setWidth: 10];
        [rectangleEx7 setHeight: 24];
        NSLog(@"Width %i, height %i, Area of rectangle is %i and Perimeter is %i",
              [rectangleEx7 width], [rectangleEx7 height], [rectangleEx7 area], [rectangleEx7 perimeter]);
    }
    return 0;
}

댓글 없음:

댓글 쓰기