04.01.2015 Views

Thinking in C++ 2nd ed Volume 1 Revision 6

Thinking in C++ 2nd ed Volume 1 Revision 6

Thinking in C++ 2nd ed Volume 1 Revision 6

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

actually a service to users because they can easily see what’s<br />

important to them and what they can ignore.<br />

The second reason for access control is to allow the library designer<br />

to change the <strong>in</strong>ternal work<strong>in</strong>gs of the class without worry<strong>in</strong>g about<br />

how it will affect the client programmer. For example, you might<br />

implement a particular class <strong>in</strong> a simple fashion to ease<br />

development, and then later discover you ne<strong>ed</strong> to rewrite it <strong>in</strong> order<br />

to make it run faster. If the <strong>in</strong>terface and implementation are<br />

clearly separat<strong>ed</strong> and protect<strong>ed</strong>, you can easily accomplish this and<br />

require only a rel<strong>in</strong>k by the user.<br />

<strong>C++</strong> uses three explicit keywords to set the boundaries <strong>in</strong> a class:<br />

public, private, protect<strong>ed</strong>. Their use and mean<strong>in</strong>g are quite<br />

straightforward. These access specifiers determ<strong>in</strong>e who can use the<br />

def<strong>in</strong>itions that follow. public means the follow<strong>in</strong>g def<strong>in</strong>itions are<br />

available to everyone. The private keyword, on the other hand,<br />

means that no one can access those def<strong>in</strong>itions except you, the<br />

creator of the type, <strong>in</strong>side function members of that type. private is<br />

a brick wall between you and the client programmer. If someone<br />

tries to access a private member, they’ll get a compile-time error.<br />

protect<strong>ed</strong> acts just like private, with the exception that an <strong>in</strong>herit<strong>in</strong>g<br />

class has access to protect<strong>ed</strong> members, but not private members.<br />

Inheritance will be <strong>in</strong>troduc<strong>ed</strong> shortly.<br />

Reus<strong>in</strong>g<br />

the implementation<br />

Once a class has been creat<strong>ed</strong> and test<strong>ed</strong>, it should (ideally)<br />

represent a useful unit of code. It turns out that this reusability is<br />

not nearly so easy to achieve as many would hope; it takes<br />

experience and <strong>in</strong>sight to produce a good design. But once you have<br />

such a design, it begs to be reus<strong>ed</strong>. Code reuse is one of the greatest<br />

advantages that object-orient<strong>ed</strong> programm<strong>in</strong>g languages provide.<br />

The simplest way to reuse a class is to just use an object of that class<br />

directly, but you can also place an object of that class <strong>in</strong>side a new<br />

class. We call this “creat<strong>in</strong>g a member object.” Your new class can<br />

be made up of any number and type of other objects, whatever is<br />

1: Introduction to Objects 47

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

Saved successfully!

Ooh no, something went wrong!