📄 page_1022.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_1022</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_1021.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_1022</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_1023.html">next page ></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 1022</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">#include聽dynarray.h<br />#include聽<iostream.h><br /><br />int聽main()<br />{<br />聽聽聽聽int聽numElements;聽聽聽//聽Array聽size<br />聽聽聽聽int聽index;聽聽聽聽聽聽聽聽聽//聽Array聽index<br /><br />聽聽聽聽cout聽<<聽Enter聽the聽array聽size:聽;<br />聽聽聽聽cin聽>>聽numElements;<br /><br />聽聽聽聽DynArray聽x(聽numElements);<br />聽聽聽聽DynArray聽y(numElements);<br /><br />聽聽聽聽for聽(index聽=聽0;聽index聽<<聽numElements;聽index++)<br />聽聽聽聽聽聽聽聽x.聽Store(index聽+聽100,聽index);<br />聽聽聽聽y.CopyFrom(x);<br />聽聽聽聽for聽(index聽=聽0;聽index聽<<聽numElements;聽index++)<br />聽聽聽聽聽聽聽聽cout聽<<聽y.聽ValueAt(聽index);<br />聽聽聽聽return聽0;<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">If the input value for </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">numElements</font><font face="Times New Roman, Times, Serif" size="3"> is 20, the class constructor creates a 20element array for </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">x</font><font face="Times New Roman, Times, Serif" size="3"> and initializes all elements to zero. Similarly, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">y</font><font face="Times New Roman, Times, Serif" size="3"> is created with all 20 elements initialized to zero. After using the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Store</font><font face="Times New Roman, Times, Serif" size="3"> function to store 20 new values into </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">x</font><font face="Times New Roman, Times, Serif" size="3">, the program does an aggregate copy of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">x</font><font face="Times New Roman, Times, Serif" size="3"> to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">y</font><font face="Times New Roman, Times, Serif" size="3"> using the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">CopyFrom</font><font face="Times New Roman, Times, Serif" size="3"> operation. Then the program outputs the 20 elements of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">y</font><font face="Times New Roman, Times, Serif" size="3">, which should be the same as the values contained in </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">x</font><font face="Times New Roman, Times, Serif" size="3">. Finally, both class objects go out of scope (because control exits the block in which they are declared), causing the class destructor to be executed for each object. Each call to the destructor deallocates a dynamic array from the free store, as we will see when we look at the implementations of the class member functions.</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">Implementation of the Class: Next, we implement each class member function, placing the function definitions into a C++ implementation file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">dynarray.cpp</font><font face="Times New Roman, Times, Serif" size="3">. As we implement the member functions, we also discuss appropriate testing strategies.</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"><i>The class constructor and destructor:</i> According to the specification file, the class constructor should allocate an array of size </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">arrSize</font><font face="Times New Roman, Times, Serif" size="3">, and the destructor should deallocate the array. The constructor must also do some error checking; it must verify </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">arrSize</font><font face="Times New Roman, Times, Serif" size="3"> is at least 1, and that allocating the dynamic data succeeded. If both of these conditions are met, the constructor sets each array element to zero.</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_1021.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_1022</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_1023.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -