12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

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

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

116 Day 4<br />

NOTE<br />

Another way of declar<strong>in</strong>g constants is to declare a variable us<strong>in</strong>g the<br />

const modifier. Here’s an example:<br />

const <strong>in</strong>t airl<strong>in</strong>er = 0;<br />

Us<strong>in</strong>g a const variable is probably the more modern method of<br />

def<strong>in</strong><strong>in</strong>g constants.<br />

The next th<strong>in</strong>g to note <strong>in</strong> the header is that the class <strong>in</strong>cludes some <strong>in</strong>l<strong>in</strong>e functions. These<br />

functions are so small that it makes sense to <strong>in</strong>l<strong>in</strong>e them. You might also notice that the<br />

Airplane class has one overloaded function called GetStatus(). When called with a character<br />

array parameter, it will return a status str<strong>in</strong>g as well as the status data member. When called<br />

without a parameter, it just returns status. Note that there is only one public data member.<br />

The rest of the data members are kept private. The only way to access the private data<br />

members is via the public functions. For <strong>in</strong>stance, you can change the speed, altitude, and<br />

head<strong>in</strong>g of an airplane only by send<strong>in</strong>g it a message. To use an analogy, consider that an air<br />

traffic controller cannot physically change an aircraft’s head<strong>in</strong>g. The best he can do is send<br />

a message to the pilot and tell him to change to a new head<strong>in</strong>g.<br />

Now turn your attention to List<strong>in</strong>g 4.2. This is the def<strong>in</strong>ition of the Airplane class. The<br />

constructor performs <strong>in</strong>itialization, <strong>in</strong>clud<strong>in</strong>g dynamically allocat<strong>in</strong>g storage for the char<br />

array that holds the name of the airplane. That memory is freed <strong>in</strong> the destructor. The<br />

SendMessage() function does most of the work. A switch statement determ<strong>in</strong>es which<br />

message was sent and takes the appropriate action. Notice that the TakeOff() and Land()<br />

functions cannot be called directly (they are protected), but rather are called through the<br />

SendMessage() function. Aga<strong>in</strong>, you can’t make an aircraft take off or land; you can only send<br />

it a message tell<strong>in</strong>g it what you want it to do. The ReportStatus() function calls GetStatus()<br />

to get a status str<strong>in</strong>g, which it outputs.<br />

The ma<strong>in</strong> program is shown <strong>in</strong> List<strong>in</strong>g 4.3. The program first sets up an array of Airplane<br />

po<strong>in</strong>ters and creates three <strong>in</strong>stances of the Airplane class. Then a loop starts. You can send<br />

messages to any of the airplanes by call<strong>in</strong>g the object’s SendMessage() function. When you<br />

send a message, you get a response back from the airplane. The do-while loop cheats a little<br />

<strong>in</strong> this program. Notice that the test condition is simply 1. This means that the loop will keep<br />

runn<strong>in</strong>g <strong>in</strong>def<strong>in</strong>itely. In this case it’s not a problem because I am us<strong>in</strong>g the break statement<br />

to break out of the loop rather than rely<strong>in</strong>g on the test condition. Run the program and play<br />

with it to get a feel for how it works.<br />

Inheritance<br />

One of the most powerful features of classes <strong>in</strong> <strong>C++</strong> is that they can be extended through<br />

<strong>in</strong>heritance.

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

Saved successfully!

Ooh no, something went wrong!