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

📄 page_991.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_991</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_990.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_991</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_992.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 991</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="Courier New, Courier, Mono New, Courier, Mono" size="2">聽聽聽聽聽聽聽int聽聽聽聽Yr;<br />聽聽聽聽聽聽聽char*聽msg;<br />};</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">In the class constructor's parameter list, we could just as well have declared </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">msgStr</font><font face="Times New Roman, Times, Serif" size="3"> as</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">const聽char[]聽msgStr</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">instead of</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">const聽char*聽msgStr</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">Remember that both of these declarations are equivalent as far as the C++ compiler is concerned. They both mean that the parameter being passed is the base address of a string.</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">As you can see in the private part of the class declaration, the private variable </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">msg</font><font face="Times New Roman, Times, Serif" size="3"> is a pointer, not a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">char</font><font face="Times New Roman, Times, Serif" size="3"> array. If we declared </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">msg</font><font face="Times New Roman, Times, Serif" size="3"> to be an array of fixed size, say, 30, the array might be either too large or too small to hold the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">msgStr</font><font face="Times New Roman, Times, Serif" size="3"> string that the client passes through the constructor's parameter list. Instead, the class constructor will dynamically allocate a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">char</font><font face="Times New Roman, Times, Serif" size="3"> array of just the right size on the free store and make </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">msg</font><font face="Times New Roman, Times, Serif" size="3"> point to this array. Here is the implementation of the class constructor as it would appear in the implementation file:</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">#include聽&lt;string.h&gt;聽聽聽聽//聽For聽strcpy()聽and聽strlen()<br />聽聽.<br />聽聽.<br />聽聽.<br />Date::Date(聽/*聽in聽*/聽int聽聽聽聽聽聽聽聽聽initMo,<br />聽聽聽聽聽聽聽聽聽聽聽聽/*聽in聽*/聽int聽聽聽聽聽聽聽聽聽initDay,<br />聽聽聽聽聽聽聽聽聽聽聽聽/*聽in聽*/聽int聽聽聽聽聽聽聽聽聽initYr,<br />聽聽聽聽聽聽聽聽聽聽聽聽/*聽in聽*/聽const聽char*聽msgStr聽聽)<br />{<br />聽聽聽聽mo聽=聽initMo;<br />聽聽聽聽day聽=聽initDay;<br />聽聽聽聽yr聽=聽initYr;<br />聽聽聽聽msg聽=聽new聽char[strlen(msgStr)聽+1];<br />聽聽聽聽//聽Assert:<br />聽聽聽聽//聽聽聽聽聽Storage聽for聽dynamic聽string聽is聽now聽on聽free聽store<br />聽聽聽聽//聽聽聽聽聽and聽its聽base聽address聽is聽in聽msg<br />聽聽聽聽strcpy(msg,聽msgStr);<br />聽聽聽聽//聽Assert:<br />聽聽聽聽//聽聽聽聽聽Incoming聽string聽has聽been聽copied聽to聽free聽store<br />}</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_990.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_991</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_992.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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