01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

142 CHAPTER 6 Extending classes<br />

type of lease will have a fixed time and a fixed amount, and it won’t recur. The header<br />

of CTFixedLease is similar to that of CTLease but in this case has ivars: totalRental<br />

and numberOfWeeks. Insert the code from the following listing into the header file.<br />

Listing 6.15<br />

CTFixedLease.h<br />

#import "CTLease.h"<br />

@interface CTFixedLease : CTLease {<br />

float totalRental;<br />

int numberOfWeeks;<br />

}<br />

@property(nonatomic) float totalRental;<br />

@property(nonatomic) int numberOfWeeks;<br />

+ (CTLease *)fixedTermLeaseWithPrice:(float)totalRental<br />

forWeeks:(int)numberOfWeeks;<br />

@end<br />

The implementation of CTFixedLease is similar to that of CTPeriodicLease. You set<br />

both instance variables for the class and return an autoreleased instance. You also<br />

make sure the description method prints out both of your instance messages in the<br />

description. Use the code in the following listing for the implementation of<br />

CTFixedLease’s main file.<br />

Listing 6.16<br />

CTFixedLease.m<br />

#import "CTFixedLease.h"<br />

@implementation CTFixedLease<br />

@synthesize totalRental, numberOfWeeks;<br />

+ (CTLease *)fixedTermLeaseWithPrice:(float)totalRental<br />

forWeeks:(int)numberOfWeeks {<br />

}<br />

CTFixedLease *lease = [CTFixedLease alloc];<br />

lease.totalRental = totalRental;<br />

lease.numberOfWeeks = numberOfWeeks;<br />

return [lease autorelease];<br />

- (NSString *)description {<br />

return [NSString stringWithFormat:@"$%0.2f for %d weeks",<br />

self.totalRental, self.numberOfWeeks];<br />

}<br />

@end<br />

With this useful collection of classes in place, you can properly model the data for<br />

your application as you continue to build it.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!