⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter02.html

📁 《C++编程思想》中文版。。。。。。。。。。。。。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<B>&lt;iostream&gt; </B>header file.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you are using an add-on library, you
must explicitly add the library name to the list of files handed to the
linker.</FONT><BR></P></DIV>
<A NAME="Heading83"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Using plain C libraries<A NAME="Index331"></A> </H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Just because you are writing code in C++,
you are not prevented from using C library functions. In fact, the entire C
library is included by default into Standard C++. There has been a tremendous
amount of work done for you in these functions, so they can save you a lot of
time.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This book will use Standard C++ (and thus
also Standard C) library functions when convenient, but only <I>standard</I>
library functions will be used, to ensure the portability of programs. In the
few cases in which library functions must be used that are not in the C++
standard, all attempts will be made to use POSIX-compliant functions. POSIX is a
standard based on a Unix standardization effort that includes functions that go
beyond the scope of the C++ library. You can generally expect to find POSIX
functions on Unix (in particular, Linux) platforms, and often under DOS/Windows.
For example, if you&#8217;re using multithreading you are better off using the
POSIX thread library because your code will then be easier to understand, port
and maintain (and the POSIX thread library will usually just use the underlying
thread facilities of the operating system, if these are
provided).</FONT><A NAME="_Toc462979721"></A><A NAME="_Toc472654725"></A><BR></P></DIV>
<A NAME="Heading84"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Your first C++ program<A NAME="Index332"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You now know almost enough of the basics
to create and compile a program. The program will use the Standard C++ iostream
classes. These read from and write to files and &#8220;standard&#8221; input and
output (which normally comes from and goes to the console, but may be redirected
to files or devices). In this simple program, a stream object will be used to
print a message on the
screen.</FONT><A NAME="_Toc462979722"></A><A NAME="_Toc472654726"></A><BR></P></DIV>
<A NAME="Heading85"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Using the iostreams class <A NAME="Index333"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To declare the functions and external
data in the iostreams class, include the header file with the
statement</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include &lt;iostream&gt;</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The first program uses the concept of
<A NAME="Index334"></A><A NAME="Index335"></A>standard output, which means
&#8220;a general-purpose place to send output.&#8221; You will see other
examples using standard output in different ways, but here it will just go to
the console. The iostream package automatically defines a variable (an object)
called <A NAME="Index336"></A><B>cout</B> that accepts all data bound for
standard output.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To send data to standard output, you use
the operator <B>&lt;&lt;</B>. C programmers know this operator as the
&#8220;bitwise left shift,&#8221; which will be described in the next chapter.
Suffice it to say that a bitwise left shift has nothing to do with output.
However, C++ allows operators to be <I>overloaded</I>. When you overload an
operator<A NAME="Index337"></A><A NAME="Index338"></A>, you give it a new
meaning when that operator is used with an object of a particular type. With
iostream objects, the operator <B>&lt;&lt;</B> means &#8220;send to.&#8221; For
example:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>cout &lt;&lt; <font color=#004488>"howdy!"</font>;</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">sends the string &#8220;howdy!&#8221; to
the object called <B>cout<A NAME="Index339"></A></B> (which is short for
&#8220;console output&#8221;).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">That&#8217;s enough operator overloading
to get you started. Chapter 12 covers operator overloading in
detail.</FONT><A NAME="_Toc462979723"></A><A NAME="_Toc472654727"></A><BR></P></DIV>
<A NAME="Heading86"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Namespaces<BR><A NAME="Index340"></A><A NAME="Index341"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As mentioned in Chapter 1, one of the
problems encountered in the C language is that you &#8220;run out of
names&#8221; for functions and identifiers when your programs reach a certain
size. Of course, you don&#8217;t really run out of names; it does, however,
become harder to think of new ones after awhile. More importantly, when a
program reaches a certain size it&#8217;s typically broken up into pieces, each
of which is built and maintained by a different person or group. Since C
effectively has a single arena where all the identifier and function names live,
this means that all the developers must be careful not to accidentally use the
same names in situations where they can conflict. This rapidly becomes tedious,
time-wasting, and, ultimately, expensive.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Standard C++ has a mechanism to prevent
this collision: the <B>namespace</B> keyword. Each set of C++ definitions in a
library or program is &#8220;wrapped&#8221; in a namespace, and if some other
definition has an identical name, but is in a different namespace, then there is
no collision.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Namespaces are a convenient and helpful
tool, but their presence means that you must be aware of them before you can
write any programs. If you simply include a header file and use some functions
or objects from that header, you&#8217;ll probably get strange-sounding errors
when you try to compile the program, to the effect that the compiler cannot find
any of the declarations for the items that you just included in the header file!
After you see this message a few times you&#8217;ll become familiar with its
meaning (which is &#8220;You included the header file but all the declarations
are within a namespace and you didn&#8217;t tell the compiler that you wanted to
use the declarations in that namespace&#8221;).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There&#8217;s a keyword that allows you
to say &#8220;I want to use the declarations and/or definitions in this
namespace.&#8221; This keyword, appropriately enough, is
<A NAME="Index342"></A><A NAME="Index343"></A><B>using</B>. All of the Standard
C++ libraries are wrapped in a single namespace, which is
<A NAME="Index344"></A><A NAME="Index345"></A><B>std</B> (for
&#8220;standard&#8221;). As this book uses the standard libraries almost
exclusively, you&#8217;ll see the following
<A NAME="Index346"></A><A NAME="Index347"></A><I>using directive</I> in almost
every program:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This means that you want to expose all
the elements from the namespace called <B>std</B>. After this statement, you
don&#8217;t have to worry that your particular library component is inside a
namespace, since the <B>using</B> directive makes that namespace available
throughout the file where the <B>using</B> directive was
written.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Exposing all the elements from a
namespace after someone has gone to the trouble to hide them may seem a bit
counterproductive, and in fact you should be careful about thoughtlessly doing
this (as you&#8217;ll learn later in the book). However, the <B>using</B>
directive exposes only those names for the current file, so it is not quite as
drastic as it first sounds. (But think twice about doing it in a header file
&#8211; that <I>is</I> reckless.)</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There&#8217;s a relationship between
namespaces and the way header files are included. Before the modern header file
inclusion was standardized (without the trailing &#8216;<B>.h</B>&#8217;, as in
<B>&lt;iostream&gt;</B>), the typical way to include a header file was with the
&#8216;<B>.h</B>&#8217;, such as <B>&lt;iostream.h&gt;</B>. At that time,
namespaces were not part of the language either. So to provide backward
compatibility with existing code, if you say </FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include &lt;iostream.h&gt;</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">it means</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include &lt;iostream&gt;
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">However, in this book the standard
include format will be used (without the &#8216;<B>.h</B>&#8217;) and so the
<B>using</B> directive must be explicit.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For now, that&#8217;s all you need to
know about namespaces, but in Chapter 10 the subject is covered much more
thoroughly.</FONT><A NAME="_Toc462979724"></A><A NAME="_Toc472654728"></A><BR></P></DIV>
<A NAME="Heading87"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Fundamentals of program structure
<A NAME="Index348"></A><A NAME="Index349"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A C or C++ program is a collection of
variables, function definitions, and function calls. When the program starts, it
executes initialization code and calls a special function,
&#8220;<B>main(&#160;)<A NAME="Index350"></A></B>.&#8221; You put the primary
code for the program here.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As mentioned earlier, a function
definition consists of a return type (which must be specified in C++), a
function name, an argument list in parentheses, and the function code contained
in braces. Here is a sample function definition:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> function() {
  <font color=#009900>// Function code here (this is a comment)</font>
} </PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The function above has an empty argument
list and a body that contains only a comment. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There can be many sets of braces within a
function definition, but there must always be at least one set surrounding the
function body. Since <B>main(&#160;)</B> is a function, it must follow these
rules. In C++, <B>main(&#160;) </B>always has return type of
<B>int</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">C and C++ are free form languages. With
few exceptions, the compiler ignores newlines and white space, so it must have
some way to determine the end of a statement. Statements are delimited by
semicolons.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">C comments start with <B>/* </B>and end
with <B>*/</B>. They can include newlines. C++ uses C-style comments and has an
additional type of comment: <B>//</B>. The <B>//</B> starts a comment that
terminates with a newline. It is more convenient than <B>/* */</B> for one-line
comments, and is used extensively in this
book.</FONT><A NAME="_Toc462979725"></A><A NAME="_Toc472654729"></A><BR></P></DIV>
<A NAME="Heading88"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
"Hello, world!"</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">And now, finally, the first
program:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C02:Hello.cpp</font>
<font color=#009900>// Saying Hello with C++</font>
#include &lt;iostream&gt; <font color=#009900>// Stream declarations</font>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;

<font color=#0000ff>int</font> main() {
  cout &lt;&lt; <font color=#004488>"Hello, World! I am "</font>
       &lt;&lt; 8 &lt;&lt; <font color=#004488>" Today!"</font> &lt;&lt; endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>cout</B> object is handed a series
of arguments via the &#8216;<B>&lt;&lt;</B>&#8217; operators. It prints out
these arguments in left-to-right order. The special iostream function
<B>endl</B> outputs the line and a newline. With iostreams, you can string
together a series of arguments like this, which makes the class easy to use.
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In C, text inside double quotes is
traditionally called a &#8220;string<A NAME="Index351"></A>.&#8221; However, the
Standard C++ library has a powerful class called <B>string</B> for manipulating
text, and so I shall use the more precise term <I>character array </I>for text
inside double quotes.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The compiler creates storage for

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -