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

📄 ch18.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
87:       // cout &lt;&lt; &quot;\tString destructor\n&quot;;88:    }89:90:    // operator equals, frees existing memory91:    // then copies string and size92:    String&amp; String::operator=(const String &amp; rhs)93:    {94:       if (this == &amp;rhs)95:          return *this;96:       delete [] itsString;97:       itsLen=rhs.GetLen();98:       itsString = new char[itsLen+1];99:       for (int i = 0; i&lt;itsLen;i++)100:         itsString[i] = rhs[i];101:      itsString[itsLen] = `\0';102:      return *this;103:      // cout &lt;&lt; &quot;\tString operator=\n&quot;;104:   }105:106:   //non constant offset operator, returns107:   // reference to character so it can be108:   // changed!109:   char &amp; String::operator[](int offset)110:   {111:      if (offset &gt; itsLen)112:         return itsString[itsLen-1];113:      else114:         return itsString[offset];115:   }116:117:   // constant offset operator for use118:   // on const objects (see copy constructor!)119:   char String::operator[](int offset) const120:   {121:      if (offset &gt; itsLen)122:         return itsString[itsLen-1];123:      else124:         return itsString[offset];125:   }126:127:   // creates a new string by adding current128:   // string to rhs129:   String String::operator+(const String&amp; rhs)130:   {131:      int  totalLen = itsLen + rhs.GetLen();132:      int i,j;133:      String temp(totalLen);134:      for ( i = 0; i&lt;itsLen; i++)135:         temp[i] = itsString[i];136:      for ( j = 0; j&lt;rhs.GetLen(); j++, i++)137:         temp[i] = rhs[j];138:      temp[totalLen]='\0';139:      return temp;140:   }141:   142:   void String::operator+=(const String&amp; rhs)143:   {144:      unsigned short rhsLen = rhs.GetLen();145:      unsigned short totalLen = itsLen + rhsLen;146:      String  temp(totalLen);147:      for (int i = 0; i&lt;itsLen; i++)148:         temp[i] = itsString[i];149:      for (int j = 0; j&lt;rhs.GetLen(); j++, i++)150:         temp[i] = rhs[i-itsLen];151:      temp[totalLen]='\0';152:      *this = temp;153:   }154:155:   // int String::ConstructorCount = 0;156:157:   ostream&amp; operator&lt;&lt;( ostream&amp; theStream,String&amp; theString)158:   {159:       theStream &lt;&lt; theString.GetString();160:       return theStream;161:   }162:163:   class pAddress164:   {165:   public:166:      pAddress(SERVICE theService,167:               const String&amp; theAddress,168:               const String&amp; theDisplay):169:         itsService(theService),170:         itsAddressString(theAddress),171:         itsDisplayString(theDisplay)172:         {}173:      // pAddress(String, String);174:      // pAddress();175:      // pAddress (const pAddress&amp;);176:      ~pAddress(){}177:      friend ostream&amp; operator&lt;&lt;( ostream&amp; theStream, pAddress&amp; theAddress);178:      String&amp; GetDisplayString() { return itsDisplayString; }179:   private:180:      SERVICE itsService;181:      String itsAddressString;182:      String itsDisplayString;183:   };184:185:   ostream&amp; operator&lt;&lt;( ostream&amp; theStream, pAddress&amp; theAddress)186:   {187:       theStream &lt;&lt; theAddress.GetDisplayString();188:       return theStream;189:   }190:191:   class PostMasterMessage192:   {193:   public:194:     // PostMasterMessage();195:196:     PostMasterMessage(const pAddress&amp; Sender,197:                     const pAddress&amp; Recipient,198:                     const String&amp; Subject,199:                     const pDate&amp; creationDate);200:201:     // other constructors here202:     // remember to include copy constructor203:     // as well as constructor from storage204:     // and constructor from wire format205:     // Also include constructors from other formats206:     ~PostMasterMessage(){}207:208:     void Edit(); // invokes editor on this message209:210:     pAddress&amp; GetSender() const { return itsSender; }211:     pAddress&amp; GetRecipient() const { return itsRecipient; }212:     String&amp; GetSubject() const { return itsSubject; }213:     //  void SetSender(pAddress&amp; );214:     // other member accessors215:216:     // operator methods here, including operator equals217:     // and conversion routines to turn PostMaster messages218:     // into messages of other formats.219:220:   private:221:      pAddress itsSender;222:      pAddress itsRecipient;223:      String  itsSubject;224:      pDate itsCreationDate;225:      pDate itsLastModDate;226:      pDate itsReceiptDate;227:      pDate itsFirstReadDate;228:      pDate itsLastReadDate;229:   };230:231:   PostMasterMessage::PostMasterMessage(232:         const pAddress&amp; Sender,233:         const pAddress&amp; Recipient,234:         const String&amp; Subject,235:         const pDate&amp; creationDate):236:         itsSender(Sender),237:         itsRecipient(Recipient),238:         itsSubject(Subject),239:         itsCreationDate(creationDate),240:         itsLastModDate(creationDate),241:         itsFirstReadDate(0),242:         itsLastReadDate(0)243:     {244:       cout &lt;&lt; &quot;Post Master Message created. \n&quot;;245:     }246:247:     void PostMasterMessage::Edit()248:      {249:         cout &lt;&lt; &quot;PostMasterMessage edit function called\n&quot;;250:      }251:252:253:   int main()254:   {255:      pAddress Sender(PostMaster, &quot;jliberty@PostMaster&quot;, &quot;Jesse Liberty&quot;);256:      pAddress Recipient(PostMaster, &quot;sl@PostMaster&quot;,&quot;Stacey Liberty&quot;);257:      PostMasterMessage PostMessage(Sender, Recipient, &quot;Saying Hello&quot;, 0);258:      cout &lt;&lt; &quot;Message review... \n&quot;;259:      cout &lt;&lt; &quot;From:\t\t&quot; &lt;&lt; PostMessage.GetSender() &lt;&lt; endl;260:      cout &lt;&lt; &quot;To:\t\t&quot; &lt;&lt; PostMessage.GetRecipient() &lt;&lt; endl;261:      cout &lt;&lt; &quot;Subject:\t&quot; &lt;&lt; PostMessage.GetSubject() &lt;&lt; endl;262:     return 0;<TT>263: }</TT></FONT></PRE><BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>WARNING:</B></FONT><B> </B>If you receive a &quot;can't	convert&quot; error, remove the <TT>const</TT> keywords from lines 210-212. <HR></BLOCKQUOTE><PRE><FONT COLOR="#0066FF">Output: Post Master Message created.Message review...From:           Jesse LibertyTo:             Stacey LibertySubject:        Saying Hello</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis: </B></FONT>On line 4, <TT>pDate</TT> is type-definedto be an <TT>unsigned long</TT>. It is not uncommon for dates to be stored as a <TT>long</TT>integer (typically as the number of seconds since an arbitrary starting date suchas <TT>January 1, 1900</TT>). In this program, this is a placeholder; you would expectto eventually turn <TT>pDate</TT> into a real class.<BR><BR>On line 5, an enumerated constant, <TT>SERVICE</TT>, is defined to allow the <TT>Address</TT>objects to keep track of what type of address they are, including PostMaster, CompuServe,and so forth.</P><P>Lines 7-161 represent the interface to and implementation of <TT>String</TT>,along much the same lines as you have seen in previous chapters. The <TT>String</TT>class is used for a number of member variables in all of the <TT>Message</TT> classesand various other classes used by messages, and as such it is pivotal in your program.A full and robust <TT>String</TT> class will be essential to making your <TT>Message</TT>classes complete.</P><P>On lines 162-183, the <TT>pAddress</TT> class is declared. This represents onlythe fundamental functionality of this class, and you would expect to flesh this outonce your program is better understood. These objects represent essential componentsin every message: both the sender's address and that of the recipient. A fully functional<TT>pAddress</TT> object will be able to handle forwarding messages, replies, andso forth.</P><P>It is the <TT>pAddress</TT> object's job to keep track of the display string aswell as the internal routing string for its service. One open question for your designis whether there should be one <TT>pAddress</TT> object or if this should be subclassedfor each service type. For now, the service is tracked as an enumerated constant,which is a member variable of each <TT>pAddress</TT> object.</P><P>Lines 191-229 show the interface to the <TT>PostMasterMessage</TT> class. In thisparticular listing, this class stands on its own, but very soon you'll want to makethis part of its inheritance hierarchy. When you do redesign this to inherit from<TT>Message</TT>, some of the member variables may move into the base classes, andsome of the member functions may become overrides of base class methods.</P><P>A variety of other constructors, accessor functions, and other member functionswill be required to make this class fully functional. Note that what this listingillustrates is that your class does not have to be 100 percent complete before youcan write a simple driver program to test some of your assumptions.</P><P>On lines 247-250, the <TT>Edit()</TT> function is &quot;stubbed out&quot; in justenough detail to indicate where the editing functionality will be put once this classis fully operational.</P><P>Lines 253-263 represent the driver program. Currently this program does nothingmore than exercise a few of the accessor functions and the <TT>operator&lt;&lt;</TT>overload. Nonetheless, this gives you the starting point for experimenting with <TT>PostMasterMessage</TT>sand a framework within which you can modify these classes and examine the impact.<H3 ALIGN="CENTER"><A NAME="Heading34"></A><FONT COLOR="#000077">Summary</FONT></H3><P>Today you saw a review of how to bring together many of the elements of C++ syntaxand apply them to object-oriented analysis, design, and programming. The developmentcycle is not a linear progression from clean analysis through design and 

⌨️ 快捷键说明

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