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

📄 apa.htm

📁 这是一本关于VC++自学教程的书籍 对于初学者以及编程有一定基础的人均有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
24: 25: double  Area (double base, double height)    // Function definition26: {27: area = (0.5*base*height);28: return area;29: }</PRE><P>This program declares three variables, base, height, and area, on line 8. Variablesstore values that are used by the program. The type of a variable specifies the valuesto be stored in the variable. Table A.1 shows the various types supported by C++.</P><P><H4>TABLE A.1. VARIABLE DATA TYPES.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Variable Data Type</I></TD>		<TD ALIGN="LEFT"><I>Values</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">unsigned short int</TD>		<TD ALIGN="LEFT">0 to 65,535</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">short int</TD>		<TD ALIGN="LEFT">-32,768 to 32,767</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">unsigned long int</TD>		<TD ALIGN="LEFT">0 to 4,294,967,925</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">long int</TD>		<TD ALIGN="LEFT">-2,147,483,648 to 2,147,483,647</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">int</TD>		<TD ALIGN="LEFT">-2,147,483,648 to 2,147,483,647 (32 bit)</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">unsigned int</TD>		<TD ALIGN="LEFT">0 to 4,294,967,295 (32 bit)</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">char</TD>		<TD ALIGN="LEFT">256 character values</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">float</TD>		<TD ALIGN="LEFT">1.2e-38 to 3.4e38</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">double</TD>		<TD ALIGN="LEFT">2.2e-308 to 1.8e308</TD>	</TR></TABLE></P><P>To define a variable, you first define its type, followed by the name. You mayalso assign values to variables by using the assignment (=) operator, as in thesetwo examples:</P><P><PRE>double base = 5;unsigned long int base =5;</PRE><P>In C++, you may also define your own type definition. You do this by using thekeyword typedef, followed by the existing type and name:</P><P><PRE>typedef unsigned long int ULONG;ULONG base =5;</PRE><P>Defining your own type does save you the trouble of typing the entire declaration.</P><P>The next line of the code, line 9, defines the prototype of your function:</P><P><PRE>double Area (double,double);</PRE><P>This function has a type double, a name Area, and a parameter list of two variablesof type double. When you define the prototype, it is not necessary to define theparameters, but it is a good practice to do so. This program takes two inputs fromthe user, namely base and height of the triangle, and calculates the area of thetriangle. The base, height, and area are all variables. The Helloworld.cpp exampleused the insertion (&lt;&lt;) operator. In this example, you use the extraction (&gt;&gt;)operator. The program queries the user to enter a value for the height of the triangleon line 13. When the user enters a value for height, the data from the screen isextracted and placed into the variable height. The process is repeated for the baseof the triangle on lines 15 and 16. After accepting the input from the user, thefunction main() passes execution to the function Area(base,height) along with theparameter values for base and height. When main()<I> </I>passes the execution tothe function Area(base, height), it expects a value of type double in return fromthe function. The calculation of the area of the triangle is conducted on line 27:</P><P><PRE>area = (0.5*base*height);</PRE><BLOCKQUOTE>	<P><HR><STRONG>NOTE:</STRONG> Area is the name of a function, and area is a variable name.	Because C++ is case sensitive, it clearly distinguishes these two names. <HR></BLOCKQUOTE><P>This statement uses the standard operators, the assignment operator (=), and themultiplication operator (*). The assignment operator assigns the result of (0.5*base*height)to the variable area. The multiplication operator (*) calculates the resulting valuesof (0.5*base*height). The assignment operator (=) has an evaluation order from right-to-left.Hence, the multiplication is carried out prior to assigning the values to area. Thefive basic mathematical operators are addition (+), subtraction (-), multiplication(*), division (/), and modulus (%).</P><P>Line 28 of the Area function returns the value of the variable area to the main()function. At this point, the control of the program is returned to line 18 of themain() function. The remainder of the program displays the result of area to thescreen.</P><P><H3><A NAME="Heading3"></A>The if Statement, Operators, and Polymorphism</H3><PRE>While programming large complex programs, it is often necessary to query the user and provide direction to the program based on his input. This is accomplished by using the if statement. The next example demonstrates the application of an if statement. The format of the if statement is</PRE><PRE>if (<I>this expression</I>)   <I>do this</I>;</PRE><P>The if statement is often used in conjunction with relational operators. Anotherformat of the if statement is</P><P><PRE>if (<I>this expression</I>)   <I>do this</I>;else   <I>do this</I>;</PRE><P>Because if statements often use relational operators, let's review relationaloperators. Relational operators are used to determine if two expressions or numbersare equal. If the two expressions or numbers are not equal, the statement will evaluateto either 0 or false. Table A.2 lists the six relational operators defined in C++.</P><P><H4>TABLE A.2. RELATIONAL OPERATORS.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Operator</I></TD>		<TD ALIGN="LEFT"><I>Name</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">==</TD>		<TD ALIGN="LEFT">Comparative</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">!=</TD>		<TD ALIGN="LEFT">Not equal</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">&gt;</TD>		<TD ALIGN="LEFT">Greater than</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">&lt;</TD>		<TD ALIGN="LEFT">Less than</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">&gt;=</TD>		<TD ALIGN="LEFT">Greater than or equal to</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">&lt;=</TD>		<TD ALIGN="LEFT">Less than or equal to</TD>	</TR></TABLE></P><P>C++ also has logical operators. The advantage of logical operators is the abilityto compare two individual expressions and conclude whether they are true or false.Table A.3 lists the three logical operators.</P><P><H4>TABLE A.3. LOGICAL OPERATORS.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Symbol</I></TD>		<TD ALIGN="LEFT"><I>Operator</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">&amp;&amp;</TD>		<TD ALIGN="LEFT">AND</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">||</TD>		<TD ALIGN="LEFT">OR</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">!</TD>		<TD ALIGN="LEFT">NOT</TD>	</TR></TABLE></P><P>An important and powerful feature of C++ is function overloading, or polymorphism.<I>Polymorphism</I> is the ability to have more than one function with the same namethat differ in their parameter lists. The next example is an extension of the previoustriangle code. In this program, you will calculate the area of a triangle and a circle.You will be asked whether you want to calculate the area of a triangle or a circle.Depending upon your response, 1 for triangle and 2 for circle, the program collectsyour input and calculates the area. In Listing A.3, the Area function is overloaded.The same function name is used to calculate the area of the triangle or the circle.The functions differ only in their parameter lists.</P><P><H4>LISTING A.3. Overload.ccp.</H4><PRE> 1: // Workspace Name:  Overload 2: // Program Name:  Overload.cpp 3:  4: # include &lt;iostream.h&gt; 5:  6: double  base,height,radius;        // Global variables 7: double  Area_of_triangle,Area_of_circle;  // Global variables 8: int  choice;                // Global variable 9: 10: double  Area (double,double);       // Function prototype11: double  Area (double);           // Function prototype12: 13: const double  pi = 3.14;          // Constant variable14: 15: void main()                 // main function16: 17: {18:   cout &lt;&lt; &quot;To find the area of a Triangle, input 1 \n&quot;;19:   cout &lt;&lt; &quot;To find the area of a Circle, input 2 \n&quot;;20:   cin &gt;&gt; choice;21: 22: if (choice == 1)23: </PRE><PRE>24: {</PRE><PRE>25:   cout &lt;&lt; &quot;Enter the base of the triangle: &quot;;26:   cin &gt;&gt; base;27:   cout &lt;&lt; &quot;Enter the height of the triangle: &quot;;28:   cin &gt;&gt; height;29: 30: Area_of_triangle = Area(base,height);31: 32:   cout &lt;&lt; &quot;The Area of the Triangle is: &quot;&lt;&lt;Area_of_triangle&lt;&lt;endl;33: }34: 35: if (choice == 2)36: 37: {38:   cout &lt;&lt; &quot;Enter radius of the Circle: &quot;;39:   cin &gt;&gt; radius;40:   Area_of_circle = Area(radius);41:   cout &lt;&lt; &quot;The area of the Circle is: &quot;&lt;&lt;Area_of_circle&lt;&lt;endl;42: }43: 44: if (choice != 1 &amp;&amp; choice != 2)45: 46: {47:   cout &lt;&lt; &quot;Sorry! You must enter either 1 or 2 \n&quot;;48: }49: }50: 51: double Area (double base, double height)52: {53:   return (0.5*base*height)54: }55: 56: double Area(double radius)57: {58:   return (pi*radius*radius);59: }</PRE><H3><A NAME="Heading4"></A>Global and Local Variables</H3><P>In all of the preceding examples, the variables have been declared at the beginningof the program, prior to defining the main() function. Declaring variables in thisfashion is more akin to C programs than C++. They are global variables and can beaccessed by all the functions. However, you may also define local variables thathave a scope only in a particular function. Local variables can have the same namesas the global variables, but they do not change the global variables. Local variablesrefer only to the function in which they are defined. This difference can be confusingand lead to erratic results.</P><P>The program in Listing A.4 clearly shows the difference between global and localvariables. You will calculate the area of a circle using global variables and localvariables.</P><P><H4>LISTING A.4. Global.cpp.</H4><PRE> 1: // Workspace:  Variable 2: // Program name:  Global.cpp 3:  4: #include &lt;iostream.h&gt; 5:  6: double  area; 7: double Area (double); 8: const double pi = 3.14; 9: double  radius = 5;10: 11: int main()12: 13: {14: cout&lt;&lt;&quot;This Program Calculates The Area Of A Circle \n&quot;;15: area = Area (radius);16: cout &lt;&lt; &quot;The Area of the Circle is: &quot;&lt;&lt;area&lt;&lt;endl;17: cout &lt;&lt; &quot;The Radius In the Main() Function is: &quot;&lt;&lt;radius&lt;&lt;endl;18: return 0;19: }20: 21: double  Area (double radius)22: {23: area = (pi*radius*radius);24: cout&lt;&lt;&quot;The Radius In the Area() Function is: &quot;&lt;&lt;radius&lt;&lt;endl;25: return area;26: }</PRE><P>The variable radius is accessible in the main() function and also the Area() function,and it is the same. The result of executing this program is shown in Figure A.6.</P><P><A HREF="javascript:popUp('0afig06.gif')"><B>FIGURE A.6.</B></A><B> </B><I>Global.cpp--usinga global variable.</I></P><P>As the program executes, it shows the value of the variable radius in the differentfunctions. You will now modify the global variable to be a local variable. Add anadditional line to the Area function defining a local variable:</P><P><PRE>double radius = 2;</PRE><P>Compile and execute this program. The results are shown in Figure A.7.</P><P><A HREF="javascript:popUp('0afig07.gif')"><B>FIGURE A.7.</B></A><B> </B><I>Global.cpp--globaland local variables.</I></P><P>You will notice that the value of the variable radius remains unchanged in themain() function and changes locally in the Area() function. The area of the circleis calculated based on the value of the local variable, whereas at the same time,the value of the global variable is not changed but is hidden from the function Area().</P><BLOCKQUOTE>	<P><HR>

⌨️ 快捷键说明

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