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

📄 page_848.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_848</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_847.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_848</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_849.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 848</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">TimeType聽time1;<br />TimeType聽time2;<br />int聽聽聽聽聽聽inputHrs;<br />int聽聽聽聽聽聽inputMins;<br />int聽聽聽聽聽聽inputSecs;<br /><br />time1.Set(5,聽20,聽0);<br />//聽Assert:聽time1聽corresponds聽to聽5:20:0<br /><br />cout聽&lt;&lt;聽Enter聽hours,聽minutes,聽seconds:聽;<br />cin聽&gt;&gt;聽inputHrs聽&gt;&gt;聽inputMins聽&gt;&gt;聽inputSecs;<br />time2.Set(inputHrs,聽inputMins,聽inputSecs);<br /><br />if聽(time1.LessThan(time2))<br />聽聽聽聽DoSomething();<br /><br />time2聽=聽time1;聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Member-by-member聽assignment<br />time2.聽Write聽();<br />//聽Assert:聽5:20:0聽is聽output</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">Earlier we remarked that the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Equal</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">LessThan</font><font face="Times New Roman, Times, Serif" size="3"> functions have only one parameter each, even though they are comparing two </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">TimeType</font><font face="Times New Roman, Times, Serif" size="3"> objects. In the If statement of the code segment above, we are comparing </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">time1</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">time2</font><font face="Times New Roman, Times, Serif" size="3">. Because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">LessThan</font><font face="Times New Roman, Times, Serif" size="3"> is a class member function, we invoke it by giving the name of a class object (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">time1</font><font face="Times New Roman, Times, Serif" size="3">), then a dot, then the function name (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">LessThan</font><font face="Times New Roman, Times, Serif" size="3">). Only one item remains unspecified: the class object that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">time1</font><font face="Times New Roman, Times, Serif" size="3"> should be compared with (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">time2</font><font face="Times New Roman, Times, Serif" size="3">). Therefore, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">LessThan</font><font face="Times New Roman, Times, Serif" size="3"> function requires only one parameter, not two. Here is another way of explaining it: If a class member function represents a binary (two-operand) operation, the first operand appears to the left of the dot operator and the second operand is in the parameter list. (To generalize, an <i>n</i>-ary operation has <i>n</i>-1 operands in the parameter list. Thus, a unary operationsuch as </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Write</font><font face="Times New Roman, Times, Serif" size="3"> or </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Increment</font><font face="Times New Roman, Times, Serif" size="3"> in the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">TimeType</font><font face="Times New Roman, Times, Serif" size="3"> classhas an empty parameter list.)</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 addition to member selection and assignment, a few other built-in operators are valid for classes. These operators are used for manipulating memory addresses, and we defer discussing them until later in the book. For now, think of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">.</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">=</font><font face="Times New Roman, Times, Serif" size="3"> as the only valid built-in operators.</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">From the very beginning, you have been working with C++ classes in a particular context: input and output. The standard header file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">iostream.h</font><font face="Times New Roman, Times, Serif" size="3"> contains the declarations of two classes</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">istream</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ostream</font><font face="Times New Roman, Times, Serif" size="3">that manage a program's I/O. The C++ standard library declares </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"> to be objects of these classes:</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">istream聽cin;<br />ostream聽cout;</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_847.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_848</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_849.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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