Monday, March 28, 2011

Creating an Object

In this lesson, we'll be creating a new object of the Person class, which we wrote in the last two tutorials.
Make sure the interface and implementation sections of person are in HelloWorld.m, or wherever your main routine is.

Type the following into your main routine:


1 int main (int argc, const char * argv[]) {
2 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
3
4 Person * junior;
5 junior = [Person alloc];
6 junior = [junior init];
7
8 [junior setAge:23];
9 [junior setWeight:200];

10 [junior eat];
11 [junior release];
12 [pool drain];
13 return 0;
14 }


Line 4 creates a new Person object named junior.
Line 5 grabs memory for that object.
Line 6 initializes the object.
Line 8 calls the setAge method on junior, with a parameter of 23. Junior's age is now 23.
Line 9 calls the setWeight method on junior, with a parameter of 200. Junior's weight is now 200.
Line 10 calls junior's eat method, raising junior's weight to 201.
Line 11 releases the memory allocated for junior.

9 comments:

  1. Awesome tutorial and tips! thanks for sharing!

    ReplyDelete
  2. This looks good, everything is in order. Where do you see yourself heading in life?

    ReplyDelete
  3. I would love some more basic stuff like, the most used codes ect :P

    ReplyDelete
  4. but what does junior look like?
    besides being 23 and 200 pounds

    ReplyDelete