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

📄 ch11.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
is used again in the next iteration of the loop. Isn't there a danger that therewill now be no pointer to that <TT>CAT</TT> object, and a memory leak has been created?</P><P>This would be a big problem, except that deleting <TT>Family</TT> returns allthe memory set aside for the array. The compiler is smart enough to destroy eachobject in the array and to return its memory to the free store.</P><P>To see this, change the size of the array from 500 to 10 in lines 26, 29, and37. Then uncomment the <TT>cout</TT> statement in line 21. When line 40 is reachedand the array is destroyed, each <TT>CAT</TT> object destructor is called.</P><P>When you create an item on the heap by using <TT>new</TT>, you always delete thatitem and free its memory with <TT>delete</TT>. Similarly, when you create an arrayby using <TT>new &lt;class&gt;[size]</TT>, you delete that array and free all itsmemory with <TT>delete[]</TT>. The brackets signal the compiler that this array isbeing deleted.</P><P>If you leave the brackets off, only the first object in the array will be deleted.You can prove this to yourself by removing the bracket on line 40. If you editedline 21 so that the destructor prints, you should now see only one <TT>CAT</TT> objectdestroyed. Congratulations! You just created a memory leak.<BLOCKQUOTE>	<P><HR><B>DO</B> remember that an array of n items is numbered from zero through n-1. <B>DON'T</B>	write or read past the end of an array. <B>DON'T</B> confuse an array of pointers	with a pointer to an array. <B>DO </B>use array indexing with pointers that point	to arrays. <HR></BLOCKQUOTE><H3 ALIGN="CENTER"><A NAME="Heading37"></A><FONT COLOR="#000077">char Arrays</FONT></H3><P>A string is a series of characters. The only strings you've seen until now havebeen unnamed string constants used in <TT>cout</TT> statements, such as</P><PRE><FONT COLOR="#0066FF">cout &lt;&lt; &quot;hello world.\n&quot;;</FONT></PRE><P>In C++ a string is an array of <TT>char</TT>s ending with a null character. Youcan declare and initialize a string just as you would any other array. For example,</P><PRE><FONT COLOR="#0066FF">char Greeting[] = { `H', `e', `l', `l', `o', ` `, `W','o','r','l','d', `\0' };</FONT></PRE><P>The last character, <TT>`\0'</TT>, is the null character, which many C++ functionsrecognize as the terminator for a string. Although this character-by-character approachworks, it is difficult to type and admits too many opportunities for error. C++ enablesyou to use a shorthand form of the previous line of code. It is</P><PRE><FONT COLOR="#0066FF">char Greeting[] = &quot;Hello World&quot;;</FONT></PRE><P>You should note two things about this syntax:<UL>	<LI>Instead of single quoted characters separated by commas and surrounded by braces,	you have a double-quoted string, no commas, and no braces.	<P>	<LI>You don't need to add the null character because the compiler adds it for you.</UL><P>The string <TT>Hello World</TT> is 12 bytes. <TT>Hello</TT> is 5 bytes, the space1, <TT>World</TT> 5, and the null character 1.</P><P>You can also create uninitialized character arrays. As with all arrays, it isimportant to ensure that you don't put more into the buffer than there is room for.</P><P>Listing 11.8 demonstrates the use of an uninitialized buffer.</P><P><A NAME="Heading38"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 11.8. Fillingan array.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:     //Listing 11.8 char array buffers2:3:     #include &lt;iostream.h&gt;4:5:     int main()6:     {7:        char buffer[80];8:        cout &lt;&lt; &quot;Enter the string: &quot;;9:        cin &gt;&gt; buffer;10:       cout &lt;&lt; &quot;Here's the buffer:  &quot; &lt;&lt; buffer &lt;&lt; endl;11:     return 0;<TT>12: }</TT></FONT><FONT COLOR="#0066FF">Output: Enter the string: Hello WorldHere's the buffer:  Hello</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis: </B></FONT>On line 7, a buffer is declaredto hold 80 characters. This is large enough to hold a 79-character string and a terminatingnull character.<BR>On line 8, the user is prompted to enter a string, which is entered into buffer online 9. It is the syntax of <TT>cin</TT> to write a terminating null to buffer afterit writes the string.</P><P>There are two problems with the program in Listing 11.8. First, if the user entersmore than 79 characters, <TT>cin</TT> writes past the end of the buffer. Second,if the user enters a space, <TT>cin</TT> thinks that it is the end of the string,and it stops writing to the buffer.</P><P>To solve theseproblems, you must call a special method on <TT>cin</TT>: <TT>get()</TT>.<TT>cin.get()</TT> takes three parameters:<DL>	<DD>The buffer to fill<BR>	<BR>	The maximum number of characters to get<BR>	<BR>	The delimiter that terminates input</DL><P><BR>The default delimiter is <TT>newline</TT>. Listing 11.9 illustrates its use.</P><P><A NAME="Heading40"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 11.9. Fillingan array.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:     //Listing 11.9 using cin.get()2:3:     #include &lt;iostream.h&gt;4:5:     int main()6:     {7:        char buffer[80];8:        cout &lt;&lt; &quot;Enter the string: &quot;;9:        cin.get(buffer, 79);       // get up to 79 or newline10:       cout &lt;&lt; &quot;Here's the buffer:  &quot; &lt;&lt; buffer &lt;&lt; endl;11:     return 0;<TT>12: }</TT></FONT><FONT COLOR="#0066FF">Output: Enter the string: Hello WorldHere's the buffer:  Hello World</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis</B></FONT><B>: </B>Line 9 calls the method <TT>get()</TT>of <TT>cin</TT>. The buffer declared in line 7 is passed in as the first argument.The second argument is the maximum number of characters to get. In this case, itmust be 79 to allow for the terminating <TT>null</TT>. There is no need to providea terminating character because the default value of <TT>newline</TT> is sufficient.<BR><TT><BR>cin</TT> and all its variations are covered on Day 17, &quot;The Preprocessor,&quot;when streams are discussed in depth.<H3 ALIGN="CENTER"><A NAME="Heading41"></A><FONT COLOR="#000077">strcpy() and strncpy()</FONT></H3><P>C++ inherits from C a library of functions for dealing with strings. Among themany functions provided are two for copying one string into another: <TT>strcpy()</TT>and <TT>strncpy()</TT>. <TT>strcpy()</TT> copies the entire contents of one stringinto a designated buffer. Listing 11.10 demonstrates the use of <TT>strcpy()</TT>.</P><P><A NAME="Heading42"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 11.10. Usingstrcpy().</B></FONT></P><PRE><FONT COLOR="#0066FF">1:     #include &lt;iostream.h&gt;2:     #include &lt;string.h&gt;3:     int main()4:     {5:        char String1[] = &quot;No man is an island&quot;;6:        char String2[80];7:8:        strcpy(String2,String1);9:10:       cout &lt;&lt; &quot;String1: &quot; &lt;&lt; String1 &lt;&lt; endl;11:       cout &lt;&lt; &quot;String2: &quot; &lt;&lt; String2 &lt;&lt; endl;12:     return 0;<TT>13: }</TT></FONT><FONT COLOR="#0066FF">Output: String1: No man is an islandString2: No man is an island</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>The header file <TT>string.h</TT>is included in line 2. This file contains the prototype of the <TT>strcpy()</TT>function. <TT>strcpy()</TT> takes two character arrays--a destination followed bya source. If the source were larger than the destination, <TT>strcpy()</TT> wouldoverwrite past the end of the buffer.<BR><BR>To protect against this, the standard library also includes <TT>strncpy()</TT>. Thisvariation takes a maximum number of characters to copy. <TT>strncpy()</TT> copiesup to the first null character or the maximum number of characters specified intothe destination buffer.</P><P>Listing 11.11 illustrates the use of<TT> strncpy()</TT>.</P><P><A NAME="Heading43"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 11.11. Usingstrncpy().</B></FONT></P><PRE><FONT COLOR="#0066FF">1:     #include &lt;iostream.h&gt;2:     #include &lt;string.h&gt;3:     int main()4:     {5:        const int MaxLength = 80;6:        char String1[] = &quot;No man is an island&quot;;7:        char String2[MaxLength+1];8:9:10:       strncpy(String2,String1,MaxLength);11:12:       cout &lt;&lt; &quot;String1: &quot; &lt;&lt; String1 &lt;&lt; endl;13:       cout &lt;&lt; &quot;String2: &quot; &lt;&lt; String2 &lt;&lt; endl;14:     return 0;<TT>15: }</TT></FONT><FONT COLOR="#0066FF">Output: String1: No man is an islandString2: No man is an island</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>In line 10, the call to <TT>strcpy()</TT>has been changed to a call to <TT>strncpy()</TT>, which takes a third parameter:the maximum number of characters to copy. The buffer <TT>String2</TT> is declaredto take <TT>MaxLength+1</TT> characters. The extra character is for the null, whichboth <TT>strcpy()</TT> and <TT>strncpy()</TT> automatically add to the end of thestring.<H3 ALIGN="CENTER"><A NAME="Heading44"></A><FONT COLOR="#000077">String Classes</FONT></H3><P>Most C++ compilers come with a class library that includes a large set of classesfor data manipulation. A standard component of a class library is a <TT>String</TT>class.</P><P>C++ inherited the null-terminated string and the library of functions that includes<TT>strcpy()</TT> from C, but these functions aren't integrated into an object-orientedframework. A <TT>String</TT> class provides an encapsulated set of data and functionsfor manipulating that data, as well as accessor functions so that the data itselfis hidden from the clients of the <TT>String</TT> class.</P><P>If your compiler doesn't already provide a <TT>String</TT> class--and perhapseven if it does--you might want to write your own. The remainder of this chapterdiscusses the design and partial implementation of <TT>String</TT> classes.</P><P>At a minimum, a <TT>String</TT> class should overcome the basic limitations ofcharacter arrays. Like all arrays, character arrays are static. You define how largethey are. They always take up that much room in memory even if you don't need itall. Writing past the end of the array is disastrous.</P><P>A good <TT>String</TT> class allocates only as much memory as it needs, and alwaysenough to hold whatever it is given. If it can't allocate enough memory, it shouldfail gracefully.</P><P>Listing 11.12 provides a first approximation of a <TT>String</TT> class.</P><P><A NAME="Heading45"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 11.12. Usinga String class.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:     //Listing 11.122:3:     #include &lt;iostream.h&gt;4:     #include &lt;string.h&gt;5:6:     // Rudimentary string class7:     class String8:     {9:        public:10:          // constructors11:          String();12:          String(const char *const);13:          String(const String &amp;);14:          ~String();15:16:          // overloaded operators17:          char &amp; operator[](unsigned short offset);18:          char operator[](unsigned short offset) const;19:          String operator+(const String&amp;);20:          void operator+=(const String&amp;);21:          String &amp; operator= (const String &amp;);22:23:          // General accessors24:          unsigned short GetLen()const { return itsLen; }25:          const char * GetString() const { return itsString; }26:27:       private:28:          String (unsigned short);         // private constructor29:          char * itsString;30:          unsigned short itsLen;31:    };32:33:    // default constructor creates string of 0 bytes34:    String::String()35:    {36:       itsString = new char[1];37:       itsString[0] = `\0';38:       itsLen=0;39:    }40:41:    // private (helper) constructor, used only by42:    // class methods for creating a new string of43:    // required size. Null filled.44:    String::String(unsigned short len)45:    {46:       itsString = new char[len+1];47:       for (unsigned short i = 0; i&lt;=len; i++)48:          itsString[i] = `\0';49:       itsLen=len;50:    }51:52:    // Converts a character array to a String53:    String::String(const char * const cString)54:    {55:       itsLen = strlen(cString);56:       itsString = new char[itsLen+1];57:       for (unsigned short i = 0; i&lt;itsLen; i++)58:          itsString[i] = cString[i];59:       itsString[itsLen]='\0';60:    }

⌨️ 快捷键说明

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