?? ch03.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Teach Yourself C++ in 21 Days</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1 ALIGN="CENTER"><A HREF="ch02.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch02.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="ch04.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch04.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></H1>
<H1></H1>
<UL>
<LI><A HREF="#Heading1">Day 3</A>
<UL>
<LI><A HREF="#Heading2">Variables and Constants</A>
<UL>
<LI><A HREF="#Heading3">What Is a Variable?</A>
<UL>
<LI><A HREF="#Heading4">Figure 3.1.</A>
</UL>
<LI><A HREF="#Heading5">Setting Aside Memory</A>
<UL>
<LI><A HREF="#Heading6">Size of Integers</A>
</UL>
<LI><A HREF="#Heading7">Listing 3.1. Determining the size of variable types</A>
<LI><A HREF="#Heading8">on your computer.</A>
<UL>
<LI><A HREF="#Heading9">signed and unsigned</A>
<LI><A HREF="#Heading10">Fundamental Variable Types</A>
</UL>
<LI><A HREF="#Heading11">Defining a Variable</A>
<UL>
<LI><A HREF="#Heading12">Case Sensitivity</A>
<LI><A HREF="#Heading13">Keywords</A>
</UL>
<LI><A HREF="#Heading14">Creating More Than One Variable at a Time</A>
<LI><A HREF="#Heading16">Assigning Values to Your Variables</A>
<LI><A HREF="#Heading17">Listing 3.2. A demonstration of the use of variables</A><A
HREF="#Heading18">.</A>
<LI><A HREF="#Heading19">typedef</A>
<LI><A HREF="#Heading20">Listing 3.3. A demonstration of typedef</A><A HREF="#Heading21">.</A>
<LI><A HREF="#Heading22">When to Use short and When to Use long</A>
<UL>
<LI><A HREF="#Heading23">Wrapping Around an unsigned Integer</A>
</UL>
<LI><A HREF="#Heading24">Listing 3.4.</A>
<LI><A HREF="#Heading25">A demonstration of putting too large a value in an unsigned
integer.</A>
<UL>
<LI><A HREF="#Heading26">Wrapping Around a signed Integer</A>
</UL>
<LI><A HREF="#Heading27">Listing 3.5.</A>
<LI><A HREF="#Heading28">A demonstration of adding too large a number to a signed
integer.</A>
<LI><A HREF="#Heading29">Characters</A>
<UL>
<LI><A HREF="#Heading30">Characters and Numbers</A>
</UL>
<LI><A HREF="#Heading31">Listing 3.6. Printing characters based on numbers</A><A
HREF="#Heading32">.</A>
<UL>
<LI><A HREF="#Heading33">Special Printing Characters</A>
</UL>
<LI><A HREF="#Heading34">Constants</A>
<UL>
<LI><A HREF="#Heading35">Literal Constants</A>
<LI><A HREF="#Heading36">Symbolic Constants</A>
</UL>
<LI><A HREF="#Heading37">Enumerated Constants</A>
<LI><A HREF="#Heading38">Listing 3.7. A demonstration of enumerated constants</A>
<LI><A HREF="#Heading39">.</A>
<LI><A HREF="#Heading40">Summary</A>
<LI><A HREF="#Heading41">Q&A</A>
<LI><A HREF="#Heading42">Workshop</A>
<UL>
<LI><A HREF="#Heading43">Quiz</A>
<LI><A HREF="#Heading44">Exercises</A>
</UL>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 3</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Variables and Constants</FONT></H2>
<P>Programs need a way to store the data they use. Variables and constants offer
various ways to represent and manipulate that data.</P>
<P>Today you will learn
<UL>
<LI>How to declare and define variables and constants.
<P>
<LI>How to assign values to variables and manipulate those values.
<P>
<LI>How to write the value of a variable to the screen.
</UL>
<H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">What Is a Variable?</FONT></H3>
<P>In C++ a variable is a place to store information. A variable is a location in
your computer's memory in which you can store a value and from which you can later
retrieve that value.</P>
<P>Your computer's memory can be viewed as a series of cubbyholes. Each cubbyhole
is one of many, many such holes all lined up. Each cubbyhole--or memory location--is
numbered sequentially. These numbers are known as memory addresses. A variable reserves
one or more cubbyholes in which you may store a value.</P>
<P>Your variable's name (for example, <TT>myVariable</TT>) is a label on one of these
cubbyholes, so that you can find it easily without knowing its actual memory address.
Figure 3.1 is a schematic representation of this idea. As you can see from the figure,
<TT>myVariable</TT> starts at memory address <TT>103</TT>. Depending on the size
of <TT>myVariable</TT>, it can take up one or more memory addresses.<BR>
<BR>
<A NAME="Heading4"></A><A HREF="javascript:if(confirm('http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch03/032cp01.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/ch03/032cp01.jpg'" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch03/032cp01.jpg"><FONT COLOR="#000077">Figure
3.1.</FONT></A><FONT COLOR="#000077"> </FONT><I>A schematic representation of memory.</I>
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>RAM is random access memory. When
you run your program, it is loaded into RAM from the disk file. All variables are
also created in RAM. When programmers talk of memory, it is usually RAM to which
they are referring.
<HR>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">Setting Aside Memory</FONT></H4>
<P>When you define a variable in C++, you must tell the compiler what kind of variable
it is: an integer, a character, and so forth. This information tells the compiler
how much room to set aside and what kind of value you want to store in your variable.</P>
<P>Each cubbyhole is one byte large. If the type of variable you create is two bytes
in size, it needs two bytes of memory, or two cubbyholes. The type of the variable
(for example, integer) tells the compiler how much memory (how many cubbyholes) to
set aside for the variable.</P>
<P>Because computers use bits and bytes to represent values, and because memory is
measured in bytes, it is important that you understand and are comfortable with these
concepts. For a full review of this topic, please read Appendix B, "C++ Keywords."
<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Size of Integers</FONT></H4>
<P>On any one computer, each variable type takes up a single, unchanging amount of
room. That is, an integer might be two bytes on one machine, and four on another,
but on either computer it is always the same, day in and day out.</P>
<P>A <TT>char</TT> variable (used to hold characters) is most often one byte long.
A <TT>short</TT> integer is two bytes on most computers, a <TT>long</TT> integer
is usually four bytes, and an integer (without the keyword <TT>short</TT> or <TT>long</TT>)
can be two or four bytes. Listing 3.1 should help you determine the exact size of
these types on your computer.</P>
<DL>
<DD>
<HR>
<FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>character</I> is a single
letter, number, or symbol that takes up one byte of memory.
<HR>
</DL>
<P><A NAME="Heading7"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 3.1. Determining
the size of variable types on your computer.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1: #include <iostream.h>
2:
3: int main()
4: {
5: cout << "The size of an int is:\t\t" << sizeof(int) << " bytes.\n";
6: cout << "The size of a short int is:\t" << sizeof(short) << " bytes.\n";
7: cout << "The size of a long int is:\t" << sizeof(long) << " bytes.\n";
8: cout << "The size of a char is:\t\t" << sizeof(char) << " bytes.\n";
9: cout << "The size of a float is:\t\t" << sizeof(float) << " bytes.\n";
10: cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n";
11:
12: return 0;
<TT>13: }</TT>
Output: The size of an int is: 2 bytes.
The size of a short int is: 2 bytes.
The size of a long int is: 4 bytes.
The size of a char is: 1 bytes.
The size of a float is: 4 bytes.
The size of a double is: 8 bytes.
</FONT></PRE>
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>On your computer, the number of
bytes presented might be different.
<HR>
</BLOCKQUOTE>
<DL>
<DD>
<HR>
<FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Most of Listing 3.1 should be
pretty familiar. The one new feature is the use of the <TT>sizeof()</TT> function
in lines 5 through 10. <TT>sizeof()</TT> is provided by your compiler, and it tells
you the size of the object you pass in as a parameter. For example, on line 5 the
keyword <TT>int</TT> is passed into <TT>sizeof()</TT>. Using <TT>sizeof()</TT>, I
was able to determine that on my computer an <TT>int</TT> is equal to a <TT>short</TT>
<TT>int</TT>, which is 2 bytes.
<HR>
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">signed and unsigned</FONT></H4>
<P>In addition, all integer types come in two varieties: <TT>signed</TT> and <TT>unsigned</TT>.
The idea here is that sometimes you need negative numbers, and sometimes you don't.
Integers (<TT>short</TT> and <TT>long</TT>) without the word "unsigned"
are assumed to be <TT>signed</TT>. <TT>Signed</TT> integers are either negative or
positive. <TT>Unsigned</TT> integers are always positive.</P>
<P>Because you have the same number of bytes for both <TT>signed</TT> and <TT>unsigned</TT>
integers, the largest number you can store in an <TT>unsigned</TT> integer is twice
as big as the largest positive number you can store in a <TT>signed</TT> integer.
An <TT>unsigned</TT> <TT>short</TT> integer can handle numbers from 0 to 65,535.
Half the numbers represented by a <TT>signed</TT> <TT>short</TT> are negative, thus
a <TT>signed</TT> <TT>short</TT> can only represent numbers from -32,768 to 32,767.
If this is confusing, be sure to read Appendix A, "Operator Precedence."
<H4 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">Fundamental Variable
Types</FONT></H4>
<P>Several other variable types are built into C++. They can be conveniently divided
into integer variables (the type discussed so far), floating-point variables, and
character variables.</P>
<P>Floating-point variables have values that can be expressed as fractions--that
is, they are real numbers. Character variables hold a single byte and are used for
holding the 256 characters and symbols of the ASCII and extended ASCII character
sets.</P>
<DL>
<DD>
<HR>
<FONT COLOR="#000077"><B>New Term: </B></FONT><I>The ASCII character set</I> is the
set of characters standardized for use on computers. ASCII is an acronym for American
Standard Code for Information Interchange. Nearly every computer operating system
supports ASCII, though many support other international character sets as well.
<HR>
</DL>
<P>The types of variables used in C++ programs are described in Table 3.1. This table
shows the variable type, how much room this book assumes it takes in memory, and
what kinds of values can be stored in these variables. The values that can be stored
are determined by the size of the variable types, so check your output from Listing
3.1. <BR>
<BR>
<FONT SIZE="4"><B>Table 3.1. Variable Types. </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="139" ALIGN="LEFT"><B><I>Type</I></B></TD>
<TD WIDTH="67" ALIGN="LEFT"><B><I>Size</I></B></TD>
<TD ALIGN="LEFT"><B><I>Values</I></B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="139" ALIGN="LEFT"><TT>unsigned short int</TT></TD>
<TD WIDTH="67" ALIGN="LEFT">2 bytes</TD>
<TD ALIGN="LEFT">0 to 65,535</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="139" ALIGN="LEFT"><TT>short int</TT></TD>
<TD WIDTH="67" ALIGN="LEFT">2 bytes</TD>
<TD ALIGN="LEFT">-32,768 to 32,767</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="139" ALIGN="LEFT"><TT>unsigned long int</TT></TD>
<TD WIDTH="67" ALIGN="LEFT">4 bytes</TD>
<TD ALIGN="LEFT">0 to 4,294,967,295</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="139" ALIGN="LEFT"><TT>long int</TT></TD>
<TD WIDTH="67" ALIGN="LEFT">4 bytes</TD>
<TD ALIGN="LEFT">-2,147,483,648 to 2,147,483,647</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -