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 a C++ Class to Work with Fractions<br />

419<br />

do just that.The first message statement sends the setNumerator: message to myFract.<br />

The argument that is supplied is the value 1. Control is then sent to the setNumerator:<br />

method you def<strong>in</strong>ed for your Fraction class.The Objective-C runtime system knows<br />

that it is the method from this class to use because it knows that myFract is an object<br />

from the Fraction class.<br />

Inside the setNumerator: method, the s<strong>in</strong>gle program l<strong>in</strong>e <strong>in</strong> that method takes the<br />

value passed <strong>in</strong> as the argument and stores it <strong>in</strong> the <strong>in</strong>stance variable numerator.So,you<br />

have effectively set the numerator of myFract to 1.<br />

The message that <strong>in</strong>vokes the setDenom<strong>in</strong>ator: method on myFract follows next,<br />

and works <strong>in</strong> a similar way.<br />

With the fraction be<strong>in</strong>g set, Program 19.2 then calls the two getter methods<br />

numerator and denom<strong>in</strong>ator to retrieve the values of the correspond<strong>in</strong>g <strong>in</strong>stance<br />

variables from myFract.The results are then passed to pr<strong>in</strong>tf to be displayed.<br />

The program next <strong>in</strong>vokes the pr<strong>in</strong>t method.This method displays the value of the<br />

fraction that is the receiver of the message. Even though you saw <strong>in</strong> the program how<br />

the numerator and denom<strong>in</strong>ator could be retrieved us<strong>in</strong>g the getter methods, a separate<br />

pr<strong>in</strong>t method was also added to the def<strong>in</strong>ition of the Fraction class for illustrative purposes.<br />

The last message <strong>in</strong> the program<br />

[myFract free];<br />

frees the memory that was used by your Fraction object.<br />

Def<strong>in</strong><strong>in</strong>g a C++ Class to Work with Fractions<br />

Program 19.3 shows how a program to implement a Fraction class might be written<br />

us<strong>in</strong>g the C++ language. C++ has become an extremely popular programm<strong>in</strong>g language<br />

for software development. It was <strong>in</strong>vented by Bjarne Stroustroup at Bell Laboratories,<br />

and was the first object-oriented programm<strong>in</strong>g language based on C—at least to my<br />

knowledge!<br />

Program 19.3 Work<strong>in</strong>g with Fractions <strong>in</strong> C++<br />

#<strong>in</strong>clude <br />

class Fraction<br />

{<br />

private:<br />

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

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

public:<br />

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

void setDenom<strong>in</strong>ator (<strong>in</strong>t denom);<br />

<strong>in</strong>t Numerator (void);

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

Saved successfully!

Ooh no, something went wrong!