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.

98 Day 4<br />

Part of a well-designed class <strong>in</strong>cludes hid<strong>in</strong>g anyth<strong>in</strong>g from public view that the user of the<br />

class doesn’t need to know.<br />

Data abstraction is the hid<strong>in</strong>g of <strong>in</strong>ternal implementations with<strong>in</strong> the class from<br />

outside view.<br />

NEW TERM<br />

Data abstraction prevents the user from know<strong>in</strong>g more than he or she needs to know about<br />

the class, but also prevents the user from mess<strong>in</strong>g with th<strong>in</strong>gs that shouldn’t be messed with.<br />

For <strong>in</strong>stance, when you get <strong>in</strong> your car and turn the key to start the car, do you want to know<br />

every detail about how the car operates? Of course not. You only want to know as much as<br />

you need to know to operate the car safely. So <strong>in</strong> this analogy the steer<strong>in</strong>g wheel, pedals, gear<br />

shift lever, speedometer, and so on represent the public <strong>in</strong>terface between the car and the<br />

driver. The driver knows which of those components to manipulate <strong>in</strong> order to make the car<br />

perform the way he wants.<br />

Conversely, the eng<strong>in</strong>e, drive tra<strong>in</strong>, and electrical system of the car are hidden from public<br />

view. The eng<strong>in</strong>e is tucked neatly away where you never have to look at it if you don’t want<br />

to. (That’s what service stations are for!) It’s a detail that you don’t need to know about, so<br />

it is hidden from you—kept private, if you prefer. Imag<strong>in</strong>e how much trouble driv<strong>in</strong>g would<br />

be if you had to keep track of everyth<strong>in</strong>g the car was do<strong>in</strong>g at all times: Is the carburetor gett<strong>in</strong>g<br />

enough gas? Does the differential have enough grease? Is the alternator produc<strong>in</strong>g adequate<br />

voltage for both the ignition and the radio to operate? Are the <strong>in</strong>take valves open<strong>in</strong>g properly?<br />

Arggghhhh!!! Who needs it! In the same way, a class keeps its <strong>in</strong>ternal implementation private<br />

so the user of the class doesn’t have to worry about what’s go<strong>in</strong>g on under the hood. The<br />

<strong>in</strong>ternal work<strong>in</strong>gs of the class are kept private, and the user <strong>in</strong>terface is public.<br />

The protected access level is a little harder to expla<strong>in</strong>. Protected class members, like private<br />

class members, cannot be accessed by users of the class. They can, however, be accessed by<br />

classes that are derived from this class. I will talk about protected access more a little later, <strong>in</strong><br />

the section “Member Functions.”<br />

The <strong>C++</strong> language has three keywords that perta<strong>in</strong> to class access. The keywords are (not<br />

surpris<strong>in</strong>gly) public, private, and protected. You specify a class member’s access level when<br />

you declare the class. A class is declared with the class keyword. A class declaration looks like<br />

a structure declaration with the access modifiers added:<br />

class Vehicle {<br />

public:<br />

bool haveKey;<br />

bool Start();<br />

void SetGear(<strong>in</strong>t gear);<br />

void Accelerate(<strong>in</strong>t acceleration);<br />

void Break(<strong>in</strong>t factor);<br />

void Turn(<strong>in</strong>t direction);

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

Saved successfully!

Ooh no, something went wrong!