📄 ch02.htm
字号:
<UL> <LI>When including library files, the file to be included is surrounded by angled brackets, as shown in the <TT>Hello2.cpp</TT> file shown earlier. The preprocessor searches a predefined path for the file.<BR> <BR> <LI>When including header files that are specific to a specific application, the filename is surrounded by quotes, such as <TT>#include "stdafx.h"</TT>. The preprocessor will search for the file in the current source file directory. If the file is not found, the search will continue along the predefined include path.</UL><P>The second line of <TT>Hello2.cpp</TT> is also an <TT>#include</TT> directive:</P><PRE><FONT COLOR="#0066FF"><TT>#include <string></TT></FONT></PRE><P>The string header file is part of the standard C++ library. Including the stringheader file enables a C++ source file to use the standard string class, which simplifiesusing text strings in a C++ application.<H4><FONT COLOR="#000077">The std Namespace</FONT></H4><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A collection of names andother identifiers in C++ is known as a <I>namespace</I>. By default, any name thatis introduced in a C++ program is in the <I>global namespace</I>. All names foundin the standard C++ library are located in the <TT>std</TT> namespace.</P><P>Namespaces make it easier to manage names in large C++ projects, especially whenusing libraries or code developed by different groups of people. Before namespaceswere introduced to C++, it wasn't unusual to have two or more libraries that wereincompatible with each other simply because they used conflicting names.</P><P>Namespaces allow libraries to place their names into a compartment that itselfhas a name. As shown in Figure 2.3, two namespaces can each use a common name, inthis case <TT>string</TT>; because each namespace provides a compartment for thename <TT>string</TT>, the two names do not conflict with each other.</P><P><A NAME="03"></A><A HREF="03.htm"><B>Figure 2.3.</B></A> <BR><I>Namespaces provide separate compartments for names used in a C++ program.</I></P><P>When using a name from a namespace, the namespace must be prefixed, like <TT>std::string</TT>or <TT>codev::string</TT>. Alternatively, a <TT>using namespace</TT> directive canbe used to tell the compiler that an identifier can be found in the global namespace,as in the next line of the program, which tells the compiler that the names foundin the program can be found in the <TT>std</TT> namespace:</P><PRE><FONT COLOR="#0066FF"><TT>using namespace std;</TT></FONT></PRE><H4><FONT COLOR="#000077">Using Comments to Document Your Code</FONT></H4><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>comment</I> is a noteprovided to the person reading the source code. It has no meaning to the compileror computer.</P><P>The next line begins with <TT>//</TT>, which is used to mark the beginning ofa single-line comment in a C++ program. By default, comments are colored green bythe Developer Studio editor. In contrast, <TT>int</TT> and <TT>return</TT> are coloredblue to indicate that they are C++ keywords.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>It's a good idea to use comments to document your code. After time has passed, you can use your comments to help explain how your code was intended to work. <HR></BLOCKQUOTE><H4><FONT COLOR="#000077">The main Function</FONT></H4><P>The next line of <TT>Hello2.cpp</TT> is the beginning of the <TT>main</TT> function.</P><PRE><FONT COLOR="#0066FF"><TT>int main()</TT></FONT></PRE><P>The first line inside the <TT>main</TT> function is a variable declaration.</P><PRE><FONT COLOR="#0066FF"><TT>string userName;</TT></FONT></PRE><P>Don't worry too much about what this means--for now, it's enough to know that<TT>userName</TT> is a <TT>string</TT> variable. A <TT>string</TT> is not one ofthe fundamental data types; instead, it's part of the standard library. The <TT>string</TT>type enables you to use strings of text as though they are built-in fundamental types.</P><P>Following the declaration of <TT>userName</TT> is a statement that displays amessage to the user as a prompt for the user's name:</P><PRE><FONT COLOR="#0066FF"><TT>cout << "What is your name? :";</TT></FONT></PRE><P>This particular statement in <TT>Hello2.cpp</TT> displays a line of charactersto the console window by using the <TT>iostream</TT> object <TT>cout</TT>. The <TT>iostream</TT>library is included with every C++ compiler, although it is not technically partof the C++ language definition; instead, it's part of the standard C++ library. Performingstandard input and output for your console mode program is easy using the <TT>iostream</TT>library.</P><P>The <TT>iostream</TT> library uses the <TT><<</TT> symbol for output andthe <TT>>></TT> for input to and from IO streams. Think of a stream as a sequenceof bytes, like a disk file, or the output to a printer or a character-mode screen.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>One simple rule of thumb is that when you see the <TT><<</TT> symbol, the value to the right of the symbol will be output to the IO object on the left. When you see the <TT>>></TT> symbol, data from the IO object on the left is stored in a variable to the right. <HR></BLOCKQUOTE><P>The next line of <TT>Hello2.cpp</TT> accepts input from the user and stores itin <TT>userName</TT>:</P><PRE><FONT COLOR="#0066FF"><TT>cin >> userName;</TT></FONT></PRE><P>The variable <TT>userName</TT> now contains whatever value was entered by theuser.</P><P>The next line displays the Hello greeting and adds the contents of the <TT>userName</TT>variable. When using <TT>cout</TT>, several different components can be output oneafter another by separating them with the <TT><<</TT> symbol:</P><PRE><FONT COLOR="#0066FF"><TT>cout << "Hello " << userName << "!" << endl;</TT></FONT></PRE><P>The last line of the <TT>main</TT> function is a <TT>return</TT> statement. Whena <TT>return</TT> statement is executed, the function <I>returns</I> or stops executing,and the caller of the function is passed the value provided after the <TT>return</TT>keyword. Because this return statement is inside <TT>main</TT>, the value <TT>0</TT>is passed back to the operating system. The <TT>return</TT> keyword can appear almostanywhere in a function. However, as a matter of style, most people prefer to havea single <TT>return</TT> statement in a function if possible.<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2><P>In this hour, you have learned more details about C++ programs. You wrote a simpleconsole-mode program and analyzed its parts. You also learned about the C++ preprocessor,type-safety, and variables.<H2><FONT COLOR="#000077"><B>Q&A</B></FONT></H2><DL> <DD><B>Q When I compile the Hello2 project and enter my first and last name, only the first name is displayed. How can I display my first and last names?</B><BR> <BR> <B>A</B> When using <TT>cin</TT> to gather input as shown in the Hello2 project, white space such as the space between your first and last name will cause your names be parsed into two separate variables. You can use <TT>cin</TT> with multiple variables much like you use <TT>cout</TT> with multiple variables; just separate the variables with the <TT>>></TT> operator. A new version of Hello2 that displays first and last names looks like this:</DL><BLOCKQUOTE> <PRE><FONT COLOR="#0066FF"><TT>#include <iostream></TT><TT>#include <string></TT><TT>using namespace std;</TT><TT>int main()</TT><TT>{</TT><TT> string strFirstName;</TT><TT> string strLastName;</TT><TT> cout << "Please enter your first and last name:";</TT><TT> cin >> strFirstName >> strLastName;</TT><TT> cout << "Hello " << strFirstName << strLastName << endl;</TT><TT> return 0;</TT><TT>}</TT></FONT></PRE></BLOCKQUOTE><PRE><FONT COLOR="#0066FF"><TT></TT></FONT></PRE><DL> <DD><B>Q When I declare a variable, sometimes I get strange error messages from the compiler in the Build window. This is the line that causes the error:</B></DL><BLOCKQUOTE> <PRE><FONT COLOR="#0066FF"><TT>int my age;</TT></FONT></PRE></BLOCKQUOTE><PRE><FONT COLOR="#0066FF"><TT></TT></FONT></PRE><DL> <DD><B>A</B> In C++, all variables must be a single identifier. The compiler complains because after using the identifier as a variable name, it can't figure out what to do with the identifier name. One coding style is to separate the words that make up a variable name with an underscore, like this:</DL><BLOCKQUOTE> <PRE><FONT COLOR="#0066FF"><TT>int my_age;</TT></FONT></PRE></BLOCKQUOTE><PRE><FONT COLOR="#0066FF"><TT></TT></FONT></PRE><H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2><P>The Workshop is designed to help you anticipate possible questions, review whatyou've learned, and begin thinking ahead to putting your knowledge into practice.The answers to the quiz are in Appendix B, "Quiz Answers."<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3><DL> <DD>1. What is the difference between the <TT>cout</TT> and <TT>cin</TT> <TT>iostream</TT> objects?<BR> <BR> 2. What are the two forms of the <TT>#include</TT> preprocessor directive?<BR> <BR> 3. What type of variable is used to store character values?<BR> <BR> 4. What is the purpose of a C++ namespace?<BR> <BR> 5. How can you declare more than one variable on a single line?<BR> <BR> 6. What is type-safety?<BR> <BR> 7. What types of variable are used to store floating-point values?<BR> <BR> 8. How do you assign a value to a variable?<BR> <BR> 9. What type of variable is normally used to store integer values?<BR> <BR> 10. Why would you declare a variable as unsigned?</DL><H3><FONT COLOR="#000077"><B>Exercise</B></FONT></H3><DL> <DD>1. Modify the Hello2 program to ask for your age in addition to your name; display the name and age in the Hello message.</DL><CENTER><P><HR><A HREF="../ch01/ch01.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch03/ch03.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR><BR><BR><IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"BORDER="0"></P><P>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -