15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

252<br />

Real-Life Programming Examples<br />

If you stored the animal class in an “animal.h” file, you’d tell your main<br />

C++ program to use this “animal.h” file by using the include command<br />

like this:<br />

#include “animal.h”<br />

Next, you’d have to define an object name and declare it as an object based<br />

on a class. So if you wanted to create a cow object with the animal class,<br />

you’d use this:<br />

#include “animal.h”<br />

int main()<br />

{<br />

animal cow;<br />

return 0;<br />

}<br />

The #include “animal.h” command tells the program to use the class<br />

stored in the “animal.h” file. Most C++ also use multiple #include commands<br />

to make a program work, so the entire C++ program up to this point<br />

actually looks like this:<br />

#include <br />

#include <br />

#include “animal.h”<br />

int main()<br />

{<br />

animal cow;<br />

return 0;<br />

}<br />

Running subprograms stored in an object<br />

After you define an object in your main program, you can run a subprogram<br />

stored in that object. So if you wanted to run the initial_position subprogram,<br />

stored in the cow object, you’d identify the object name followed<br />

by the object’s subprogram to run, such as<br />

cow.initial_position (1,1);<br />

The cow.initial_position (1,1) command tells the computer to use<br />

the cow object, run the initial_position subprogram stored in that cow<br />

object, and use the values 1 and 1.

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

Saved successfully!

Ooh no, something went wrong!