Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

Chapter 11

Examining Object-Oriented

Programming

In This Chapter

Making nachos

Reviewing object-oriented programming

Introducing abstraction and classification

Discovering why object-oriented programming is important

What, exactly, is object-oriented programming? Object-oriented pro­ gramming, or OOP as those in the know prefer to call it, relies on two

principles you learned before you ever got out of Pampers: abstraction and classification. To explain, let me tell you a little story.

Abstracting Microwave Ovens

Sometimes when my son and I are watching football (which only happens when my wife can’t find the switcher), I whip up a terribly unhealthy batch of nachos. I dump some chips on a plate, throw on some beans, cheese, and lots of jalapeños, and nuke the whole mess in the microwave oven for five minutes.

To use my microwave, I open the door, throw the stuff in, and punch a few buttons. After a few minutes, the nachos are done. (I try not to stand in front of the microwave while it’s working lest my eyes start glowing in the dark.)

Now think for a minute about all the things I don’t do to use my microwave:

I don’t rewire or change anything inside the microwave to get it to work. The microwave has an interface — the front panel with all the buttons and the little time display — that lets me do everything I need.

I don’t have to reprogram the software used to drive the little processor inside my microwave, even if I cooked a different dish the last time I used the microwave.

158 Part III: Introduction to Classes

I don’t look inside my microwave’s case.

Even if I were a microwave designer and knew all about the inner work­ ings of a microwave, including its software, I would still use it to heat my nachos without thinking about all that stuff.

These are not profound observations. You can deal with only so much stress in your life. To reduce the number of things that you deal with, you work at a certain level of detail. In object-oriented (OO) computerese, the level of detail at which you are working is called the level of abstraction. To introduce another OO term while I have the chance, I abstract away the details of the microwave’s innards.

When I’m working on nachos, I view my microwave oven as a box. (As I’m trying to knock out a snack, I can’t worry about the innards of the microwave oven and still follow the Cowboys on the tube.) As long as I operate the microwave only through its interface (the keypad), there should be nothing

I can do to

Cause the microwave to enter an inconsistent state and crash.

Turn my nachos into a blackened, flaming mass.

Burst into flames!

Preparing functional nachos

Suppose that I were to ask my son to write an algorithm for how Dad makes nachos. After he understood what I wanted, he would probably write “open a can of beans, grate some cheese, cut the jalapeños,” and so on. When it came to the part about microwaving the concoction, he would write something like “cook in the microwave for five minutes.”

That description is straightforward and complete. But it’s not the way a func­ tional programmer would code a program to make nachos. Functional pro­ grammers live in a world devoid of objects such as microwave ovens and other appliances. They tend to worry about flow charts with their myriad functional paths. In a functional solution to the nachos problem, the flow of control would pass through my finger to the front panel and then to the inter­ nals of the microwave. Pretty soon, flow would be wiggling around through complex logic paths about how long to turn on the microwave tube and whether to sound the “come and get it” tone.

In a world like this, it’s difficult to think in terms of levels of abstraction. There are no objects, no abstractions behind which to hide inherent complexity.