Friday, March 25, 2011

Implementation

Time to write some code for the person class!
Here is the implementation for our person class. Examine the following code:


1  @implementation Person
2  -(void) setAge: (int) a{
3     age = a;
4  }
5  -(void) setWeight: (int) w{
6     weight = w;
7  }
8  -(void) eat{
9     weight++;
10 }
11 @end


Line 1 lets the compiler know that this is where the code for the Person implementation begins.

Line 2 is a repeat of the declaration we saw in the interface section.

Line 3 is the operation that takes place in setAge. The age of the person is set to be the age you pass into the function.

Lines 5-7 are exactly the same as the setAge code, except instead of age, the variable is weight.

Line 9 is the operation for our eat function. weight++ means that the value of weight will be increased by 1. When you eat, you gain weight!

Line 11 signifies the end of the Person implementation.

In the next tutorial, we'll get to use our Person class definition to create a new person object!

12 comments:

  1. Where can I find a good manual on this?

    ReplyDelete
  2. As a student who is now taking a computer science class for the first time, this was very interesting. Following

    ReplyDelete
  3. uhh awesome, idk if I missed any lessions but could you explain like rly basic stuff. I have tryed to get in to coding but i get bored easy ;D

    do something fun with the codes! :D

    ReplyDelete
  4. I think is is beyond my skills :(

    ReplyDelete