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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5. All objects of a particular type can receive the same messages. This<br />

is actually a very load<strong>ed</strong> statement, as you will see later. Because an<br />

object of type “circle” is also an object of type “shape,” a circle is<br />

guarante<strong>ed</strong> to receive shape messages. This means you can write<br />

code that talks to shapes and automatically handle anyth<strong>in</strong>g that<br />

fits the description of a shape. This substitutability is one of the<br />

most powerful concepts <strong>in</strong> OOP.<br />

An object has an <strong>in</strong>terface<br />

Aristotle was probably the first to beg<strong>in</strong> a careful study of the<br />

concept of type; he spoke of th<strong>in</strong>gs such as “the class of fishes and<br />

the class of birds.” The idea that all objects, while be<strong>in</strong>g unique, are<br />

also part of a class of objects that have characteristics and behaviors<br />

<strong>in</strong> common was directly us<strong>ed</strong> <strong>in</strong> the first object-orient<strong>ed</strong> language,<br />

Simula-67, with its fundamental keyword class that <strong>in</strong>troduces a<br />

new type <strong>in</strong>to a program.<br />

Simula, as its name implies, was creat<strong>ed</strong> for develop<strong>in</strong>g simulations<br />

such as the classic “bank teller problem 2 .” In this, you have a bunch<br />

of tellers, customers, accounts, transactions, units of money – a lot<br />

of “objects.” Objects that are identical except for their state dur<strong>in</strong>g a<br />

program’s execution are group<strong>ed</strong> together <strong>in</strong>to “classes of objects”<br />

and that’s where the keyword class came from. Creat<strong>in</strong>g abstract<br />

data types (classes) is a fundamental concept <strong>in</strong> object-orient<strong>ed</strong><br />

programm<strong>in</strong>g. Abstract data types work almost exactly like built-<strong>in</strong><br />

types: You can create variables of a type (call<strong>ed</strong> objects or <strong>in</strong>stances<br />

<strong>in</strong> object-orient<strong>ed</strong> parlance) and manipulate those variables (call<strong>ed</strong><br />

send<strong>in</strong>g messages or requests; you send a message and the object<br />

figures out what to do with it). The members (elements) of each<br />

class share some commonality: every account has a balance, every<br />

teller can accept a deposit, etc. At the same time, each member has<br />

its own state, each account has a different balance, each teller has a<br />

name. Thus the tellers, customers, accounts, transactions, etc., can<br />

each be represent<strong>ed</strong> with a unique entity <strong>in</strong> the computer program.<br />

This entity is the object, and each object belongs to a particular<br />

class that def<strong>in</strong>es its characteristics and behaviors.<br />

2 You can f<strong>in</strong>d an <strong>in</strong>terest<strong>in</strong>g implementation of this problem later <strong>in</strong> the book.<br />

1: Introduction to Objects 43

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

Saved successfully!

Ooh no, something went wrong!