Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [37]

By Root 457 0
const char * argv[])

{

@autoreleasepool {

// Create an instance of Person

id person = [[Person alloc] init];

// Give the instance variables interesting values

[person setWeightInKilos:96];

[person setHeightInMeters:1.8];

// Call the bodyMassIndex method

float bmi = [person bodyMassIndex];

NSLog(@"person (%d, %f) has a BMI of %f",

[person weightInKilos], [person heightInMeters], bmi);

[person count];

}

return 0;

}

Build and run the program. (Ignore the warning from the compiler because you’re doing this on purpose. In most cases, you’ll want to pay attention to this warning if you see it!) You should see a runtime exception logged to the console:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',

reason: '-[Person count]: unrecognized selector sent to instance 0x100108de0'

After examining the exception, delete the problematic line before continuing.

You’ve created this new Employee class, but you haven’t used it yet. Change main.m to use Employee:

#import

#import "Employee.h"

int main(int argc, const char * argv[])

{

@autoreleasepool {

// Create an instance of Person

Person * person = [[Employee alloc] init];

// Give the instance variables interesting values

[person setWeightInKilos:96];

[person setHeightInMeters:1.8];

// Call the bodyMassIndex method

float bmi = [person bodyMassIndex];

NSLog(@"person (%d, %f) has a BMI of %f",

[person weightInKilos], [person heightInMeters], bmi);

}

return 0;

}

Notice that your person variable is still declared as a pointer to a Person. Think this will cause a problem? Build and run the program, and you’ll see that your program still works fine. This is because an employee is a kind of person – it can do anything a person can. That is, we can use an instance of Employee anywhere that the program expects an instance of Person.

Now, however, you’re going to use a method that is unique to Employee, so you must change the type of the pointer variable:

#import

#import "Employee.h"

int main(int argc, const char * argv[])

{

@autoreleasepool {

// Create an instance of Person

Employee *person = [[Employee alloc] init];

// Give the instance variables interesting values

[person setWeightInKilos:96];

[person setHeightInMeters:1.8];

[person setEmployeeID:15];

// Call the bodyMassIndex method

float bmi = [person bodyMassIndex];

NSLog(@"Employee %d has a BMI of %f", [person employeeID], bmi);

}

return 0;

}

Overriding methods


To review, when a message is sent, the search for the method of that name starts at the object’s class and goes up the inheritance hierarchy. The first implementation that is found is the one that gets executed. Thus, you can override inherited methods with a custom implementation. Let’s say, for example, that you decided that employees always have a Body Mass Index of 19. In this case, you might override the bodyMassIndex method in Employee. Open Employee.m and do so:

#import "Employee.h"

@implementation Employee

@synthesize employeeID;

- (float)bodyMassIndex

{

return 19.0;

}

@end

Build and run the program and note that your new implementation of bodyMassIndex is the one that gets executed – not the implementation from Person.

Notice that you implemented bodyMassIndex in Employee.m, but you didn’t declare it in Employee.h. Declaring a method in the header file advertises the method so that instances of other classes can call it. However, because Employee inherits from Person, everyone already knows that instances of Employee have a bodyMassIndex method. There is no need to advertise it again.

super


What if you decided that all employees get an automatic 10% off their BMI as calculated in the Person’s implementation? You could, of course, retype the code in the Employee implementation, but it would be so much more convenient to call Person’s version of bodyMassIndex and multiply the result by 0.9 before returning it. To do this, you use the super directive. Try it in Employee.m:

Return Main Page Previous Page Next Page

®Online Book Reader