- •About the Author
- •Dedication
- •Author’s Acknowledgments
- •Contents at a Glance
- •Table of Contents
- •Introduction
- •Who Should Buy This Book
- •How This Book Is Organized
- •Part I: Programming a Computer
- •Part II: Learning Programming with Liberty BASIC
- •Part III: Advanced Programming with Liberty BASIC
- •Part VI: Internet Programming
- •Part VII: The Part of Tens
- •How to Use This Book
- •Foolish assumptions
- •Icons used in this book
- •Why Learn Computer Programming?
- •How Does a Computer Program Work?
- •What Do I Need to Know to Program a Computer?
- •The joy of assembly language
- •C: The portable assembler
- •High-level programming languages
- •Database programming languages
- •Scripting programming languages
- •The program’s users
- •The target computer
- •Prototyping
- •Choosing a programming language
- •Defining how the program should work
- •The Life Cycle of a Typical Program
- •The development cycle
- •The maintenance cycle
- •The upgrade cycle
- •Writing Programs in an Editor
- •Using a Compiler or an Interpreter
- •Compilers
- •Interpreters
- •P-code: A combination compiler and interpreter
- •So what do I use?
- •Squashing Bugs with a Debugger
- •Writing a Help File
- •Creating an Installation Program
- •Why Learn Liberty BASIC?
- •Liberty BASIC is easy
- •Liberty BASIC runs on Windows
- •You can start using Liberty BASIC today
- •Installing Liberty BASIC
- •Loading Liberty BASIC
- •Your First Liberty BASIC Program
- •Running a Liberty BASIC program
- •Saving a Liberty BASIC program
- •Getting Help Using Liberty BASIC
- •Exiting Liberty BASIC
- •Getting input
- •Displaying output
- •Sending Data to the Printer
- •Storing Data in Variables
- •Creating a variable
- •Assigning a value to a variable
- •Declaring your variables
- •Using Constants
- •Commenting Your Code
- •Using variables
- •Working with precedence
- •Using parentheses
- •Manipulating Strings
- •Declaring variables as strings
- •Smashing strings together
- •Counting the length of a string
- •Playing with UPPERCASE and lowercase
- •Trimming the front and back of a string
- •Inserting spaces
- •Yanking characters out of a string
- •Looking for a string inside another string
- •Using Boolean Expressions
- •Using variables in Boolean expressions
- •Using Boolean operators
- •Exploring IF THEN Statements
- •IF THEN ELSE statements
- •Working with SELECT CASE Statements
- •Checking a range of values
- •Checking a relational operator
- •Boolean expression inside the loop
- •Looping a Fixed Number of Times
- •Counting with different numbers
- •Counting in increments
- •Anatomy of a Computer Bug
- •Syntax Errors
- •Fun with Logic Errors
- •Stepping line by line
- •Tracing through your program
- •Designing a Window
- •Creating a new window
- •Defining the size and location of a window
- •Adding color to a window
- •Putting Controls in a Window
- •Creating a command button
- •Displaying text
- •Creating a check box
- •Creating a radio button
- •Creating text boxes
- •Creating list boxes
- •Creating combo boxes
- •Creating group boxes
- •Storing Stuff in Text Files
- •Creating a new text file
- •Putting stuff in a text file
- •Adding new stuff to an existing text file
- •Retrieving data from a text file
- •Creating a new binary file
- •Saving stuff in a binary file
- •Changing stuff in a binary file
- •Retrieving stuff from a binary file
- •Creating a Graphics Control
- •Using Turtle Graphics
- •Defining line thickness
- •Defining line colors
- •Drawing Circles
- •Drawing Boxes
- •Displaying Text
- •Making Sounds
- •Making a beeping noise
- •Playing WAV files
- •Passing Data by Value or by Reference
- •Using Functions
- •Defining a function
- •Passing data to a function
- •Calling a function
- •Exiting prematurely from a function
- •Using Subroutines
- •Defining a subroutine
- •Passing data to a subroutine
- •Calling a subroutine
- •Exiting prematurely from a subroutine
- •Writing Modular Programs
- •Introducing Structured Programming
- •Sequential instructions
- •Branching instructions
- •Looping instructions
- •Putting structured programming into practice
- •The Problem with Software
- •Ways to Make Programming Easier
- •Breaking Programs into Objects
- •How to use objects
- •How to create an object
- •Creating an object
- •Starting with a Pointer
- •Defining the parts of a linked list
- •Creating a linked list
- •Managing a linked list
- •Making Data Structures with Linked Lists
- •Stacks
- •Queues
- •Trees
- •Graphs
- •Creating a Record
- •Manipulating Data in Records
- •Storing data in a record
- •Retrieving data from a record
- •Using Records with Arrays
- •Making an Array
- •Making a Multidimensional Array
- •Creating Dynamic Arrays
- •Insertion Sort
- •Bubble Sort
- •Shell Sort
- •Quicksort
- •Sorting Algorithms
- •Searching Sequentially
- •Performing a Binary Search
- •Hashing
- •Searching by using a hash function
- •Dealing with collisions
- •Picking a Searching Algorithm
- •Choosing the Right Data Structure
- •Choosing the Right Algorithm
- •Put the condition most likely to be false first
- •Put the condition most likely to be true first
- •Clean out your loops
- •Use the correct data types
- •Using a Faster Language
- •Optimizing Your Compiler
- •Programming Computer Games
- •Creating Computer Animation
- •Making (And Breaking) Encryption
- •Internet Programming
- •Fighting Computer Viruses and Worms
- •Hacking for Hire
- •Participating in an Open-Source Project
- •Niche-Market Programming
- •Teaching Others about Computers
- •Selling Your Own Software
- •Trying Commercial Compilers
- •Windows programming
- •Macintosh and Palm OS programming
- •Linux programming
- •Testing the Shareware and
- •BASIC compilers
- •C/C++ and Java compilers
- •Pascal compilers
- •Using a Proprietary Language
- •HyperCard
- •Revolution
- •PowerBuilder
- •Shopping by Mail Order
- •Getting Your Hands on Source Code
- •Joining a Local User Group
- •Frequenting Usenet Newsgroups
- •Playing Core War
- •Programming a Battling Robot
- •Toying with Lego Mindstorms
- •Index
- •End-User License Agreement
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: