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.

Totally Immersed: <strong>C++</strong> Classes and Object-Oriented Programm<strong>in</strong>g<br />

NEW TERM<br />

NEW TERM<br />

NOTE<br />

Inheritance means tak<strong>in</strong>g an exist<strong>in</strong>g class and add<strong>in</strong>g functionality by deriv<strong>in</strong>g a new<br />

class from it.<br />

The class you start with is called the base class, and the new class you create is called<br />

the derived class.<br />

Let’s take the Airplane class as an example. The civilian and military worlds are quite<br />

different, as you know. In order to represent a military aircraft, I can derive a class from<br />

Airplane and add functionality to it:<br />

class MilitaryPlane : public Airplane {<br />

public:<br />

MilitaryPlane(char* name, <strong>in</strong>t _type);<br />

virtual <strong>in</strong>t GetStatus(char* statusStr<strong>in</strong>g);<br />

protected:<br />

virtual void TakeOff();<br />

virtual void Land()<br />

virtual void Attack();<br />

virtual void SetMission();<br />

private:<br />

Mission theMission;<br />

};<br />

A MilitaryPlane has everyth<strong>in</strong>g an Airplane has, plus a few more goodies. Note the first l<strong>in</strong>e<br />

of the class def<strong>in</strong>ition. The colon after the class name is used to tell the compiler that I am<br />

<strong>in</strong>herit<strong>in</strong>g from another class. The class name follow<strong>in</strong>g the colon is the base class from which<br />

I am deriv<strong>in</strong>g. The public keyword, when used here, means that I am claim<strong>in</strong>g access to all<br />

the public functions and data members of the base class.<br />

When you derive a class from another class, the new class gets all the<br />

functionality of the base class plus whatever new features you add. You<br />

can add data members and functions to the new class, but you cannot<br />

remove anyth<strong>in</strong>g from what the base class offers.<br />

You’ll notice that <strong>in</strong> the private section there is a l<strong>in</strong>e that declares a variable of the Mission<br />

class. The Mission class could encapsulate everyth<strong>in</strong>g that deals with the mission of a military<br />

aircraft: the target, navigation waypo<strong>in</strong>ts, <strong>in</strong>gress and egress altitudes and head<strong>in</strong>gs, and so<br />

on. This illustrates the use of a data member that is an <strong>in</strong>stance of another class. In fact, you’ll<br />

see that a lot when programm<strong>in</strong>g <strong>in</strong> <strong>C++</strong>Builder.<br />

There’s someth<strong>in</strong>g else here that I haven’t discussed yet. Note the virtual keyword. This<br />

specifies that the function is a virtual function.<br />

117<br />

4

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

Saved successfully!

Ooh no, something went wrong!