Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [36]
You can also link in libraries of code. For example, you might find a great library for parsing data from digital telescopes on the Internet. If your program needed this capability, you would compile that library and add it to your project. When Xcode built your program, it would link in all the functions and classes defined in that library.
Challenge
Create new Foundation Command Line Tool called Stocks. Then create a class called StockHolding to represent a stock that you have purchased. It will be a subclass of NSObject. For instance variables, it will have two floats named purchaseSharePrice and currentSharePrice and one int named numberOfShares. Create accessor methods for the instance variables. Create two other instance methods:
- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOfShares
In main(), fill an array with three instances of StockHolding. Then iterate through the array printing out the value of each.
Figure 17.2 An array of StockHolding objects
18
Inheritance
When you created the Person class, you declared it to be a subclass of NSObject. This means that every instance of Person will have the instance variables and methods defined in NSObject as well as the instance variables and methods defined in Person. We say that Person inherits the instance variables and methods from NSObject. In this section, we are going to delve a bit into inheritance.
Open up the BMITime project and create a new file: an Objective-C class. Name it Employee and create it as a subclass of NSObject. Soon, we will change the Employee class to be a subclass of Person. Makes sense, right? Employees are people. They have heights and weights. However, not all people are employees. We’ll also add an employee-specific instance variable to our class – an employee ID.
Figure 18.1 Inheritance diagram of some classes you know
Open Employee.h. Import Person.h, change the superclass to Person, and add an instance variable to hold the employee’s ID number:
#import "Person.h"
@interface Employee : Person
{
int employeeID;
}
@property int employeeID;
@end
Open Employee.m and synthesize the accessors:
#import "Employee.h"
@implementation Employee
@synthesize employeeID;
@end
Now you have a new Employee class with all the instance variables of Person and a new instance variable called employeeID. Instances of Employee will respond to all the same messages that an instance of Person will. Instances of Employee will also respond to the messages setEmployeeID: and employeeID.
And, because Person inherits from NSObject, Employee also inherits all the instance variables and methods from NSObject. All objects inherit (either directly or indirectly) from NSObject.
NSObject has many methods, but only one instance variable: the isa pointer. Every object’s isa pointer points back to the class that created it.
Figure 18.2 Object diagram for BMITime
Let’s say that you send the message fido to an object. In order to respond to this message, the object uses the isa pointer to find its class and ask, “Do you have an instance method named fido?” If the class has a method named fido, it gets executed. If the class doesn’t have a fido method, it asks its superclass, “Do you have an instance method called fido?” And up, up the chain it goes on the hunt for a method named fido. The hunt stops when the method is found or when the top of the chain is reached. At the top of the chain, NSObject says, “Nope, no fido method.” Then you get an error message that says something like “This instance of Employee does not respond to the fido selector.”
Try it. Open up main.m and send the instance of Person a message it won’t understand:
int main(int argc,