Previous Table of Contents Next


The Development Cycle

If every program worked the first time you tried it, that would be the complete development cycle: write the program, compile the source code, link the program, and run it. Unfortunately, almost every program, no matter how trivial, can and will have errors, or bugs, in it. Some bugs will cause the compile to fail, some will cause the link to fail, and some will only show up when you run the program.

Whatever the type of bug you find, you must fix it, and that involves editing your source code, recompiling and relinking, and then rerunning the program. This cycle is represented in Figure 1.1, which diagrams the steps in the development cycle.

HELLO.CPP—Your First C++ Program

Traditional programming books begin by writing the words Hello World to the screen, or a variation on that statement. This time-honored tradition is carried on here.

Type the first program directly into your editor, exactly as shown. After you are certain it is correct, save the file, compile it, link it, and run it. It will print the words Hello World to your screen. Don’t worry too much about how it works; this is really just to get you comfortable with the development cycle. Every aspect of this program will be covered over the next couple of days.

The following listing contains line numbers on the left. These numbers are for reference within the book. They should not be typed in to your editor. For example, in line 1 of Listing 1.1, you should enter:

#include <iostream.h>


Figure 1.1  The steps in the development of a C++ program.

Listing 1.1 HELLO.CPP, the Hello World Program


1: #include <iostream.h>
2:
3: int main()
4: {
5:    cout << “Hello World!\n”;
6:    return 0;
7: }

Make certain you enter this exactly as shown. Pay careful attention to the punctuation. The << in line 5 is the redirection symbol, produced on most keyboards by holding the shift key and pressing the comma key. Line 5 ends with a semicolon; don’t leave this off!

Also check to make sure you are following your compiler directions properly. Most compilers will link automatically, but check your documentation. If you get errors, look over your code carefully and determine how it is different from Listing 1.1. If you see an error on line 1, such as cannot find file iostream.h, check your compiler documentation for directions on setting up your include path or environment variables. If you receive an error that there is no prototype for main, add the line int main(); just before line three. You will need to add this line to every program in this book before the beginning of the main function. Most compilers don’t require this, but a few do.

Your finished program will look like this:

1: #include <iostream.h>
2:
3: int main();
4: int main()
5: {
6:  cout <<”Hello World!\n”;
7:  return 0;
8:

Try running HELLO.EXE; it should write

Hello World!

directly to your screen. If so, congratulations! You’ve just entered, compiled, and run your first C++ program. It might not look like much, but almost every professional C++ programmer started out with this exact program.

Compile Errors

Compile-time errors can occur for any number of reasons. Usually they are a result of a typo or other inadvertent minor error. Good compilers will not only tell you what you did wrong, they’ll point you to the exact place in your code where you made the mistake. The great ones will even suggest a remedy!

You can see this by intentionally putting an error into your program. If HELLO.CPP ran smoothly, edit it now and remove the closing brace on line 6. Your program will now look like Listing 1.2.

Listing 1.2 Demonstration of Compiler Error


1: #include <iostream.h>
2:
3: int main()
4: {
5:    cout << “Hello World!\n”;
6:    return 0;

Recompile your program and you should see an error that looks similar to

Hello.cpp, line 5: Compound statement missing terminating }
in function main().

This error tells you the file and line number of the problem and what the problem is (although I admit it is somewhat cryptic). Note that the error message points you to line 5. The compiler wasn’t sure if you intended to put the closing brace before or after the cout statement on line 5. Sometimes the errors just get you to the general vicinity of the problem. If a compiler could perfectly identify every problem, it would fix the code itself.

Summary

After reading this chapter, you should have a good understanding of how C++ evolved and what problems it was designed to solve. You should feel confident that learning C++ is the right choice for anyone interested in programming in the next decade. C++ provides the tools of object-oriented programming and the performance of a systems level language, which makes C++ the development language of choice.

In this very first chapter, you’ve learned how to write, compile, link, and run your first C++ program. You’ve learned what the normal development cycle is. You also learned a little of what object-oriented programming is all about. You will return to these topics during the remaining hours.

Q&A

Q What is the difference between a text editor and a word processor?
A A text editor produces files with plain text in them. There are no formatting commands or other special symbols required by a particular word processor. Text files do not have automatic word wrap, bold print, italics, and so forth. Word processors usually can produce text files, but you must be careful to save your file as plain text.
Q My compiler has a built-in editor; is that the right thing to use?
A Almost all compilers will compile code produced by any text editor. The advantages of using the built-in text editor, however, might include the capability to quickly move back and forth between the edit and compile steps of the development cycle. Sophisticated compilers include a fully integrated development environment, enabling the programmer to access help files, edit, and compile the code in place, and to resolve compile and link errors without ever leaving the environment.
Q Can I ignore warning messages from my compiler?
A Absolutely not. Get into the habit of treating warning messages as errors. C++ uses the compiler to warn you when you are doing something you might not intend; heed those warnings and do what is required to make them go away.
Q What is compile-time?
A Compile-time is the time when you run your compiler, as opposed to link-time (when you run the linker) or run-time (when you run the program).


Previous Table of Contents Next