Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

258 Part IV: Dealing with Data Structures

Subprogram A

Subprogram B

Figure 19-2:

Subprogram D

Instructions

 

from one

 

subprogram

 

can

 

accidentally

 

modify data

Subprogram F

that another

subprogram

 

uses.

 

 

 

 

 

 

 

Subprogram A messes up data used by subprogram D

Subprogram F messes up data used by subprogram B

Breaking Programs into Objects

After programmers succeeded in breaking a large program into smaller subprograms, the next logical step was to isolate both data and the instructions that manipulate that data into an individual unit — in other words, an object.

A program consists of one or more objects in which each object is a selfcontained unit that contains the following two items:

Data (also known as properties)

Instructions (also known as methods) for manipulating that data

Because an object doesn’t depend on any other part of your program, designing a program by using objects provides the following benefits:

Reliability: If your program doesn’t work, you just need to isolate the bug in a single object and debug that one object instead of trying to debug an entire program that may contain a million lines of code.

Reusability: Because objects are self-contained units, you can (theoretically) copy an object from one program and plug it into another program, much like making a structure by using building blocks. Reusing objects not only simplifies creating new programs, but also helps create new programs faster because you can reuse objects that already work.

Chapter 19: Playing with Object-Oriented Programming 259

Getting an inheritance from an object

Objects encourage reliability and reusability through a concept known as inheritance. The main idea behind inheritance is to encourage programmers to reuse existing code.

In the old days of programming, you could divide a large program into several subprograms. After studying one of your subprograms, you may realize that, with a little modification, you could adapt it to work in a second program. Unfortunately, any time that you modify a program, you take the risk of messing it up so that it doesn’t work at all.

To prevent the problem of modifying an existing subprogram and wrecking it by mistake, objects

use inheritance. Inheritance enables you to copy an existing object and then add new code to this new copy without ever modifying any of the existing code inside. Thus the new copy of your object “inherits” all the old data and code, while still enabling you to tack on new code to modify the object for a slightly different purpose.

Inheritance not only protects you from ruining a perfectly good chunk of code by mistake, but it also helps you create programs faster. Copying and modifying an existing object is easier than creating a brand new object from scratch.

In a traditionally designed program, you often store data in one location and the instructions that manipulate that data in another location. Figure 19-3 shows a traditional program divided into subprograms (represented by boxes). Each subprogram can access the data it needs, but can also access data used by another subprogram.

In comparison, an object-oriented program (depicted as ovals in Figure 19-3) lumps data and the instructions that manipulate that data in a single location (known by its official programming term as encapsulation). Encapsulation simply keeps one part of a program from messing with data that another part of the program uses.

How to use objects

One of the biggest problems with programming is the need to constantly modify existing programs. People use most programs for years, if not decades. Rather than write a brand new program to replace an existing one, most companies prefer to modify the existing program. The theory is that modifying an existing program (that already works) takes less time and creates a more reliable program than does creating a brand new program.

260 Part IV: Dealing with Data Structures

 

Subprogram A

 

 

Data

Object A

 

 

Data

 

Subprogram B

Object B

 

 

 

 

Data

 

Subprogram C

 

 

Data

 

 

 

Object C

 

Subprogram D

Data

Figure 19-3:

 

Object D

Subprogram E

Data

In object-

Object E

oriented

 

programs,

Subprogram F

Data

objects

 

isolate data

A typical program divided

The same program divided into multiple objects

from other

into subprograms

 

objects.

 

 

 

Unfortunately, constantly modifying an existing program can create a program as confusing to read as a novel written one page at a time by 300 different people. In both cases, the overall structure is patched together in so many different ways that it can prove hard to tell where one part ends and another part begins.

That’s why object-oriented programming looks so appealing. To modify an object-oriented program, just unplug the object containing the program features that you want to change, rewrite or replace it, and plug it back into the original program.

Best of all, objects make easy work of identifying which part of the program you want to update. Suppose, for example, that a video game displays aliens that pop up on-screen so that you can shoot them in the head. Your job is to modify the way the aliens look and move on-screen.

If the video game is written in the traditional way of programming (dividing the program into smaller subprograms), you must figure out which subprogram controls the way the alien looks and which subprogram controls the way the alien moves. Even if you manage to find which subprograms control the alien’s appearance and movement, you still must worry whether these subprograms rely on other subprograms lurking somewhere deep within the bowels of your million-line program. Sound complicated? It is — especially if you’re the one whose job relies on modifying a million-line video game program in less than two weeks.

Chapter 19: Playing with Object-Oriented Programming 261

Hiding and exposing data in an object

Because objects need to communicate with one another, objects can classify their data and instructions into one of three categories: private, public, and protected.

If an object defines data and instructions as private, only that particular object can use that data and instructions. No other object can ever use private data and instructions. (That’s why you call them private.)

On the other hand, other objects can use public data and instructions. Objects use public data

and instructions to communicate and share data with other objects.

Protected data and instructions work the same as private data and instructions, with one important exception: If you use inheritance to copy an existing object and create a new object, the new object inherits only public and protected data and instructions. Any private data and instructions remain with the old object.

But if the video game is written by using object-oriented programming techniques, your job is much easier. All you need to do is find the object that represents the alien. Inside that object is all the subprograms you need to modify the way the alien looks and moves. Change this single object, plug it back into the original program, and you’re done.

How to create an object

The first part to creating an object is to define a class, which is similar to a record. (See Chapter 17 for more information about records.) A class defines data and instructions to manipulate that data. After you create a class, you can create one or more objects based on that class to use in your program.

Liberty BASIC doesn’t offer commands for creating objects, so the following code examples use the C++ programming language. Don’t worry about trying to understand the syntax of C++; just browse the code to get a general idea of how the whole thing works.

The following example shows how to create a class in C++:

class monster

{

public:

int x_coordinate;

int y_coordinate; void moveme(int, int);

void initialize(int, int); };

262 Part IV: Dealing with Data Structures

This simple C++ code tells the computer to perform the following tasks:

1.The first line tells the computer, “This line starts a new class, monster.”

2.The second line tells the computer, “This line is the start of the class definition.”

3.The third line tells the computer, “Any things that appear on the following lines are public, which means that any part of the program can access them.”

4.The fourth line creates the integer variable x_coordinate.

5.The fifth line creates the integer variable y_coordinate.

6.The sixth line tells the computer, “The object contains a subprogram (or method), moveme, that accepts two integer values.”

7.The seventh line tells the computer, “The object contains a subprogram, initialize, that accepts two integer values.”

8.The eighth line defines the end of the class definition.

A class isn’t an object. To create an object, you must define a variable that represents your defined class.

Writing an object’s methods

After you declare the subprograms (or methods) that you want to store in a class, you still need to write the actual instructions that make that subprogram. Suppose you want to use the class definition defined in the preceding section in the following example:

class monster

{

public:

int x_coordinate;

int y_coordinate; void moveme(int, int);

void initialize(int, int); };

This class defines two subprograms (or methods), moveme and initialize. To make these methods actually do something, you must write the complete methods directly below the class definition, as follows: