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.

250<br />

Real-Life Programming Examples<br />

public:<br />

private:<br />

};<br />

Underneath both the public and private keywords, you must declare<br />

your data and subprograms. If you wanted to define an X coordinate and<br />

Y coordinate variable as public and an X variable as private, you’d do<br />

this:<br />

class animal<br />

{<br />

public:<br />

int x_coordinate;<br />

int y_coordinate;<br />

private:<br />

int x;<br />

};<br />

Not all objects have both public and private data and subprograms. Some<br />

objects may just have public data and subprograms.<br />

The preceding code creates three integer variables — x_coordinate,<br />

y_coordinate, and x. After you define the data your object will use, the<br />

next step is to define the subprograms it’ll use, such as<br />

class animal<br />

{<br />

public:<br />

int x_coordinate;<br />

int y_coordinate;<br />

void initial_position (int, int);<br />

void move(int, int);<br />

private:<br />

int x;<br />

};<br />

The preceding code defines two subprograms (methods) — initial_<br />

position and move. The initial_position and move subprograms both<br />

accept two integers in their parameter list. After you define the names of the<br />

subprograms you want to use, you have to write the actual code to make<br />

each subprogram work, such as<br />

class animal

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

Saved successfully!

Ooh no, something went wrong!