?? ch01.htm
字號:
Cycle</FONT></H3>
<P>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 the program. 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.</P>
<P>Whatever 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.<BR>
<BR>
<A NAME="Heading18"></A><A HREF="javascript:if(confirm('http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch01/01zcp01.jpg \n\nThis file was not retrieved by Teleport Pro, because the server reports that this file cannot be found. \n\nDo you want to open it from the server?'))window.location='http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch01/01zcp01.jpg'" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch01/01zcp01.jpg"><FONT COLOR="#000077">Figure
1.1.</FONT></A><FONT COLOR="#000077"> </FONT><I>The steps in the development of a
C++ program.</I>
<H3 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">HELLO.CPPYour First
C++ Program</FONT></H3>
<P>Traditional programming books begin by writing the words <TT>Hello World</TT>
to the screen, or a variation on that statement. This time-honored tradition is carried
on here.</P>
<P>Type the first program directly into your editor, exactly as shown. Once you are
certain it is correct, save the file, compile it, link it, and run it. It will print
the words <TT>Hello World</TT> 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.
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>WARNING: </B></FONT>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:</P>
<PRE><FONT COLOR="#0066FF">#include <iostream.h></FONT></PRE>
<P>
<HR>
</BLOCKQUOTE>
<PRE></PRE>
<P><A NAME="Heading20"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 1.1. HELLO.CPP,
the Hello World program.</B></FONT><FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">1: #include <iostream.h>
2:
3: int main()
4: {
5: cout << "Hello World!\n";
6: return 0;
<TT>7: }</TT></FONT>
</PRE>
<P>Make certain you enter this exactly as shown. Pay careful attention to the punctuation.
The <TT><<</TT> in line 5 is the redirection symbol, produced on most keyboards
by holding the Shift key and pressing the comma key twice. Line 5 ends with a semicolon;
don't leave this off!</P>
<P>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 the above. If
you see an error on line 1, such as <TT>cannot find file iostream.h</TT>, check your
compiler documentation for directions on setting up your <TT>include</TT> path or
environment variables. If you receive an error that there is no prototype for <TT>main</TT>,
add the line <TT>int main();</TT> just before line 3. You will need to add this line
before the beginning of the <TT>main</TT> function in every program in this book.
Most compilers don't require this, but a few do.</P>
<P>Your finished program will look like this:</P>
<PRE><FONT COLOR="#0066FF">1: #include <iostream.h>
2:
3:
4: int main();
5: {
6: cout <<"Hello World!\n";
7: return 0;
8: }
</FONT></PRE>
<P>Try running <TT>HELLO.EXE</TT>; it should write</P>
<PRE><FONT COLOR="#0066FF">Hello World!
</FONT></PRE>
<P>directly to your screen. If so, congratulations! You've just entered, compiled,
and run your first C++ program. It may not look like much, but almost every professional
C++ programmer started out with this exact program.
<H3 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">Compile Errors</FONT></H3>
<P>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!</P>
<P>You can see this by intentionally putting an error into your program. If <TT>HELLO.CPP</TT>
ran smoothly, edit it now and remove the closing brace on line 6. Your program will
now look like Listing 1.2.</P>
<P><A NAME="Heading22"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 1.2. Demonstration
of compiler error.</B></FONT>
<PRE><FONT COLOR="#0066FF">1: #include <iostream.h>
2:
3: int main()
4: {
5: cout << "Hello World!\n";
<TT>6: return 0;</TT></FONT> </PRE>
<P><BR>
Recompile your program and you should see an error that looks similar to the following:</P>
<PRE><FONT COLOR="#0066FF">Hello.cpp, line 5: Compound statement missing terminating } in function main().
</FONT></PRE>
<P>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 <TT>cout</TT> 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.
<H3 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>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.</P>
<P>Today you learned how to enter, compile, link, and run your first C++ program,
and 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 next three weeks.
<H3 ALIGN="CENTER"><A NAME="Heading25"></A><FONT COLOR="#000077">Q&A</FONT></H3>
<DL>
<DD><B>Q. What is the difference between a text editor and a word processor?</B><BR>
<BR>
<B>A.</B> 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.<BR>
<BR>
<B>Q. If my compiler has a built-in editor, must I use it?</B><BR>
<BR>
<B>A.</B> Almost all compilers will compile code produced by any text editor. The
advantages of using the built-in text editor, however, might include the ability
to quickly move back and forth between the edit and compile steps of the development
cycle. Sophisticated compilers include a fully integrated development environment,
allowing 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.<BR>
<BR>
<B>Q. Can I ignore warning messages from my compiler?</B><BR>
<BR>
<B>A.</B> Many books hedge on this one, but I'll stake myself to this position: No!
Get into the habit, from day one, of treating warning messages as errors. C++ uses
the compiler to warn you when you are doing something you may not intend. Heed those
warnings, and do what is required to make them go away.<BR>
<BR>
<B>Q. What is compile time?</B><BR>
<BR>
<B>A.</B> Compile time is the time when you run your compiler, as opposed to link
time (when you run the linker) or run-time (when running the program). This is just
programmer shorthand to identify the three times when errors usually surface.
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading26"></A><FONT COLOR="#000077">Workshop</FONT></H3>
<P>The Workshop provides quiz questions to help you solidify your understanding of
the material covered and exercises to provide you with experience in using what you've
learned. Try to answer the quiz and exercise questions before checking the answers
in Appendix D, and make sure you understand the answers before continuing to the
next chapter.
<H4 ALIGN="CENTER"><A NAME="Heading27"></A><FONT COLOR="#000077">Quiz</FONT></H4>
<DL>
<DD><B>1.</B> What is the difference between an interpreter and a compiler?<BR>
<BR>
<B>2.</B> How do you compile the source code with your compiler?<BR>
<BR>
<B>3.</B> What does the linker do?<BR>
<BR>
<B>4.</B> What are the steps in the normal development cycle?
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading28"></A><FONT COLOR="#000077">Exercises</FONT></H4>
<DL>
<DD><B>1.</B> Look at the following program and try to guess what it does without
running it.
</DL>
<PRE><FONT COLOR="#0066FF">1: #include <iostream.h>
2: int main()
3: {
4: int x = 5;
5: int y = 7;
6: cout "\n";
7: cout << x + y << " " << x * y;
8: cout "\n";
9:return 0;
10: }
</FONT></PRE>
<DL>
<DD><B>2.</B> Type in the program from Exercise 1, and then compile and link it.
What does it do? Does it do what you guessed?<BR>
<BR>
<B>3.</B> Type in the following program and compile it. What error do you receive?
</DL>
<PRE><FONT COLOR="#0066FF">1: include <iostream.h>
2: int main()
3: {
4: cout << "Hello World\n";
5: return 0;
6: }
</FONT></PRE>
<DL>
<DD><B>4.</B> Fix the error in the program in Exercise 3, and recompile, link, and
run it. What does it do?
<CENTER>
<DD><BR>
<BR>
<BR>
<A HREF="fm.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/fm.htm"><IMG SRC="../buttons/BLANPREV.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANPREV.GIF" WIDTH="37"
HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="javascript:if(confirm('http://www.mcp.com/sams \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mcp.com/sams'" tppabs="http://www.mcp.com/sams"><IMG
SRC="../buttons/BLANHOME.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
BORDER="0"></A><A HREF="../index.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/index.htm"><IMG SRC="../buttons/BLANTOC.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANTOC.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch02.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch02.htm"><IMG SRC="../buttons/BLANNEXT.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANNEXT.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="#heading1"><IMG SRC="../buttons/BLANTOP.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANTOP.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A></CENTER>
</DL>
</BODY>
</HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -