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

📄 chapter03.html

📁 Thinking in c++ 2nd edition,c++编程思想(第2版)
💻 HTML
📖 第 1 页 / 共 5 页
字号:
  <font color=#009900>// Simultaneous definition &amp; initialization:</font>
  <font color=#0000ff>char</font> pizza = 'A', pop = 'Z';
  <font color=#0000ff>int</font> dongdings = 100, twinkles = 150, 
    heehos = 200;
  <font color=#0000ff>float</font> chocolate = 3.14159;
  <font color=#009900>// Exponential notation:</font>
  <font color=#0000ff>double</font> fudge_ripple = 6e-4; 
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The first part of the program defines
variables of the four basic data types without initializing them. If you
don&#8217;t initialize a variable, the Standard says that its contents are
undefined (usually, this means they contain garbage). The second part of the
program defines and initializes variables at the same time (it&#8217;s always
best, if possible, to provide an initialization value at the point of
definition). Notice the use of exponential notation<A NAME="Index533"></A> in
the constant 6e-4, meaning &#8220;6 times 10 to the minus fourth
power.&#8221;</FONT><A NAME="_Toc312374157"></A><A NAME="_Toc462979755"></A><A NAME="_Toc472654761"></A><BR></P></DIV>
<A NAME="Heading119"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
bool, true, &amp; false</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Before <B>bool </B>became part of
Standard C++, everyone tended to use different techniques in order to produce
Boolean-like behavior.
<A NAME="Index534"></A><A NAME="Index535"></A><A NAME="Index536"></A><A NAME="Index537"></A><A NAME="Index538"></A><A NAME="Index539"></A>These
produced portability problems and could introduce subtle
errors.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Standard C++ <B>bool</B> type can
have two states expressed by the built-in constants <B>true</B> (which converts
to an integral one) and <B>false</B> (which converts to an integral zero). All
three names are keywords. In addition, some language elements have been
adapted<A NAME="Index540"></A>:</FONT><BR></P></DIV>
<DIV ALIGN="CENTER"><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=81 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana"><B>Element</B></FONT><BR></P></DIV>
</TH>
<TH WIDTH=180 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana"><B>Usage with bool</B></FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=81 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>&amp;&amp;   ||  
!</B></FONT><BR></P></DIV>
</TD>
<TD WIDTH=180 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Take bool arguments and produce
<B>bool</B> results.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=81 COLSPAN=1 ROWSPAN=1 VALIGN="MIDDLE">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>&lt;   &gt;   &lt;=  &gt;=  ==  
!=</B></FONT><BR></P></DIV>
</TD>
<TD WIDTH=180 COLSPAN=1 ROWSPAN=1 VALIGN="MIDDLE">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Produce <B>bool</B>
results.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=81 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>if</B>,<B> for</B>,<B>
</B></FONT><BR><FONT FACE="Georgia"><B>while</B>,<B> do</B></FONT><BR></P></DIV>
</TD>
<TD WIDTH=180 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Conditional expressions convert to
<B>bool</B> values.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=81 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>? :</B></FONT><BR></P></DIV>
</TD>
<TD WIDTH=180 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">First operand converts to <B>bool</B>
value.</FONT><BR></P></DIV>
</TD>
</TR>
</TABLE></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Because there&#8217;s a lot of existing
code that uses an <B>int</B> to represent a flag, the compiler will implicitly
convert from an <B>int</B> to a <B>bool</B> (nonzero values will produce <B>true
</B>while zero values produce <B>false</B>).<B> </B>Ideally, the compiler will
give you a warning as a suggestion to correct the situation.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">An idiom that falls under &#8220;poor
programming style&#8221; is the use of <B>++</B> to set a flag to true. This is
still allowed, but <A NAME="Index541"></A><I>deprecated</I>, which means that at
some time in the future it will be made illegal. The problem is that
you&#8217;re making an implicit type conversion from <B>bool</B> to <B>int</B>,
incrementing the value (perhaps beyond the range of the normal <B>bool</B>
values of zero and one), and then implicitly converting it back
again.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Pointers (which will be introduced later
in this chapter) will also be automatically converted to <B>bool</B> when
necessary.</FONT><A NAME="_Toc462979756"></A><A NAME="_Toc472654762"></A><BR></P></DIV>
<A NAME="Heading120"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Specifiers<BR><A NAME="Index542"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Specifiers modify the meanings of the
basic built-in types and expand them to a much larger set. There are four
specifiers: <A NAME="Index543"></A><B>long<A NAME="Index544"></A></B>,
<A NAME="Index545"></A><B>short<A NAME="Index546"></A></B>,
<A NAME="Index547"></A><B>signed</B>,<A NAME="Index548"></A> and
<A NAME="Index549"></A><B>unsigned<A NAME="Index550"></A></B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>long</B> and <B>short</B> modify the
maximum and minimum values that a data type will hold. A plain <B>int</B> must
be at least the size of a <B>short</B>. The size hierarchy for integral types
is: <B>short</B> <B>int</B>, <B>int</B>, <B>long</B> <B>int</B>. All the sizes
could conceivably be the same, as long as they satisfy the minimum/maximum value
requirements. On a machine with a 64-bit word, for instance, all the data types
might be 64 bits.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The size hierarchy for floating point
numbers<A NAME="Index551"></A> is: <A NAME="Index552"></A><B>float</B>,
<A NAME="Index553"></A><B>double</B>, and
<A NAME="Index554"></A><A NAME="Index555"></A><B>long</B> <B>double</B>.
&#8220;<A NAME="Index556"></A>long float&#8221; is not a legal type. There are
no <B>short</B> floating-point numbers.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>signed</B> and <B>unsigned</B>
specifiers tell the compiler how to use the sign bit with integral types and
characters (floating-point numbers always contain a sign). An <B>unsigned</B>
number does not keep track of the sign and thus has an extra bit available, so
it can store positive numbers twice as large as the positive numbers that can be
stored in a <B>signed</B> number. <B>signed</B> is the default and is only
necessary with <A NAME="Index557"></A><A NAME="Index558"></A><B>char</B>;
<B>char</B> may or may not default to <B>signed</B>. By specifying
<A NAME="Index559"></A><A NAME="Index560"></A><B>signed</B> <B>char</B>, you
force the sign bit to be used. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The following example shows the size of
the data types in bytes by using the
<A NAME="Index561"></A><A NAME="Index562"></A><B>sizeof</B> operator, introduced
later in this chapter:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:Specify.cpp</font>
<font color=#009900>// Demonstrates the use of specifiers</font>
#include &lt;iostream&gt;
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;

<font color=#0000ff>int</font> main() {
  <font color=#0000ff>char</font> c;
  <font color=#0000ff>unsigned</font> <font color=#0000ff>char</font> cu;
  <font color=#0000ff>int</font> i;
  <font color=#0000ff>unsigned</font> <font color=#0000ff>int</font> iu;
  <font color=#0000ff>short</font> <font color=#0000ff>int</font> is;
  <font color=#0000ff>short</font> iis; <font color=#009900>// Same as short int</font>
  <font color=#0000ff>unsigned</font> <font color=#0000ff>short</font> <font color=#0000ff>int</font> isu;
  <font color=#0000ff>unsigned</font> <font color=#0000ff>short</font> iisu;
  <font color=#0000ff>long</font> <font color=#0000ff>int</font> il;
  <font color=#0000ff>long</font> iil;  <font color=#009900>// Same as long int</font>
  <font color=#0000ff>unsigned</font> <font color=#0000ff>long</font> <font color=#0000ff>int</font> ilu;
  <font color=#0000ff>unsigned</font> <font color=#0000ff>long</font> iilu;
  <font color=#0000ff>float</font> f;
  <font color=#0000ff>double</font> d;
  <font color=#0000ff>long</font> <font color=#0000ff>double</font> ld;
  cout 
    &lt;&lt; <font color=#004488>"\n char= "</font> &lt;&lt; <font color=#0000ff>sizeof</font>(c)
    &lt;&lt; <font color=#004488>"\n unsigned char = "</font> &lt;&lt; <font color=#0000ff>sizeof</font>(cu)
    &lt;&lt; <font color=#004488>"\n int = "</font> &lt;&lt; <font color=#0000ff>sizeof</font>(i)
    &lt;&lt; <font color=#004488>"\n unsigned int = "</font> &lt;&lt; <font color=#0000ff>sizeof</font>(iu)
    &lt;&lt; <font color=#004488>"\n short = "</font> &lt;&lt; <font color=#0000ff>sizeof</font>(is)
    &lt;&lt; <font color=#004488>"\n unsigned short = "</font> &lt;&lt; <font color=#0000ff>sizeof</font>(isu)
    &lt;&lt; <font color=#004488>"\n long = "</font> &lt;&lt; <font color=#0000ff>sizeof</font>(il) 
    &lt;&lt; <font color=#004488>"\n unsigned long = "</font> &lt;&lt; <font color=#0000ff>sizeof</font>(ilu)
    &lt;&lt; <font color=#004488>"\n float = "</font> &lt;&lt; <font color=#0000ff>sizeof</font>(f)
    &lt;&lt; <font color=#004488>"\n double = "</font> &lt;&lt; <font color=#0000ff>sizeof</font>(d)
    &lt;&lt; <font color=#004488>"\n long double = "</font> &lt;&lt; <font color=#0000ff>sizeof</font>(ld) 
    &lt;&lt; endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Be aware that the results you get by
running this program will probably be different from one machine/operating
system/compiler to the next, since (as mentioned previously) the only thing that
must be consistent is that each different type hold the minimum and maximum
values specified in the Standard.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you are modifying an <B>int</B> with
<B>short</B> or <B>long</B>, the keyword <B>int</B> is optional, as shown
above.</FONT><A NAME="_Toc462979757"></A><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index563"></A><A NAME="_Toc472654763"></A><BR></P></DIV>
<A NAME="Heading121"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Introduction to pointers</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Whenever you run a program, it is first
loaded (typically from disk) into the computer&#8217;s memory. Thus, all
elements of your program are located somewhere in memory.
<A NAME="Index564"></A>Memory is typically laid out as a sequential series of
memory locations; we usually refer to these locations as eight-bit
<A NAME="Index565"></A><I>bytes </I>but actually the size of each space depends
on the architecture of the particular machine and is usually called that
machine&#8217;s <A NAME="Index566"></A><A NAME="Index567"></A><I>word size</I>.
Each space can be uniquely distinguished from all other spaces by its
<A NAME="Index568"></A><I>address</I>. For the purposes of this discussion,
we&#8217;ll just say that all machines use bytes that have sequential addresses
starting at zero and going up to however much memory you have in your
computer.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Since your program lives in memory while
it&#8217;s being run, every element of your program has an address. Suppose we
start with a simple program:</FONT><BR></P></DIV>

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

<font color=#0000ff>int</font> dog, cat, bird, fish;

<font color=#0000ff>void</font> f(<font color=#0000ff>int</font> pet) {
  cout &lt;&lt; <font color=#004488>"pet id number: "</font> &lt;&lt; pet &lt;&lt; endl;
}

<font color=#0000ff>int</font> main() {
  <font color=#0000ff>int</font> i, j, k;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Each of the elements in this program has
a location in storage when the progr

⌨️ 快捷键说明

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