PDA

View Full Version : Help with Objc code.. Something I haven't seen.




macsrockmysocks
Jul 28, 2006, 09:17 PM
As some know, I just started with Objective-C with Steve Kochan's book. Well, one of the exercises told me to write a program to display coordinates. So I wrote one, then checked it online. I did a few things wrong but very minimal. But, I just realized when I checked again, something was weird. Here is the code:

//Program to weirdly work with coordinates

#include <objc/Object.h>

//@ Interface Section

@interface Point: Object

{
int x;
int y;
}

-(void) setX: (int) xVal;
-(void) setY: (int) yVal;
-(int) x;
-(int) y;

@end

//@ Implementation Section

@implementation Point

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

-(void) setY: (int) yVal
{
y = yVal;
}

-(int) x
{
return x;
}

-(int) y
{
return y;
}

@end

//Program Section

int main (int argc, char *argv[])
{
Point *pt1, *pt2;
pt1 = [[Point alloc] init];
pt2 = [[Point alloc] init];

[pt1 setX: 300];
[pt1 setY: 100];
[pt2 setX: 50];
[pt2 setY: 100];

printf ("Pt1 = %i, %i)\n", [pt1 x], [pt1 y]);
printf ("Pt2 = %i, %i)\n", [pt2 x], [pt2 y]);

[pt1 free];
[pt2 free];

return 0;

}
At the top where it says #include <objc/Object.h>, I never saw that before. Can someone explain to me what it means, where it is used, and why I didnt use #import <stdio.h> and #import <objc/Object.h>. Sorry if you see more and more posts by me on this forum as I start to need some more help. Don't think of it as spamming. Thanks:)



mduser63
Jul 28, 2006, 10:05 PM
I'm not sure whether you're asking about that specific line or about #include statements in general. I'm going to assume the former, as the book you're going through covers #import/#include statements in some detail.

Object.h is the header file that declares the Object class. Since Object is referenced as the parent class of Point, the compiler needs to know about it. That's why it's included. Does that make sense?

macsrockmysocks
Jul 28, 2006, 10:10 PM
I'm not sure whether you're asking about that specific line or about #include statements in general. I'm going to assume the former, as the book you're going through covers #import/#include statements in some detail.

Object.h is the header file that declares the Object class. Since Object is referenced as the parent class of Point, the compiler needs to know about it. That's why it's included. Does that make sense?

I just want to know why in that particular program why I had to used include. I had never seen it before I did this exercise. I just want to know what they are about and what they are used for before I move on to the next chapter.

mduser63
Jul 29, 2006, 01:06 AM
Have you seen #import statements? If so, then all you need to know is that #include is the same as #import except that #import ensures that the same header file doesn't get included multiple times.

If you don't know what #import or #include mean, just take their word for it, and be patient. The book you're working through does a good job of explaining them, just not right at the beginning.

slooksterPSV
Jul 29, 2006, 06:14 AM
Well you are going to have to #import <stdio.h> because printf is a function that is specified in stdio.h (standard input/output).