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.

Def<strong>in</strong><strong>in</strong>g an Objective-C Class to Work with Fractions<br />

417<br />

The name of the new class is Fraction, and its parent class is Object.Classes <strong>in</strong>herit<br />

methods and variables from their parents.<br />

As you can see <strong>in</strong> the @<strong>in</strong>terface section, the declarations<br />

<strong>in</strong>t numerator;<br />

<strong>in</strong>t denom<strong>in</strong>ator;<br />

say that a Fraction object has two <strong>in</strong>teger members called numerator and<br />

denom<strong>in</strong>ator.<br />

The members declared <strong>in</strong> this section are known as the <strong>in</strong>stance variables. Each time<br />

you create a new object, a new and unique set of <strong>in</strong>stance variables is created.Therefore,<br />

if you have two fractions, one called fracA and another called fracB, each has its own<br />

set of <strong>in</strong>stance variables.That is, fracA and fracB each has its own separate numerator<br />

and denom<strong>in</strong>ator.<br />

You have to def<strong>in</strong>e methods to work with your fractions.You need to be able to set<br />

the value of a fraction to a particular value. Because you don’t have direct access to the<br />

<strong>in</strong>ternal representation of a fraction (<strong>in</strong> other words, direct access to its <strong>in</strong>stance variables),<br />

you must write methods to set the numerator and denom<strong>in</strong>ator (these are known<br />

as setters).You also need methods to retrieve the values of your <strong>in</strong>stance variables (such<br />

methods are known as getters). 1<br />

The fact that the <strong>in</strong>stance variables for an object are kept hidden from the user of the<br />

object is another key concept of OOP known as data encapsulation.This assures someone<br />

extend<strong>in</strong>g or modify<strong>in</strong>g a class that all the code that accesses the data (that is, the<br />

<strong>in</strong>stance variables) for that class is conta<strong>in</strong>ed <strong>in</strong> the methods. Data encapsulation provides<br />

a nice layer of <strong>in</strong>sulation between the programmer and the class developer.<br />

Here’s what one such setter method declaration looks like:<br />

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

The lead<strong>in</strong>g m<strong>in</strong>us sign (-) says that the method is an <strong>in</strong>stance method.The only other<br />

option is a plus sign (+), which <strong>in</strong>dicates a class method. A class method is one that performs<br />

some operation on the class itself, such as creat<strong>in</strong>g a new <strong>in</strong>stance of the class.This<br />

is similar to manufactur<strong>in</strong>g a new car, <strong>in</strong> that the car is the class and you want to create a<br />

new one—which would be a class method.<br />

An <strong>in</strong>stance method performs some operation on a particular <strong>in</strong>stance of a class, such<br />

as sett<strong>in</strong>g its value, retriev<strong>in</strong>g its value, display<strong>in</strong>g its value, and so on. Referr<strong>in</strong>g to the<br />

car example, after you have manufactured the car, you might need to fill it with gas.The<br />

operation of fill<strong>in</strong>g it with gas is performed on a particular car, so it is analogous to an<br />

<strong>in</strong>stance method.<br />

1.You can get direct access to the <strong>in</strong>stance variables, but it’s generally considered poor programm<strong>in</strong>g<br />

practice.

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

Saved successfully!

Ooh no, something went wrong!