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.

forc<strong>ed</strong> to use the idioms of the underly<strong>in</strong>g mach<strong>in</strong>e. These concepts<br />

are express<strong>ed</strong> as fundamental units <strong>in</strong> the programm<strong>in</strong>g language<br />

by us<strong>in</strong>g the class keyword.<br />

It seems a pity, however, to go to all the trouble to create a class and<br />

then be forc<strong>ed</strong> to create a brand new one that might have similar<br />

functionality. It’s nicer if we can take the exist<strong>in</strong>g class, clone it and<br />

make additions and modifications to the clone. This is effectively<br />

what you get with <strong>in</strong>heritance, with the exception that if the orig<strong>in</strong>al<br />

class (call<strong>ed</strong> the base or super or parent class) is chang<strong>ed</strong>, the<br />

modifi<strong>ed</strong> “clone” (call<strong>ed</strong> the deriv<strong>ed</strong> or <strong>in</strong>herit<strong>ed</strong> or sub or child<br />

class) also reflects those changes.<br />

Base<br />

Deriv<strong>ed</strong><br />

(The arrow <strong>in</strong> the above UML diagram po<strong>in</strong>ts from the deriv<strong>ed</strong> class<br />

to the base class. As you shall see, there can be more than one<br />

deriv<strong>ed</strong> class.)<br />

A type does more than describe the constra<strong>in</strong>ts on a set of objects; it<br />

also has a relationship with other types. Two types can have<br />

characteristics and behaviors <strong>in</strong> common, but one type may conta<strong>in</strong><br />

more characteristics than another and may also handle more<br />

messages (or handle them differently). Inheritance expresses this<br />

similarity between types with the concept of base types and deriv<strong>ed</strong><br />

types. A base type conta<strong>in</strong>s all the characteristics and behaviors that<br />

are shar<strong>ed</strong> among the types deriv<strong>ed</strong> from it. You create a base type<br />

to represent the core of your ideas about some objects <strong>in</strong> your<br />

system. From the base type, you derive other types to express the<br />

different ways that core can be realiz<strong>ed</strong>.<br />

For example, a trash-recycl<strong>in</strong>g mach<strong>in</strong>e sorts pieces of trash. The<br />

base type is “trash,” and each piece of trash has a weight, a value,<br />

and so on and can be shr<strong>ed</strong>d<strong>ed</strong>, melt<strong>ed</strong>, or decompos<strong>ed</strong>. From this,<br />

1: Introduction to Objects 49

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

Saved successfully!

Ooh no, something went wrong!