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

Chapter 19: Playing with Object-Oriented Programming 263

void monster::moveme(int new_x, int new_y)

{

x_coordinate = x_coordinate + new_x; y_coordinate = y_coordinate + new_y;

}

void monster::initialize(int init_x, int init_y)

{

x_coordinate = init_x; y_coordinate = init_y;

}

The initialize method defines the X coordinate and Y coordinate of any object that you derive from the class monster. You use the moveme method to move an object’s position a certain distance in the X (right and left) direction and Y (up and down) direction.

Creating an object

After you define a class and write any methods that you declare within that class, you still need to define a variable to represent that class. This variable represents the actual object in object-oriented programming.

To create an object, you declare a variable to represent that object. The following program defines an object named zombie to represent the monster class definition:

#include <iostream.h> class monster

{

public:

int x_coordinate; int y_coordinate;

void moveme(int, int); void initialize(int, int); };

void monster::moveme(int new_x, int new_y)

{

x_coordinate = x_coordinate + new_x; y_coordinate = y_coordinate + new_y;

}

void monster::initialize(int init_x, int init_y)

{

x_coordinate = init_x; y_coordinate = init_y;

}

void main()

{

264 Part IV: Dealing with Data Structures

monster zombie; zombie.initialize(12, 15);

cout << “The X-location of the zombie is “ << zombie.x_coordinate << “\n”;

cout << “The Y-location of the zombie is “ << zombie.y_coordinate << “\n”;

zombie.moveme (34, 9);

cout << “The new X-location of the zombie is “ << zombie.x_coordinate << “\n”;

cout << “The new Y-location of the zombie is “ << zombie.y_coordinate << “\n”;

}

The main C++ program starts with the void main() line. Just examining this portion, line by line, makes the computer do the following:

1.The first line tells the computer, “This line is the start of the C++ program.”

2.The second line tells the computer, “This line is the start of all instructions inside the main C++ program.”

3.The third line tells the computer, “Create the object zombie and base it on the class definition monster.”

4.The fourth line runs the method initialize, which defines the X coordinate (12) and Y coordinate (15) of the zombie object.

5.The fifth line prints the message, The X-location of the zombie is 12.

6.The sixth line prints the message, The Y-location of the zombie is 15.

7.The seventh line runs the moveme method, which moves the zombie object 34 units of measurement in the X direction and 9 units in the Y direction.

8.The eighth line prints the message, The new X-location of the zombie is 46. (That’s 12, the original X-location of the zombie as Step 4 defines, plus 34 as Step 7 defines, which equals 46.)

9.The ninth line prints the message, The new Y-location of the zombie is 24. (That’s 15, the original Y-location of the zombie as Step 4 defines, plus 9 as Step 7 defines, which equals 24.)

10.The tenth line tells the computer, “This line represents the end of the C++ main program.”

Although the C++ program may look confusing, just remember these important points and you’re fine:

To create an object, you must define a class first.

A class contains data and instructions (methods) for manipulating the object’s data.

Chapter 19: Playing with Object-Oriented Programming 265

Common terms associated with object-oriented programming

Although you aren’t likely to become an expert in object-oriented programming from this brief introduction, you can at least understand the purpose behind objects, which is to make modifying and reusing programs easier.

If you plan to continue studying computer programming, you’re likely to run into object-ori- ented programming over and over (or at least until a new programming methodology pops up to make programming easier). To add to your limited knowledge of object-oriented programming, remember the following common objectoriented terms that you’re likely to see at one time or another:

Encapsulation means lumping all related data and instructions to manipulate that data in a single location.

Inheritance means passing data and instructions from one object to a second object that derives from the first object.

Message is a subprogram (also known as a method ) that manipulates the data inside an object.

Object is a collection of data and instructions for manipulating that data, grouped together in a self-contained unit.

Choosing an Object-Oriented Language

After you understand the general principles behind object-oriented programming, you may be eager to start trying it out on your own. Liberty BASIC doesn’t support object-oriented programming, so you need to switch to a different programming language. The two types of programming languages that you can choose from are as follows:

Hybrid object-oriented languages

True (or pure) object-oriented languages

A hybrid object-oriented language simply takes an existing language and slaps object-oriented features on top of it. Some of the more popular hybrid languages include Pascal (as implemented in Delphi), BASIC (as implemented in Visual Basic), and C++.

The main advantage of using a hybrid language is that if you already know how to use a language such as Pascal, BASIC, or C++, you can quickly learn to use the object-oriented features of these languages with a minimum of training, anguish, and experimentation. If you’re not quite sure about the benefits

266 Part IV: Dealing with Data Structures

of object-oriented programming, you can write a small part of your program using objects and write the bulk of your program using old-fashioned programming methods.

Of course, the main disadvantage of hybrid languages is that they enable programmers to mix traditional and object-oriented programming techniques, which can become an untidy mess. Hybrid languages enable programmers to use none, some, or all object-oriented programming techniques, so using hybrid languages often creates programs that don’t take full advantage of objects, destroying the advantage of using objects while making the program harder to read and understand.

That’s why many people prefer pure object-oriented languages that force you to use objects right from the start. Some popular pure object-oriented languages include the following:

SmallTalk

Eiffel

C#

Java

Whether you decide to stick to a conventional language (and use its objectoriented hybrid) or jump straight into a pure object-oriented language, get used to the idea behind breaking your program into objects. Object-oriented programming techniques alone don’t make software easier to write and more reliable, but they can make you more aware of the problems in writing software and how object-oriented programming can solve those problems.

In the long run, nobody really cares what language you use, whether you use any object-oriented programming techniques, or whether you write software while sitting in your underwear and listening to Barry Manilow albums at

3 a.m. The important point is to write software on time that works. If you can do that, you can focus on producing results and leave trivial details, such as wearing a tie, dealing with corporate politics, and fighting each other for a cubicle near a window, for your co-workers to worry about.

Part V

Algorithms: Telling

the Computer

What to Do

In this part . . .

Aprogram is nothing more than a list of instructions, but you can create instructions in various ways to

tell the computer how to perform the same task. If you want to give directions to tell someone how to get from the airport to your house, for example, you probably can tell that person two or three different ways. Each way eventually gets the person to your house, but one way may prove easier, another way may prove faster during the day, and the third way may prove more scenic.

In the world of computer programming, a specific way to accomplish a task is known as an algorithm. By choosing the fastest set of instructions (the algorithm), you can make your program faster and more efficient. This part of the book introduces you to several common algorithms for accomplishing different tasks so that you can understand the foundation on which you build most programs.

Chapter 20

Sorting

In This Chapter

Sorting data in many ways

Picking a sorting algorithm

Programs typically accept data from the outside world (such as from someone typing on the keyboard), manipulate that data somehow, and

spit that data back out in a format that someone finds useful.

A database is fairly useless if it enables you to store information without enabling you to do anything to rearrange that information. You may, for example, want to rearrange your data alphabetically by last name, numerically by telephone area code, or by some other criterion, such as by those people who’re single and earn $75,000 or more every year. Your program needs to know how to sort data.

Although sorting may seem a fairly mundane topic, it can actually get rather complex. That’s because whenever a program sorts data, it needs to sort the information as quickly as possible. After all, a program that sorts names and addresses is useless if it takes three hours just to sort 15 names.

So part of computer science centers on studying and developing the most efficient sorting methods (known as sorting algorithms) possible. Because many types of programs need to sort data, nearly every programmer needs to know the different sorting algorithms available and how they work. Throughout this chapter, you can type various Liberty BASIC programs and see for yourself exactly how a particular sorting algorithm does its sorting magic.

Computer scientists have created a variety of sorting algorithms — but no single, perfect algorithm that your program should use all the time. The most efficient algorithm depends partly on the data that you want to sort and partly on the data structures that your program uses to store data.