20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

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.

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

Program 19.2 Cont<strong>in</strong>ued<br />

pr<strong>in</strong>tf ("The value of the fraction is %i/%i\n", numerator, denom<strong>in</strong>ator);<br />

}<br />

@end<br />

//------- program section -------<br />

<strong>in</strong>t ma<strong>in</strong> (void)<br />

{<br />

Fraction<br />

*myFract;<br />

myFract = [Fraction new];<br />

[myFract setNumerator: 1];<br />

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

pr<strong>in</strong>tf ("The numerator is %i, and the denom<strong>in</strong>ator is %i\n",<br />

[myFract numerator], [myFract denom<strong>in</strong>ator]);<br />

[myFract pr<strong>in</strong>t]; // use the method to display the fraction<br />

[myFract free];<br />

}<br />

return 0;<br />

Program 19.2 Output<br />

The numerator is 1, and the denom<strong>in</strong>ator is 3<br />

The value of the fraction is 1/3<br />

As you can see from the comments <strong>in</strong> Program 19.2, the program is logically divided<br />

<strong>in</strong>to three sections: the @<strong>in</strong>terface section, the @implementation section, and the program<br />

section.These sections are typically placed <strong>in</strong> separate files.The @<strong>in</strong>terface section<br />

is usually put <strong>in</strong>to a header file that gets <strong>in</strong>cluded <strong>in</strong> any program that wants to work<br />

with that particular class. It tells the compiler what variables and methods are conta<strong>in</strong>ed<br />

<strong>in</strong> the class.<br />

The @implementation section conta<strong>in</strong>s the actual code that implements these methods.<br />

F<strong>in</strong>ally, the program section conta<strong>in</strong>s the program code to carry out the <strong>in</strong>tended<br />

purpose of the program.

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

Saved successfully!

Ooh no, something went wrong!