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

📄 ch03.htm

📁 Why C++ is the emerging standard in software development. The steps to develop a C++ program. How
💻 HTM
📖 第 1 页 / 共 4 页
字号:
		<TD WIDTH="139" ALIGN="LEFT"><TT>int (16 bit)</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>int (32 bit)</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">		<TD WIDTH="139" ALIGN="LEFT"><TT>unsigned int (16 bit)</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>unsigned int (32 bit)</TT></TD>		<TD WIDTH="67" ALIGN="LEFT">2 bytes</TD>		<TD ALIGN="LEFT">0 to 4,294,967,295</TD>	</TR>	<TR ALIGN="LEFT" rowspan="1">		<TD WIDTH="139" ALIGN="LEFT"><TT>char</TT></TD>		<TD WIDTH="67" ALIGN="LEFT">1 byte</TD>		<TD ALIGN="LEFT">256 character values</TD>	</TR>	<TR ALIGN="LEFT" rowspan="1">		<TD WIDTH="139" ALIGN="LEFT"><TT>float</TT></TD>		<TD WIDTH="67" ALIGN="LEFT">4 bytes</TD>		<TD ALIGN="LEFT">1.2e-38 to 3.4e38</TD>	</TR>	<TR ALIGN="LEFT" rowspan="1">		<TD WIDTH="139" ALIGN="LEFT"><TT>double</TT></TD>		<TD WIDTH="67" ALIGN="LEFT">8 bytes</TD>		<TD ALIGN="LEFT">2.2e-308 to 1.8e308</TD>	</TR></TABLE><BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>The sizes of variables might be	different from those shown in Table 3.1, depending on the compiler and the computer	you are using. If your computer had the same output as was presented in Listing 3.1,	Table 3.1 should apply to your compiler. If your output from Listing 3.1 was different,	you should consult your compiler's manual for the values that your variable types	can hold. <HR></BLOCKQUOTE><CENTER><H3><A NAME="Heading11"></A><FONT COLOR="#000077">Defining a Variable</FONT></H3></CENTER><P>You create or define a variable by stating its type, followed by one or more spaces,followed by the variable name and a semicolon. The variable name can be virtuallyany combination of letters, but cannot contain spaces. Legal variable names include<TT>x</TT>, <TT>J23qrsnf</TT>, and <TT>myAge</TT>. Good variable names tell you whatthe variables are for; using good names makes it easier to understand the flow ofyour program. The following statement defines an integer variable called <TT>myAge</TT>:</P><PRE><FONT COLOR="#0066FF">int myAge;</FONT></PRE><P>As a general programming practice, avoid such horrific names as <TT>J23qrsnf</TT>,and restrict single-letter variable names (such as <TT>x</TT> or <TT>i</TT>) to variablesthat are used only very briefly. Try to use expressive names such as <TT>myAge</TT>or <TT>howMany</TT>. Such names are easier to understand three weeks later when youare scratching your head trying to figure out what you meant when you wrote thatline of code.</P><P>Try this experiment: Guess what these pieces of programs do, based on the firstfew lines of code:</P><P>Example 1</P><PRE><FONT COLOR="#0066FF">main(){     unsigned short x;     unsigned short y;     ULONG z;     z = x * y;}</FONT></PRE><P>Example 2</P><PRE><FONT COLOR="#0066FF">main (){     unsigned short Width;     unsigned short Length;     unsigned short Area;     Area = Width * Length;}</FONT></PRE><P>Clearly, the second program is easier to understand, and the inconvenience ofhaving to type the longer variable names is more than made up for by how much easierit is to maintain the second program.<CENTER><H4><A NAME="Heading12"></A><FONT COLOR="#000077">Case Sensitivity</FONT></H4></CENTER><P>C++ is case-sensitive. In other words, uppercase and lowercase letters are consideredto be different. A variable named <TT>age</TT> is different from <TT>Age</TT>, whichis different from <TT>AGE</TT>.<BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Some compilers allow you to turn	case sensitivity off. Don't be tempted to do this; your programs won't work with	other compilers, and other C++ programmers will be very confused by your code. <HR></BLOCKQUOTE><P>There are various conventions for how to name variables, and although it doesn'tmuch matter which method you adopt, it is important to be consistent throughout yourprogram.</P><P>Many programmers prefer to use all lowercase letters for their variable names.If the name requires two words (for example, my car), there are two popular conventions:<TT>my_car</TT> or <TT>myCar</TT>. The latter form is called camel-notation, becausethe capitalization looks something like a camel's hump.</P><P>Some people find the underscore character (<TT>my_car</TT>) to be easier to read,while others prefer to avoid the underscore, because it is more difficult to type.This book uses camel-notation, in which the second and all subsequent words are capitalized:<TT>myCar</TT>, <TT>theQuickBrownFox</TT>, and so forth.<BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Many advanced programmers employ	a notation style that is often referred to as Hungarian notation. The idea behind	Hungarian notation is to prefix every variable with a set of characters that describes	its type. Integer variables might begin with a lowercase letter i, longs might begin	with a lowercase l. Other notations indicate constants, globals, pointers, and so	forth. Most of this is much more important in C programming, because C++ supports	the creation of user-defined types (see Day 6, &quot;Basic Classes&quot;) and because	C++ is strongly typed. <HR></BLOCKQUOTE><CENTER><H4><A NAME="Heading13"></A><FONT COLOR="#000077">Keywords</FONT></H4></CENTER><P>Some words are reserved by C++, and you may not use them as variable names. Theseare keywords used by the compiler to control your program. Keywords include <TT>if</TT>,<TT>while</TT>, <TT>for</TT>, and <TT>main</TT>. Your compiler manual should providea complete list, but generally, any reasonable name for a variable is almost certainlynot a keyword.<BLOCKQUOTE>	<P><HR><B>DO</B> define a variable by writing the type, then the variable name. <B>DO</B>	use meaningful variable names. DO remember that C++ is case sensitive. <B>DON'T</B>	use C++ keywords as variable names. DO understand the number of bytes each variable	type consumes in memory, and what values can be stored in variables of that type.	<B>DON'T</B> use <TT>unsigned</TT> variables for negative numbers. <HR></BLOCKQUOTE><CENTER><H3><A NAME="Heading14"></A><FONT COLOR="#000077">Creating More Than One Variableat a Time</FONT></H3></CENTER><CENTER><H3><FONT COLOR="#000077"></FONT></H3></CENTER><P>You can create more than one variable of the same type in one statement by writingthe type and then the variable names, separated by commas. For example:</P><PRE><FONT COLOR="#0066FF">unsigned int myAge, myWeight;   // two unsigned int variableslong area, width, length;       // three longs</FONT></PRE><P>As you can see, <TT>myAge</TT> and <TT>myWeight</TT> are each declared as <TT>unsigned</TT>integer variables. The second line declares three individual <TT>long</TT> variablesnamed <TT>area</TT>, <TT>width</TT>, and <TT>length</TT>. The type (<TT>long</TT>)is assigned to all the variables, so you cannot mix types in one definition statement.<CENTER><H3><A NAME="Heading16"></A><FONT COLOR="#000077">Assigning Values to Your Variables</FONT></H3></CENTER><P>You assign a value to a variable by using the assignment operator (<TT>=</TT>).Thus, you would assign <TT>5</TT> to <TT>Width</TT> by writing</P><PRE><FONT COLOR="#0066FF">unsigned short Width;Width = 5;</FONT></PRE><P>You can combine these steps and initialize <TT>Width</TT> when you define it bywriting</P><PRE><FONT COLOR="#0066FF">unsigned short Width = 5;</FONT></PRE><P>Initialization looks very much like assignment, and with integer variables, thedifference is minor. Later, when constants are covered, you will see that some valuesmust be initialized because they cannot be assigned to. The essential differenceis that initialization takes place at the moment you create the variable.</P><P>Just as you can define more than one variable at a time, you can initialize morethan one variable at creation. For example:</P><PRE><FONT COLOR="#0066FF">// create two long variables and initialize them&#194;long width = 5, length = 7;  </FONT></PRE><P>This example initializes the <TT>long</TT> integer variable <TT>width</TT> tothe value <TT>5</TT> and the <TT>long</TT> integer variable <TT>length</TT> to thevalue <TT>7</TT>. You can even mix definitions and initializations:</P><PRE><FONT COLOR="#0066FF">int myAge = 39, yourAge, hisAge = 40;</FONT></PRE><P>This example creates three type <TT>int</TT> variables, and it initializes thefirst and third.</P><P>Listing 3.2 shows a complete program, ready to compile, that computes the areaof a rectangle and writes the answer to the screen.</P><P><A NAME="Heading17"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 3.2. A demonstrationof the use of variables.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:   // Demonstration of variables2:   #include &lt;iostream.h&gt;3:4:   int main()5:   {6:     unsigned short int Width = 5, Length;7:     Length = 10;8:9:     // create  an unsigned short and initialize with result10:       // of multiplying Width by Length11:     unsigned short int Area  = Width * Length;12:13:     cout &lt;&lt; &quot;Width:&quot; &lt;&lt; Width &lt;&lt; &quot;\n&quot;;14:     cout &lt;&lt; &quot;Length: &quot;  &lt;&lt; Length &lt;&lt; endl;15:     cout &lt;&lt; &quot;Area: &quot; &lt;&lt; Area &lt;&lt; endl;16:        return 0;<TT>17: }</TT></FONT></PRE><PRE><FONT COLOR="#0066FF">Output: Width:5Length: 10Area: 50</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Line 2 includes the required<TT>include</TT> statement for the <TT>iostream</TT>'s library so that <TT>cout</TT>will work. Line 4 begins the program. <BR><BR>On line 6, <TT>Width</TT> is defined as an <TT>unsigned</TT> <TT>short</TT> integer,and its value is initialized to <TT>5</TT>. Another <TT>unsigned short</TT> integer,<TT>Length</TT>, is also defined, but it is not initialized. On line 7, the value<TT>10</TT> is assigned to <TT>Length</TT>.</P><P>On line 11, an <TT>unsigned short</TT> integer, <TT>Area</TT>, is defined, andit is initialized with the value obtained by multiplying <TT>Width</TT> times <TT>Length</TT>.On lines 13-15, the values of the variables are printed to the screen. Note thatthe special word <TT>endl</TT> creates a new line.<CENTER><H3><A NAME="Heading19"></A><FONT COLOR="#000077">typedef</FONT></H3></CENTER><P>It can become tedious, repetitious, and, most important, error-prone to keep writing<TT>unsigned short int</TT>. C++ enables you to create an alias for this phrase byusing the keyword <TT>typedef</TT>, which stands for type definition.</P><P>In effect, you are creating a synonym, and it is important to distinguish thisfrom creating a new type (which you will do on Day 6). <TT>typedef</TT> is used bywriting the keyword <TT>typedef</TT>, followed by the existing type and then thenew name. For example</P><PRE><FONT COLOR="#0066FF">typedef unsigned short int USHORT</FONT></PRE><P>creates the new name <TT>USHORT</TT> that you can use anywhere you might havewritten <TT>unsigned short int</TT>. Listing 3.3 is a replay of Listing 3.2, usingthe type definition <TT>USHORT</TT> rather than <TT>unsigned short int</TT>.</P><P><A NAME="Heading20"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 3.3. A demonstrationof typedef</B></FONT><FONT SIZE="2" COLOR="#000077"><B>.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:   // *****************2:   // Demonstrates typedef keyword3:   #include &lt;iostream.h&gt;4:5:   typedef unsigned short int USHORT;       //typedef defined6:7:   void main()8:   {9:     USHORT  Width = 5;10:    USHORT Length;11:    Length = 10;12:    USHORT Area  = Width * Length;13:    cout &lt;&lt; &quot;Width:&quot; &lt;&lt; Width &lt;&lt; &quot;\n&quot;;14:    cout &lt;&lt; &quot;Length: &quot;  &lt;&lt; Length &lt;&lt; endl;15:    cout &lt;&lt; &quot;Area: &quot; &lt;&lt; Area &lt;&lt;endl;<TT>16: }</TT>Output: Width:5Length: 10Area: 50</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis: </B></FONT>On line 5, <TT>USHORT</TT> is typedefinedas a synonym for <TT>unsigned short int</TT>. The program is very much like Listing3.2, and the output is the same.<CENTER><H3><A NAME="Heading22"></A><FONT COLOR="#000077">When to Use short and When to Use

⌨️ 快捷键说明

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