IOS square类集成rectangl类 计算面积和周长

来源:互联网 发布:想在淘宝直播 编辑:程序博客网 时间:2024/04/30 07:53

定义rectangl类

////  rectangle.h//  0827-矩形面积和周长////  Created by panba on 15-8-28.//  Copyright (c) 2015年 panba. All rights reserved.//#import <Foundation/Foundation.h>@interface rectangle : NSObject@property int width,height;-(void) setWidth:(int)width andheight:(int)height;-(int) returnarea;-(int) returnperimeter;@end

声明rectangl类

////  rectangle.m//  0827-矩形面积和周长////  Created by panba on 15-8-28.//  Copyright (c) 2015年 panba. All rights reserved.//#import "rectangle.h"@implementation rectangle@synthesize width,height;-(void) setWidth:(int)x andheight:(int)y{    width = x;    height = y;}-(int) returnarea{    return width*height;}-(int) returnperimeter{    return (width+height)*2;}@end
定义square类

////  square.h//  0827-矩形面积和周长////  Created by panba on 15-8-28.//  Copyright (c) 2015年 panba. All rights reserved.//#import <Foundation/Foundation.h>#import "rectangle.h"@interface square : rectangle-(void) setside:(int) n;-(int) getside;@end

声明square类

////  square.m//  0827-矩形面积和周长////  Created by panba on 15-8-28.//  Copyright (c) 2015年 panba. All rights reserved.//#import "square.h"@implementation square-(void) setside:(int) value{    [self setWidth:value andheight:value];}-(int) getside{    return self.width;}@end
主函数

////  main.m//  0827-矩形面积和周长////  Created by panba on 15-8-28.//  Copyright (c) 2015年 panba. All rights reserved.//#import <Foundation/Foundation.h>#include "rectangle.h"#include "square.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        rectangle *myrect = [[rectangle alloc] init];        [myrect setWidth:5 andheight:10];        NSLog(@"width = %i,height = %i",myrect.width,myrect.height);        NSLog(@"area is %i",[myrect returnarea]);        NSLog(@"preimeter is %i",[myrect returnperimeter]);                square *mysqua = [[square alloc] init];        [mysqua setside:9];        NSLog(@"squares side is %i",[mysqua getside ]);        NSLog(@"square area is %i,preimeter is %i",[mysqua returnarea],[mysqua returnperimeter]);    }    return 0;}




0 0
原创粉丝点击