20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

418 Chapter 19 Object-Oriented <strong>Programm<strong>in</strong>g</strong><br />

When you declare a new method (and similar to declar<strong>in</strong>g a function), you tell the<br />

Objective-C compiler whether the method returns a value, and if it does, what type of<br />

value it returns.This is done by enclos<strong>in</strong>g the return type <strong>in</strong> parentheses after the lead<strong>in</strong>g<br />

m<strong>in</strong>us or plus sign. So, the declaration<br />

-(<strong>in</strong>t) numerator;<br />

specifies that the <strong>in</strong>stance method called numerator returns an <strong>in</strong>teger value. Similarly,<br />

the l<strong>in</strong>e<br />

-(void) setNumerator: (<strong>in</strong>t) num;<br />

def<strong>in</strong>es a method that doesn’t return a value that can be used to set the numerator of<br />

your fraction.<br />

When a method takes an argument, you append a colon to the method name when<br />

referr<strong>in</strong>g to the method.Therefore, the correct way to identify these two methods is<br />

setNumerator: and setDenom<strong>in</strong>ator:—each of which takes a s<strong>in</strong>gle argument. Also, the<br />

identification of the numerator and denom<strong>in</strong>ator methods without a trail<strong>in</strong>g colon <strong>in</strong>dicates<br />

that these methods do not take any arguments.<br />

The setNumerator: method takes the <strong>in</strong>teger argument you called num and simply<br />

stores it <strong>in</strong> the <strong>in</strong>stance variable numerator. Similarly, setDenom<strong>in</strong>ator: stores the value<br />

of its argument denom <strong>in</strong> the <strong>in</strong>stance variable denom<strong>in</strong>ator.Note that methods have<br />

direct access to their <strong>in</strong>stance variables.<br />

The last method def<strong>in</strong>ed <strong>in</strong> your Objective-C program is called pr<strong>in</strong>t. It’s use is to<br />

display the value of a fraction. As you see, it takes no arguments and returns no results. It<br />

simply uses pr<strong>in</strong>tf to display the numerator and denom<strong>in</strong>ator of the fraction, separated<br />

by a slash.<br />

Inside ma<strong>in</strong>,you def<strong>in</strong>e a variable called myFract with the follow<strong>in</strong>g l<strong>in</strong>e:<br />

Fraction *myFract;<br />

This l<strong>in</strong>e says that myFract is an object of type Fraction; that is, myFract is used to<br />

store values from your new Fraction class.The asterisk (*) <strong>in</strong> front of myFraction says<br />

that myFract is actually a po<strong>in</strong>ter to a Fraction. In fact, it po<strong>in</strong>ts to the structure that<br />

conta<strong>in</strong>s the data for a particular <strong>in</strong>stance from the Fraction class.<br />

Now that you have an object to store a Fraction,you need to create one, just like<br />

you ask the factory to build you a new car.This is done with the follow<strong>in</strong>g l<strong>in</strong>e:<br />

myFract = [Fraction new];<br />

You want to allocate memory storage space for a new fraction.The expression<br />

[Fraction new]<br />

sends a message to your newly created Fraction class.You are ask<strong>in</strong>g the Fraction class<br />

to apply the new method, but you never def<strong>in</strong>ed a new method, so where did it come<br />

from? The method was <strong>in</strong>herited from a parent class.<br />

You are now ready to set the value of your Fraction.The program l<strong>in</strong>es<br />

[myFract setNumerator: 1];<br />

[myFract setDenom<strong>in</strong>ator: 3];

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

Saved successfully!

Ooh no, something went wrong!