15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

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

258<br />

Real-Life Programming Examples<br />

multiplies them by 2, and uses that result <strong>for</strong> the cow object’s new X and Y<br />

coordinates.<br />

Not every object-oriented language requires you to identify polymorphic<br />

subprograms. Some languages let you inherit and modify subprograms without<br />

identifying them first.<br />

So running the main program would now print the following on-screen:<br />

X-location = 1<br />

Y-location = 1<br />

New X-location = 48<br />

New Y-location = 18<br />

The cow’s initial IQ is 45<br />

The cow’s IQ is now 81<br />

Notice that the main program now uses the Move subprogram stored in<br />

the human class, which multiplies each number by 2. So the cow.move<br />

(24, 9) command sets the cow’s x_coordinate to 48 (24 * 2) and the<br />

y_coordinate to 18 (9 * 2).<br />

Although these examples use C++, the basic steps to using objects remain<br />

the same:<br />

1. Create a class that defines the data and subprograms used by an<br />

object.<br />

2. Create one or more additional classes that inherit code from another<br />

class.<br />

3. Use polymorphism to rewrite code inherited from another object.<br />

4. Declare an object as a specific class type.<br />

5. Use an object’s subprograms to manipulate that object’s data.<br />

Object-oriented <strong>programming</strong> can help you design large programs faster, but<br />

ultimately, your own skill as a programmer determines the quality of your<br />

programs.

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

Saved successfully!

Ooh no, something went wrong!