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

📄 page_342.html

📁 Programming and Problem Solving with C++
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">	<html>		<head>			<title>page_342</title>			<link rel="stylesheet" href="reset.css" type="text/css" media="all">			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		</head>		<body>		<table summary="top nav" border="0" width="100%">			<tr>				<td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_341.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_342</strong></td>				<td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_343.html">next page&nbsp;&gt;</a></td>			</tr>					<tr>				<td align="left" colspan="3" style="background: #ffffff; padding: 20px;">    <table border="0" width="100%" cellpadding="0"><tr><td align="center">  <table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="left"></td>  <td align="right"></td>  </tr></table></td></tr><tr><td align="left"><p></p><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="right"><font face="Times New Roman, Times, Serif" size="2" color="#FF0000">Page 342</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">Exactly what are in these header files?</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">It turns out that there is nothing magical about header files. Their contents are nothing more than a series of C++ declarations. There are declarations of named constants such as </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">INT_MAX</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">INT_MIN</font><font face="Times New Roman, Times, Serif" size="3">, and there are declarations of stream variables like </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">cin</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">cout</font><font face="Times New Roman, Times, Serif" size="3">. But most of the items in a header file are function prototypes.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">Suppose that your program needs to use the library function </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">sqrt</font><font face="Times New Roman, Times, Serif" size="3"> in a statement like this:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">y聽=聽sqrt(x);</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">Every identifier must be declared before it can be used. If you forget to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">#include</font><font face="Times New Roman, Times, Serif" size="3"> the header file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">math.h</font><font face="Times New Roman, Times, Serif" size="3">, the compiler gives you an UNDECLARED IDENTIFIER error message. The file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">math.h</font><font face="Times New Roman, Times, Serif" size="3"> contains function prototypes for </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">sqrt</font><font face="Times New Roman, Times, Serif" size="3"> and all of the other math-oriented library functions. With this header file included in your program, the compiler not only knows that the identifier </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">sqrt</font><font face="Times New Roman, Times, Serif" size="3"> is the name of a function but it also can verify that your function call is correct with respect to the number of parameters and their data types.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">Header files save you the trouble of specifying all of the library function prototypes yourself at the beginning of your program. With just one linethe </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">#include</font><font face="Times New Roman, Times, Serif" size="3"> directiveyou cause the preprocessor to go out and find the header file and insert the prototypes into your program. In later chapters, we see how to create our own header files that contain declarations specific to our programs.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="17"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">Parameters</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="3">When a function is executed, it uses the actual parameters given to it in the function call. How is this done? The answer to this question depends on the nature of the formal parameters. C++ supports two kinds of formal parameters: value parameters and reference parameters. With a value parameter, which is declared without an ampersand (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">&amp;</font><font face="Times New Roman, Times, Serif" size="3">) at the end of the data type name, the function receives a copy of the actual parameter's value. With a reference parameter, which is declared by adding an ampersand to the data type name, the function receives the location (memory address) of the actual parameter. Before we examine in detail the difference between these two kinds of parameters, let's look at an example of a function heading with a mixture of reference and value parameter declarations.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="12"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">void聽Example(聽int&amp;聽paraml,聽聽聽聽聽聽聽聽聽聽聽//聽A聽reference聽parameter<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽int聽聽param2,聽聽聽聽聽聽聽聽聽聽聽//聽A聽value聽parameter<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽float聽param3聽)聽聽聽聽聽聽聽聽聽//聽Another聽value聽parameter</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><p><font size="0"></font></p>  </td>			</tr>				<tr>				<td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_341.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_342</strong></td>				<td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_343.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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