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.

NOTE<br />

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

The scope-resolution operator is required only when you have derived<br />

and base class functions with the same name and the same function<br />

signature. You can call a public or protected function of the base class<br />

at any time without the need for the scope-resolution operator,<br />

provided they aren’t overridden. For example, if you wanted to check<br />

the status of the aircraft prior to takeoff, you could do someth<strong>in</strong>g like<br />

this:<br />

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

{<br />

if (GetStatus() != ONRAMP) Land(); // gotta land first!<br />

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

// new code goes here<br />

}<br />

In this case, the GetStatus() function exists only <strong>in</strong> the base class, so<br />

there is no need for the scope-resolution operator. In the case of the<br />

Land() function, the MilitaryPlane version will be called because it has<br />

the most immediate scope.<br />

When you derive a class from another class, you must be sure to call the base class’s<br />

constructor so that all ancestor classes are properly <strong>in</strong>itialized. Call<strong>in</strong>g the base class<br />

constructor is done <strong>in</strong> the <strong>in</strong>itializer list. Here’s how the constructor for MilitaryPlane might<br />

look:<br />

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

: Airplane(_name, MILITARY) // call base class<br />

{<br />

// body of constructor<br />

}<br />

Be sure to call the base class constructor whenever you derive a class from a base class. Figure<br />

4.1 illustrates the concept of <strong>in</strong>heritance.<br />

Figure 4.1.<br />

An example of<br />

<strong>in</strong>heritance.<br />

MilitaryFighter MilitaryCargo Airl<strong>in</strong>er Commuter S<strong>in</strong>gleEng<strong>in</strong>e Tw<strong>in</strong>Eng<strong>in</strong>e<br />

F16<br />

C130<br />

Airplane<br />

Commerical<br />

MilitaryPlane Airplane<br />

CivilianPlane<br />

PrivatePlane CargoPlane<br />

119<br />

4

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

Saved successfully!

Ooh no, something went wrong!