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

📄 apa.htm

📁 这是一本关于VC++自学教程的书籍 对于初学者以及编程有一定基础的人均有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<STRONG>NOTE:</STRONG> It is always advisable to differentiate your global and local	variables by prefixing them with a g for global and l for local. <HR></BLOCKQUOTE><H2><A NAME="Heading5"></A>Pointers</H2><P>Pointers are one of the most important features of C++, and they are always confusingto new programmers of C++. Pointers work by providing access to the original datadirectly, which increases efficiency. Pointers primarily work with two operators,the indirection operator (*) and the address-of operator (&amp;). It is common practiceto add a p to the beginning of a pointer variable's name to distinguish it from othervariables. A pointer is just another variable, but the difference is it holds a memoryaddress. You declare a pointer by putting an asterisk (*) in front of the pointername. To access the address of the variable, you put the &amp; operator in frontof the variable name.</P><P>To understand pointers, you need a brief overview of how variables are stored.You covered different variable types in Table A.1. Table A.4 shows the size of thevariable types.</P><P><H4>TABLE A.4. VARIABLE TYPE SIZES.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Variable Type</I></TD>		<TD ALIGN="LEFT"><I>Size in Bytes</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">unsigned short int</TD>		<TD ALIGN="LEFT">2 bytes</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">short int</TD>		<TD ALIGN="LEFT">2 bytes</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">unsigned long int</TD>		<TD ALIGN="LEFT">4 bytes</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">long int</TD>		<TD ALIGN="LEFT">4 bytes</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">int</TD>		<TD ALIGN="LEFT">4 bytes (32 bit)</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">unsigned int</TD>		<TD ALIGN="LEFT">2 bytes(32 bit)</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">char</TD>		<TD ALIGN="LEFT">1 byte</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">float</TD>		<TD ALIGN="LEFT">4 bytes</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">double</TD>		<TD ALIGN="LEFT">8 bytes</TD>	</TR></TABLE></P><P>In the program address.cpp in Listing A.5, the two variables base and radius eachoccupy 8 and 4 bytes. Assume that your computer memory has a certain space to storethese variables, they are sequentially numbered from 1 through 12, and each spaceis 1 byte. When you declare the variable base of type double, it occupies 8 bytes.Assume these 8 bytes reside at locations beginning from 1 through 8. You also declaredanother variable radius of type int, which occupies 4 bytes and its location is byte9 through byte 12. The location of each of these variables is termed as its address.Hence, the variable base has an address beginning at address 1 and ending at address8. Similarly, the variable radius has an address beginning at address 9 and endingat address 12. When you use the address-of operator (&amp;) on a variable, this isthe address returned. The variable base has an address from 1 through 8, but theaddress-of operator returns its address as 1. Internally, the system already knowsthat the total addresses occupied are 8 because you defined its type as double.</P><BLOCKQUOTE>	<P><HR><STRONG>NOTE:</STRONG> The byte size shown in Table A.4 is not fixed. It can be different	depending on your compiler and the hardware on which it runs. To determine the size	of the variable for your individual compiler and hardware settings, use the sizeof()	function as implemented in Listing A.5 on lines 13 and 16. <HR></BLOCKQUOTE><P>The program in Listing A.5 shows how to access the memory address of variables.</P><P><H4>LISTING A.5. Address.cpp.</H4><PRE> 1: // Workspace:  Pointers 2: // Program name:  Address.cpp 3:  4: #include &lt;iostream.h&gt; 5:  6: double base = 5.0; 7: int radius = 2; 8:  9: void main()10: {11: cout&lt;&lt;&quot;The VALUE of base is: &quot;&lt;&lt;base&lt;&lt;endl;12: cout&lt;&lt;&quot;The ADDRESS of base is: &quot;&lt;&lt;&amp;base&lt;&lt;endl;13: cout&lt;&lt;&quot;The SIZE of double base is: &quot;&lt;&lt;sizeof(double)&lt;&lt; &quot;bytes \n&quot;;14: cout&lt;&lt;&quot;The VALUE of radius is: &quot;&lt;&lt;radius&lt;&lt;endl;15: cout&lt;&lt;&quot;The ADDRESS of radius is: &quot;&lt;&lt;&amp;radius&lt;&lt;endl;16: cout&lt;&lt;&quot;The SIZE of integer radius is: &quot;&lt;&lt;sizeof(int)&lt;&lt;&quot; bytes \n&quot;;17: }</PRE><P>The address of the variables is accessed directly on lines 12 and 15 by usingthe address-of operator (&amp;).The addresses of the variables base and radius areshown in Figure A.8. The addresses of the variables depend on your system, so theymight not be the same.</P><P><A HREF="javascript:popUp('0afig08.gif')"><B>FIGURE A.8.</B></A><B> </B><I>Usingthe address-of operator.</I></P><P>The indirection operator (*) operates by providing access to the value storedin the address of the variable. When a pointer is declared for a specific variabletype (such as int), it should not be used with any other type unless it is recastto a new type. You should remember that a pointer is a variable, and like all othervariables, it should be declared and initialized. A pointer that is not initializedcould be dangerous. The program in Listing A.5 is modified to access the values ofthe variables radius and base. The modified program is provided in Listing A.6.</P><P><H4>LISTING A.6. Address.cpp.</H4><PRE> 1: // Workspace:  Pointers</PRE><PRE> 2: // Program name:  Address.cpp 3:  4: #include &lt;iostream.h&gt; 5:  6: double base =5.0; 7: int radius =2; 8:  9: double *pBase =0;           // Initialize the pointer variable10: int *pRadius =0;            // Initialize the pointer variable11: 12: void main()13: {14: pBase = &amp;base;                   // Assign the address of base15: pRadius = &amp;radius;               // Assign the address of radius16: cout&lt;&lt;&quot;The VALUE of base is: &quot;&lt;&lt;base&lt;&lt;endl;    // Output value of base17: cout&lt;&lt;&quot;The ADDRESS of base is: &quot;&lt;&lt;&amp;base&lt;&lt;endl; // Output address of           &Acirc;base18: cout&lt;&lt;&quot;The SIZE of double base is: &quot;&lt;&lt;sizeof(double)&lt;&lt; &quot;bytes \n 19: cout&lt;&lt;&quot;The VALUE of pBase is: &quot;&lt;&lt;*pBase&lt;&lt;endl;  	            &Acirc; // Output redirected value of base20: 21: cout&lt;&lt;&quot;The VALUE of radius is: &quot;&lt;&lt;radius&lt;&lt;endl;     	            &Acirc;// Output value of radius22: cout&lt;&lt;&quot;The ADDRESS of radius is: &quot;&lt;&lt;&amp;radius&lt;&lt;endl;   	            &Acirc;// Output address of base23: cout&lt;&lt;&quot;The SIZE of integer radius is: &quot;&lt;&lt;sizeof(int)&lt;&lt;&quot; bytes \n&quot;; 24: cout&lt;&lt;&quot;The VALUE of pRadius is: &quot;&lt;&lt;*pRadius&lt;&lt;endl;  	            &Acirc;// Output redirected value of radius25: 26: }</PRE><H3><A NAME="Heading6"></A>References</H3><P>An important feature in C++ that is used often with function parameters is <I>references</I>.Reference is simply a synonym for variable. Until now, you have passed parametersin functions by value. You will learn how to pass parameters by reference. You createa reference variable by specifying its type and preceding the name with the referenceoperator (&amp;). If you have a variable float radius, you create a reference with</P><P><PRE>void functionname (float &amp;rfradius);</PRE><P>You can give the reference variable any name you want; in the following example,the reference variable names have an rf prefix. The advantage of a reference is thatyou can pass it as a parameter, like any other variable. However, unlike regularparameters, changes made to the reference's value while in a function are storedin the original variable. The example in Listing A.7 shows how the reference changesthe value of the variable in the main() function.</P><P><H4>LISTING A.7. Refer.cpp.</H4><PRE> 1: // Workspace:  Reference 2: // Program name:  Refer.cpp 3:  4: #include &lt;iostream.h&gt; 5:  6: void squareit (float &amp;num); 7: int main() 8:  9: {10: float num=5.0;11: 12: cout&lt;&lt;&quot;In Main: before squaring number: &quot;&lt;&lt;num*num&lt;&lt;&quot;\n&quot;;13: 14: squareit (num);15: cout&lt;&lt;&quot;In Main: after squaring number: &quot;&lt;&lt;num*num&lt;&lt;&quot;\n&quot;;16: return 0;17: 18: }19: 20: void  squareit (float &amp;rfnum)21: {22: 23: cout&lt;&lt;&quot;In Squareit: before squaring number: &quot;&lt;&lt;rfnum*rfnum&lt;&lt;&quot;\n&quot;;24: 25: rfnum = rfnum+5;26: 27: cout&lt;&lt;&quot;In Squareit: after squaring number: &quot;&lt;&lt;rfnum*rfnum&lt;&lt;&quot;\n&quot;;28: 29: }</PRE><P>You define a function squareit on line 6, and its parameters are references. Thisis the function prototype. On line 10, the variable num is given a value of 5. Thesquare of the number is displayed to the screen on line 15. On line 14, you callthe squareit function.</P><BLOCKQUOTE>	<P><HR><STRONG>NOTE:</STRONG> You pass the variable num and not its address. <HR></BLOCKQUOTE><P>Only when execution jumps to line 20 from line 14 are the variables identifiedas references. On line 27, the references are squared and displayed. They shouldbe the same as the variables because they are just like aliases for the variables.On line 25, you add 5 to the reference, which in turn changes the variable num. Theincremented value is squared and displayed to the screen. Execution returns to main()on line 15, where the display confirms the variable was changed. The output for thisprogram is shown in Figure A.9.</P><P><A HREF="javascript:popUp('0afig09.gif')"><B>FIGURE A.9.</B></A><B> </B><I>Passingparameters by reference.</I><DL>	<P></DL><H2><A NAME="Heading7"></A>Classes</H2><P>In the previous sections, you used data types (int, float, and so on) that areinherently built into C++. In large complex programs, it is easier to define yourown type, which could be a combination of the inherent types. Classes were addedto C++ primarily for this purpose--to enable the programmer to be able to definecustom data types and methods. The concept of classes in C++ evolved due to certainlimitations of the concept of structures in C. To thoroughly understand classes,you have to step back into C and understand structures first.</P><P>A structure in C/C++ is a way of representing your own custom data. When you definedvariables, you first defined their data types, followed by their names:</P><P><PRE>int radius;</PRE><P>To define your own data types, you use the keyword struct. The syntax for declaringa structure is</P><P><PRE><I>struct</I> [<I>structure_name</I>]  {  <I>data_members</I>  }</PRE><P>The <I>data_members</I> of a structure are variables and functions. When functionsare associated with classes, they are more appropriately referred to as methods.From now on, you use the term function for program code that is not a part of a structureor class. A reference to methods indicates that the function is associated with aclass structure. To understand how structures are used, review the example in ListingA.8.</P><P><H4>LISTING A.8. Struct.cpp.</H4><PRE> 1: // Workspace Name: Class1 2: // Program Name:  Struct.cpp 3: #include &lt;iostream.h&gt; 4:  5: struct farm_house  6: { 7: int pig_values; 8: }; 9: 10: int main()11: {12: farm_house pig1, pig2, pig3;13: 14: pig1.pig_values = 12;15: pig2.pig_values = 13;16: pig3.pig_values = 14;17: 18: cout &lt;&lt; &quot;The value of pig1 is &quot; &lt;&lt; pig1.pig_values&lt;&lt; &quot;\n&quot;;19: cout &lt;&lt; &quot;The value of pig2 is &quot; &lt;&lt; pig2.pig_values &lt;&lt; &quot;\n&quot;;20: cout &lt;&lt; &quot;The value of pig3 is &quot; &lt;&lt; pig3.pig_values &lt;&lt; &quot;\n&quot;;21:  22: return 0;23: }.</PRE><P>On line 5, the struct keyword is followed by the name of the structure. The actualdefinition of the structure is enclosed in the curly brackets. This particular structuredefines a data member of type int and name pig_value. If you remember, I mentionedearlier that when you define a structure, you basically define a custom-made datatype. All data types end with a semicolon, so the structure should also end witha semicolon. On line 12, you define three instances of the same type of farm_house,each of which contains a single int type variable.</P><BLOCKQUOTE>	<P><HR><STRONG>NOTE:</STRONG> If you strictly use C, then to define instances on line 12,	you must use the keyword struct:</P>	<PRE>struct farm_house pig1, pig2, pig3;</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE>	<P>This is no longer required in C++. <HR></BLOCKQUOTE><P>On lines 14 through 16, you assign values to the member variables of each structure.The structure member operator (.), also called the dot operator, is used to accessmember variables of the structure. On lines 18 through 20, the assigned values areoutput to the screen. Figure A.10 shows the output from this program.</P><P><A HREF="javascript:popUp('0afig10.gif')"><B>FIGURE A.10.</B></A><B> </B><I>Structureoutput.</I></P><P>The most important concept of object-oriented programming is encapsulation. Encapsulationcan involve one or more classes. Encapsulation promotes safeguards and data hiding.The struct.cpp program had no encapsulation or classes. What do encapsulation andclasses mean in object-oriented programming?</P>

⌨️ 快捷键说明

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