judaco / Objective-C

0 stars 0 forks source link

Objective-C - Lesson 03 #2

Open judaco opened 7 years ago

judaco commented 7 years ago
//
//  main.m
//  Inheritance
//
//  Created by hackeru on 19/03/2017.
//  Copyright © 2017 Juda. All rights reserved.
//

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

@interface ClassA : NSObject {
    int x;
}

-(void)initX;

@end

@implementation ClassA

-(void)initX{
    x = 100;
}
@end

@interface ClassB : ClassA
-(void)printX;
@end

@implementation ClassB
-(void)printX{
    NSLog(@"x = %i", x);
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        ClassB *b = [[ClassB alloc] init];
//        [b initX];
//        [b printX];
//        
//        ClassA *a = [[ClassB alloc] init];
//        [a initX];

        Square *s1 = [[Square alloc] init];
        [s1 setSide: 7];
        NSLog(@"area is %i", [s1 area]);

    }
    return 0;
}
judaco commented 7 years ago
//
//  Rectangle.h
//  Inheritance
//
//  Created by hackeru on 19/03/2017.
//  Copyright © 2017 Juda. All rights reserved.
//

#import <Foundation/Foundation.h>
@class XYPoint; //don't import all the code, just know which the class exists - much more efficiency

@interface  Rectangle : NSObject

@property int width, height;
-(int) area;
-(int) perimeter;
-(void) setWidth:(int)w andHeight: (int) h;
-(void) setOrigin: (XYPoint *) o;//setter for XY
-(XYPoint *) origin;//getter for XY

@end
judaco commented 7 years ago
//
//  Rectangle.m
//  Inheritance
//
//  Created by hackeru on 19/03/2017.
//  Copyright © 2017 Juda. All rights reserved.
//

#import "Rectangle.h"

@implementation Rectangle
{
    XYPoint *origin;
}

@synthesize width, height;

-(void) setWidth:(int)w andHeight:(int)h {
    width = w;
    height = h;
}
-(int) area {
    return width * height;
}

-(int) perimeter {
    return 2*(width + height);
}

-(void) setOrigin:(XYPoint *)o {
    origin = o;
}

-(XYPoint *) origin {
    return origin;
}
@end
judaco commented 7 years ago
//
//  Square.h
//  Inheritance
//
//  Created by hackeru on 19/03/2017.
//  Copyright © 2017 Juda. All rights reserved.
//

#import "Rectangle.h"

@interface Square : Rectangle

-(void) setSide: (int) s;
-(int) side;

@end
judaco commented 7 years ago
//
//  Square.m
//  Inheritance
//
//  Created by hackeru on 19/03/2017.
//  Copyright © 2017 Juda. All rights reserved.
//

#import "Square.h"

@implementation Square

-(void)setSide:(int)s {
    [super setWidth:s andHeight: s];
}

-(int)side {
    return self.width;
}

@end
judaco commented 7 years ago
//
//  XYPoint.h
//  Inheritance
//
//  Created by hackeru on 19/03/2017.
//  Copyright © 2017 Juda. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface XYPoint : NSObject

@property int x, y;
-(void) setX:(int) xVal andY: (int) yVal;

@end
judaco commented 7 years ago
//
//  XYPoint.m
//  Inheritance
//
//  Created by hackeru on 19/03/2017.
//  Copyright © 2017 Juda. All rights reserved.
//

#import "XYPoint.h"

@implementation XYPoint

@synthesize x, y;

-(void)setX:(int)xVal andY:(int)yVal {
    x = xVal;
    y = yVal;
}

@end