Amazon.com Widgets Chapter 8, Program 2
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 24, 2014, 03:30:16 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Program Examples
| | |-+ Chapter 8
| | | |-+ Chapter 8, Program 2
Pages: [1] Go Down
Print
Author Topic: Chapter 8, Program 2 (Read 2524 times)
nate
Newbie
*
Posts: 9






on: February 19, 2009, 12:25:44 PM

Program 8.2 Rectangle.h Interface File

Code: (Objective-C)
#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

Program 8.2 Rectangle.m Implementation File

Code: (Objective-C)
#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

Program 8.2

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

int main( int argc, char* argv[] )
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    Rectangle* r = [[Rectangle alloc] init];

    [r setWidth: 5 andHeight: 8];

    NSLog( @"w = %i, h = %i", r.width, r.height );
    NSLog( @"Area = %i, Perimeter = %i", [r area], [r perimeter] );

    [r release];

    [pool drain];
    return 0;
}

Program 8.2 Output

Code: (Objective-C)
w = 5, h = 8
Area = 40, Perimeter = 26

Logged
scutter7282
Newbie
*
Posts: 18






Reply #1 on: June 12, 2009, 10:52:23 AM

Just so there's no confusion, the downloadable file is correct, but the posting of the code has the interface file showing the code for the implementation.  The file should be:

Program 8.2 Rectangle.h Interface File
Code: (Objective-C)
#import <Foundation/Foundation.h>


@interface Rectangle: NSObject
{
int width;
int height;
}

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

@end
Logged
mclifford82
Newbie
*
Posts: 2


Email




Reply #2 on: June 28, 2009, 01:57:24 PM

Perhaps this is a silly question, but what FILE does the main routine go into?  I have Rectangle.h and Rectangle.m as shown, but what file in the project does the following code go into (either .m, .h or another file?):

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

int main( int argc, char* argv[] )
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    Rectangle* r = [[Rectangle alloc] init];

    [r setWidth: 5 andHeight: 8];

    NSLog( @"w = %i, h = %i", r.width, r.height );
    NSLog( @"Area = %i, Perimeter = %i", [r area], [r perimeter] );

    [r release];

    [pool drain];
    return 0;
}
Logged
esc
Global Moderator
Full Member
*****
Posts: 230






Reply #3 on: June 28, 2009, 02:36:30 PM

Perhaps this is a silly question, but what FILE does the main routine go into?  I have Rectangle.h and Rectangle.m as shown, but what file in the project does the following code go into (either .m, .h or another file?):

Any .m file.  Since you're organizing and separating your Rectangle class definition and implementation into .h and .m, you may want to have your main routine in a separate .m file, e.g. test.m.

Logged
mclifford82
Newbie
*
Posts: 2


Email




Reply #4 on: June 28, 2009, 02:45:38 PM

Thanks esc!  I figured it was something simple like that, but wanted to make sure before continuing.
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.