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

Create successful ePaper yourself

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

120 Day 4<br />

You can see from Figure 4.1 that the class called F16 is descended from the class called<br />

MilitaryFighter. Ultimately, F16 is derived from Airplane s<strong>in</strong>ce Airplane is the base class for<br />

all classes.<br />

Multiple Inheritance<br />

NEW TERM<br />

NOTE<br />

The act of deriv<strong>in</strong>g a class from two or more base classes is called multiple <strong>in</strong>heritance.<br />

Multiple <strong>in</strong>heritance is not used frequently, but it can be very handy when needed. For<br />

example, let’s say you had a class called Armaments that kept track of the armaments for a<br />

particular aircraft. It might look like this:<br />

class Armaments {<br />

public:<br />

Armaments();<br />

LoadArms();<br />

private:<br />

bool isArmed;<br />

<strong>in</strong>t numSidew<strong>in</strong>ders;<br />

<strong>in</strong>t numSparrows;<br />

// etc.<br />

};<br />

Now let’s say that you were to create a class to represent a military fighter. You could <strong>in</strong>herit<br />

from both MilitaryPlane and Armaments:<br />

class Fighter : public MilitaryPlane, public Armaments {<br />

public:<br />

Fighter(char* name);<br />

private:<br />

// other stuff<br />

};<br />

Now you have a class that conta<strong>in</strong>s all the public elements of MilitaryPlane and all the public<br />

elements of Armaments. This would allow you to do the follow<strong>in</strong>g:<br />

Fighter fighter(“F16”);<br />

fighter.LoadArms();<br />

fighter.SendMessage(...);<br />

// etc.<br />

The two base classes are blended to form a s<strong>in</strong>gle class.<br />

You should call the base class constructor for all base classes. The<br />

follow<strong>in</strong>g illustrates:<br />

F16::F16(char* _name)<br />

: MilitaryPlane(_name, F16), Armaments()<br />

{

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

Saved successfully!

Ooh no, something went wrong!