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: