Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macsrockmysocks

macrumors regular
Original poster
Feb 21, 2006
233
0
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:

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

macrumors 68040
Nov 9, 2004
3,042
31
Salt Lake City, UT
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

macrumors regular
Original poster
Feb 21, 2006
233
0
mduser63 said:
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

macrumors 68040
Nov 9, 2004
3,042
31
Salt Lake City, UT
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.