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.

Type Name<br />

Interface<br />

Light<br />

+on()<br />

+off()<br />

+brighten()<br />

+dim()<br />

Light lt;<br />

lt.on();<br />

The <strong>in</strong>terface establishes what requests you can make for a<br />

particular object. However, there must be code somewhere to<br />

satisfy that request. This, along with the hidden data, comprises the<br />

implementation. From a proc<strong>ed</strong>ural programm<strong>in</strong>g standpo<strong>in</strong>t, it’s<br />

not that complicat<strong>ed</strong>. A type has a function associat<strong>ed</strong> with each<br />

possible request, and when you make a particular request to an<br />

object, that function is call<strong>ed</strong>. This process is usually summariz<strong>ed</strong><br />

by say<strong>in</strong>g that you “send a message” (make a request) to an object,<br />

and the object figures out what to do with that message (it executes<br />

code).<br />

Here, the name of the type/class is Light, the name of this<br />

particular Light object is lt, and the requests that you can make of a<br />

Light object are to turn it on, turn it off, make it brighter or make it<br />

dimmer. You create a Light object by simply declar<strong>in</strong>g a name (lt<br />

lt)<br />

for that identifier. To send a message to the object, you state the<br />

name of the object and connect it to the message request with a<br />

period (dot). From the standpo<strong>in</strong>t of the user of a pre-def<strong>in</strong><strong>ed</strong> class,<br />

that’s pretty much all there is to programm<strong>in</strong>g with objects.<br />

The diagram shown above follows the format of the Unifi<strong>ed</strong><br />

Model<strong>in</strong>g Language (UML). Each class is represent<strong>ed</strong> by a box, with<br />

the type name <strong>in</strong> the top portion of the box, any data members that<br />

you care to describe <strong>in</strong> the middle portion of the box, and the<br />

member functions (the functions that belong to this object, which<br />

receive any messages you send to that object) <strong>in</strong> the bottom portion<br />

of the box. The ‘+’ signs before the member functions <strong>in</strong>dicate they<br />

are public. Very often, only the name of the class and the public<br />

member functions are shown <strong>in</strong> UML design diagrams, and so the<br />

1: Introduction to Objects 45

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

Saved successfully!

Ooh no, something went wrong!