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.

118 Day 4<br />

NEW TERM<br />

A virtual function is a function that will be automatically called if a function by that<br />

name exists <strong>in</strong> the derived class.<br />

For example, note that the TakeOff() function is a virtual function <strong>in</strong> the Airplane class. Refer<br />

to List<strong>in</strong>g 4.2. Notice that TakeOff() is called by SendMessage() <strong>in</strong> response to the<br />

MSG_TAKEOFF message. If the MilitaryPlane class did not provide its own TakeOff() function,<br />

the base class’s TakeOff() function would be called. Because the MilitaryPlane class does<br />

provide a TakeOff() function, that function, rather than the function <strong>in</strong> the base class, will<br />

be called.<br />

NEW TERM<br />

Replac<strong>in</strong>g a base class function <strong>in</strong> a derived class is called overrid<strong>in</strong>g the function.<br />

In order for overrid<strong>in</strong>g to work, the function signature must exactly match that of the<br />

function <strong>in</strong> the base class. In other words, the return type, function name, and parameter list<br />

must all be the same as the base class function.<br />

You can override a function with the <strong>in</strong>tention of replac<strong>in</strong>g the base class function, or you<br />

can override a function to enhance the base class function. Take the TakeOff() function, for<br />

example. If you wanted to completely replace what the TakeOff() function of Airplane does,<br />

you would override it and supply whatever code you wanted:<br />

void MilitaryPlane::TakeOff(<strong>in</strong>t dir)<br />

{<br />

// new code goes here<br />

}<br />

But if you wanted your function to take the functionality of the base class and add to it, you<br />

would first call the base class function and then add new code:<br />

void MilitaryPlane::TakeOff(<strong>in</strong>t dir)<br />

{<br />

Airplane::TakeOff(dir);<br />

// new code goes here<br />

}<br />

By call<strong>in</strong>g the base class function, you get the orig<strong>in</strong>al functionality of the function. You could<br />

then add code before or after the base class call to enhance the function. The scope-resolution<br />

operator is used to tell the compiler that you are call<strong>in</strong>g the TakeOff() function of the<br />

Airplane class. Note that the TakeOff() function is <strong>in</strong> the protected section of the Airplane<br />

class. If it were <strong>in</strong> the private section, this would not work because even a derived class cannot<br />

access the private members of its ancestor class. By mak<strong>in</strong>g the TakeOff() function protected,<br />

it is hidden from the outside world but still accessible to derived classes.

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

Saved successfully!

Ooh no, something went wrong!