Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 19, 2017, 09:40:14 AM *
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: Chapter 10 - Exercise 2 Solution  (Read 2742 times)
bertobettencourt
Newbie
*
Posts: 45


Email
« on: October 27, 2013, 09:01:07 AM »

Rectangle.h

Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Rectangle : NSObject

@property int width, height;

-(int) area;
-(int) perimeter;
-(void) setWidth:(int) w andHeight:(int) h;
-(id) initWithWidth:(int) w andHeight:(int) h; // declared my custom initializer method
-(id) init; // declared NSObject init, so that i can override it

@end

Rectangle.m

Code: (Objective-C)
#import "Rectangle.h"

@implementation Rectangle
@synthesize width, height;

-(int) area{
   
    return width * height;
}
-(int) perimeter{
   
    return (width + height) * 2;
}
-(void) setWidth:(int) w andHeight:(int) h{
    width = w;
    height = h;
}
-(id) initWithWidth:(int) w andHeight:(int) h{ // defined my custom initializer
    self = [super init];
    if (self){
        [self setWidth:w andHeight:h]; // calling setter method
    }
    return self;
}
-(id) init{ // overwriting default init, telling init when initialized please parse the method initWith
   
  return   [self initWithWidth:0 andHeight:0];
   
}
@end


Square.h

Code: (Objective-C)
#import "Rectangle.h"

@interface Square : Rectangle // here we can see it's superclass is Rectangle, therefore it's being inherited

-(void) setSide: (int)s;
-(int) side;
-(id) initWithSide: (int) side; // custom initializer
-(id) init; // declaring init again, this means i will overwrite the inherited method "init"

@end

Square.m

Code: (Objective-C)
#import "Square.h"

@implementation Square


-(void) setSide: (int)s{
    [self setWidth:s andHeight:s];
}
-(int) side{
   
    return self.width;
}

-(id) initWithSide: (int) side{ // defining my custom initializer
   
    self = [super init];
    if (self){
        [self setWidth:side andHeight:side]; // calling setter method
}
    return self;
}
-(id) init{ // overwriting inherited init - THE ABOVE INIT WOULD WORK WITHOUT THIS INIT METHOD, ITS JUST FOR COMPLETENESS SAKE
    return [self initWithSide:0];
}
@end


main.m

Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "Square.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        Rectangle *myRect = [[Rectangle alloc] initWithWidth:10 andHeight:5];
       
        NSLog(@"Rectangle a: %i and p: %i", [myRect area], [myRect perimeter]);
       


        Square *mySqua = [[Square alloc] initWithSide:5];
       
        NSLog(@"Square a: %i and p:%i", [mySqua area], [mySqua perimeter]);

       
    }
    return 0;
}


Cheers.
Logged
cwiley
Newbie
*
Posts: 1


Email
« Reply #1 on: June 22, 2016, 07:33:41 PM »

I don't know if this is right and would like to throw it out for discussion but another way to implement the designated initializer in Square.m is:

Code: (Objective-C)
-(id) initWithSide: (int) side{ // defining my custom initializer  
      
    return [super initWithWidth:side andHeight:side];  //Calling the super class's designated initializer
  
   //Don't need to check self    
 
}  


For reference here is the original code:

Code: (Objective-C)
-(id) initWithSide: (int) side{ // defining my custom initializer  
      
    self = [super init];  
    if (self){  
        [self setWidth:side andHeight:side]; // calling setter method  
}  
    return self;  
}  

I don't know if checking the initialization of the super class was successful is inherently good but I tried this out and it works.

Let me know what you all think
« Last Edit: June 22, 2016, 08:38:24 PM by cwiley » 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.