15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

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

232<br />

How Object-Oriented Programming Works<br />

To overcome the limitations of subprograms, computer scientists invented<br />

object-oriented <strong>programming</strong> (abbreviated as OOP). Like structured <strong>programming</strong>,<br />

which encourages you to break a large program into subprograms,<br />

OOP encourages you to break a large program into smaller parts, or<br />

objects.<br />

Object-oriented <strong>programming</strong> has actually been around since 1962 when two<br />

Norwegian computer scientists Ole-Johan Dahl and Kristen Nygaard developed<br />

a language called SIMULA, which was designed to help simulate realworld<br />

events. It took object-oriented <strong>programming</strong> nearly 40 more years to<br />

finally get accepted as a practical tool, so just because an idea is proven to<br />

work doesn’t mean people will accept it if they can continue being com<strong>for</strong>table<br />

(and getting paid) to keep doing something that doesn’t work.<br />

How Object-Oriented Programming Works<br />

Like subprograms, objects divide a large program into smaller, interchangeable<br />

parts. The main difference is that subprograms divide a program into<br />

separate tasks whereas objects divide a program into real-world items.<br />

For example, consider a hotel reservation program used by the front desk<br />

when a guest checks in. Dividing this problem into tasks might create the<br />

following:<br />

✦ Subprogram #1: RoomAvailable (Checks if a hotel room is available)<br />

✦ Subprogram #2: RoomBeds (Checks if the room has 1 or 2 beds)<br />

✦ Subprogram #3: RoomType (Checks if it’s a smoking or a nonsmoking<br />

room)<br />

✦ Subprogram #4: RoomPrice (Checks the price)<br />

Dividing this problem into objects, you could create the following:<br />

✦ Object #1: Guest<br />

✦ Object #2: Front desk clerk<br />

✦ Object #3: Hotel room<br />

Figure 7-1 shows how a task-oriented solution might break a program into<br />

multiple subprograms. The main program works by running each subprogram,<br />

one at a time, with each subprogram per<strong>for</strong>ming a specific task (such<br />

as determining whether a room is smoking or nonsmoking).

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

Saved successfully!

Ooh no, something went wrong!