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

📄 page_920.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_920</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_919.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_920</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_921.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 920</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">class's default constructor. When an </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ExtTime</font><font face="Times New Roman, Times, Serif" size="3"> object is created with the declaration</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">ExtTime聽time2;</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">the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ExtTime</font><font face="Times New Roman, Times, Serif" size="3"> class's default constructor first implicitly calls </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time</font><font face="Times New Roman, Times, Serif" size="3">'s default constructor, after which its body executes, setting </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">zone</font><font face="Times New Roman, Times, Serif" size="3"> to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">EST</font><font face="Times New Roman, Times, Serif" size="3">.</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">Next, look at the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Set</font><font face="Times New Roman, Times, Serif" size="3"> function in Figure 16-6. This function overrides the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Set</font><font face="Times New Roman, Times, Serif" size="3"> function inherited from the base class. Consequently, there are two distinct </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Set</font><font face="Times New Roman, Times, Serif" size="3"> functions, one a public member of the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time</font><font face="Times New Roman, Times, Serif" size="3"> class, the other a public member of the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ExtTime</font><font face="Times New Roman, Times, Serif" size="3"> class. Their full names are </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time::Set</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ExtTime::Set</font><font face="Times New Roman, Times, Serif" size="3">. In Figure 16-6, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ExtTime::Set</font><font face="Times New Roman, Times, Serif" size="3"> function begins by reaching up into its base class and calling </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time::Set</font><font face="Times New Roman, Times, Serif" size="3"> to set the hours, minutes, and seconds. (Remember that a class derived from </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time</font><font face="Times New Roman, Times, Serif" size="3"> cannot access the private data </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">hrs</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">mins</font><font face="Times New Roman, Times, Serif" size="3">, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">secs</font><font face="Times New Roman, Times, Serif" size="3"> directly; these variables are private to the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time</font><font face="Times New Roman, Times, Serif" size="3"> class.) The function then finishes by assigning a value to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ExtTime</font><font face="Times New Roman, Times, Serif" size="3">'s private data, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">zone</font><font face="Times New Roman, Times, Serif" size="3"> variable.</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">The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Write</font><font face="Times New Roman, Times, Serif" size="3"> function in Figure 16-6 uses a similar strategy. It reaches up into its base class and invokes </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time::Write</font><font face="Times New Roman, Times, Serif" size="3"> to output the hours, minutes, and seconds. Then it outputs a string corresponding to the time </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">zone</font><font face="Times New Roman, Times, Serif" size="3">. (Recall that a value of enumeration type cannot be output directly in C++. If we were to print the value of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">zone</font><font face="Times New Roman, Times, Serif" size="3"> directly, the output would be an integer from 0 through 7the internal representations of the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ZoneType</font><font face="Times New Roman, Times, Serif" size="3"> values. The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Write</font><font face="Times New Roman, Times, Serif" size="3"> function establishes an array of eight strings and selects the correct string by using </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">zone</font><font face="Times New Roman, Times, Serif" size="3"> to index into the array.)</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">Now we can compile the file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">exttime.cpp</font><font face="Times New Roman, Times, Serif" size="3"> into an object code file, say, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">exttime.obj</font><font face="Times New Roman, Times, Serif" size="3">. After writing a test driver and compiling it into </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">test.obj</font><font face="Times New Roman, Times, Serif" size="3">, we can obtain an executable file by linking three object 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="Courier New, Courier, Mono New, Courier, Mono" size="2">1.聽test.obj<br />2.聽exttime.obj<br />3.聽time.obj</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">We can then test the resulting program.</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">The remarkable thing about derived classes and inheritance is that modification of the base class is unnecessary. The source code for the implementation of the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time</font><font face="Times New Roman, Times, Serif" size="3"> class may be unavailable. Yet variations of this ADT can continue to be created without that source code, in ways the creator never even considered. Through classes and inheritance, OOP languages facilitate code reuse. A class such as </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time</font><font face="Times New Roman, Times, Serif" size="3"> can be used as-is in many different contexts, or it can be adapted to a particular context by using inheritance. Inheritance allows us to create <i>extensible</i> data abstractionsa derived class typically extends the base class by including additional private data or public operations or both.</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_919.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_920</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_921.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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