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

📄 ch18.htm

📁 good book for learning c++ standard language
💻 HTM
📖 第 1 页 / 共 5 页
字号:
67:       // ConstructorCount++;
68:    }
69:
70:    // copy constructor
71:    String::String (const String & rhs)
72:    {
73:       itsLen=rhs.GetLen();
74:       itsString = new char[itsLen+1];
75:       for (int i = 0; i<itsLen;i++)
76:          itsString[i] = rhs[i];
77:       itsString[itsLen] = `\0';
78:       // cout << "\tString(String&) constructor\n";
79:       // ConstructorCount++;
80:    }
81:
82:    // destructor, frees allocated memory
83:    String::~String ()
84:    {
85:       delete [] itsString;
86:       itsLen = 0;
87:       // cout << "\tString destructor\n";
88:    }
89:
90:    // operator equals, frees existing memory
91:    // then copies string and size
92:    String& String::operator=(const String & rhs)
93:    {
94:       if (this == &rhs)
95:          return *this;
96:       delete [] itsString;
97:       itsLen=rhs.GetLen();
98:       itsString = new char[itsLen+1];
99:       for (int i = 0; i<itsLen;i++)
100:         itsString[i] = rhs[i];
101:      itsString[itsLen] = `\0';
102:      return *this;
103:      // cout << "\tString operator=\n";
104:   }
105:
106:   //non constant offset operator, returns
107:   // reference to character so it can be
108:   // changed!
109:   char & String::operator[](int offset)
110:   {
111:      if (offset > itsLen)
112:         return itsString[itsLen-1];
113:      else
114:         return itsString[offset];
115:   }
116:
117:   // constant offset operator for use
118:   // on const objects (see copy constructor!)
119:   char String::operator[](int offset) const
120:   {
121:      if (offset > itsLen)
122:         return itsString[itsLen-1];
123:      else
124:         return itsString[offset];
125:   }
126:
127:   // creates a new string by adding current
128:   // string to rhs
129:   String String::operator+(const String& rhs)
130:   {
131:      int  totalLen = itsLen + rhs.GetLen();
132:      int i,j;
133:      String temp(totalLen);
134:      for ( i = 0; i<itsLen; i++)
135:         temp[i] = itsString[i];
136:      for ( j = 0; j<rhs.GetLen(); j++, i++)
137:         temp[i] = rhs[j];
138:      temp[totalLen]='\0';
139:      return temp;
140:   }
141:   
142:   void String::operator+=(const String& rhs)
143:   {
144:      unsigned short rhsLen = rhs.GetLen();
145:      unsigned short totalLen = itsLen + rhsLen;
146:      String  temp(totalLen);
147:      for (int i = 0; i<itsLen; i++)
148:         temp[i] = itsString[i];
149:      for (int j = 0; j<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& operator<<( ostream& theStream,String& theString)
158:   {
159:       theStream << theString.GetString();
160:       return theStream;
161:   }
162:
163:   class pAddress
164:   {
165:   public:
166:      pAddress(SERVICE theService,
167:               const String& theAddress,
168:               const String& theDisplay):
169:         itsService(theService),
170:         itsAddressString(theAddress),
171:         itsDisplayString(theDisplay)
172:         {}
173:      // pAddress(String, String);
174:      // pAddress();
175:      // pAddress (const pAddress&);
176:      ~pAddress(){}
177:      friend ostream& operator<<( ostream& theStream, pAddress& theAddress);
178:      String& GetDisplayString() { return itsDisplayString; }
179:   private:
180:      SERVICE itsService;
181:      String itsAddressString;
182:      String itsDisplayString;
183:   };
184:
185:   ostream& operator<<( ostream& theStream, pAddress& theAddress)
186:   {
187:       theStream << theAddress.GetDisplayString();
188:       return theStream;
189:   }
190:
191:   class PostMasterMessage
192:   {
193:   public:
194:     // PostMasterMessage();
195:
196:     PostMasterMessage(const pAddress& Sender,
197:                     const pAddress& Recipient,
198:                     const String& Subject,
199:                     const pDate& creationDate);
200:
201:     // other constructors here
202:     // remember to include copy constructor
203:     // as well as constructor from storage
204:     // and constructor from wire format
205:     // Also include constructors from other formats
206:     ~PostMasterMessage(){}
207:
208:     void Edit(); // invokes editor on this message
209:
210:     pAddress& GetSender() const { return itsSender; }
211:     pAddress& GetRecipient() const { return itsRecipient; }
212:     String& GetSubject() const { return itsSubject; }
213:     //  void SetSender(pAddress& );
214:     // other member accessors
215:
216:     // operator methods here, including operator equals
217:     // and conversion routines to turn PostMaster messages
218:     // 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& Sender,
233:         const pAddress& Recipient,
234:         const String& Subject,
235:         const pDate& 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 << "Post Master Message created. \n";
245:     }
246:
247:     void PostMasterMessage::Edit()
248:      {
249:         cout << "PostMasterMessage edit function called\n";
250:      }
251:
252:
253:   int main()
254:   {
255:      pAddress Sender(PostMaster, "jliberty@PostMaster", "Jesse Liberty");
256:      pAddress Recipient(PostMaster, "sl@PostMaster","Stacey Liberty");
257:      PostMasterMessage PostMessage(Sender, Recipient, "Saying Hello", 0);
258:      cout << "Message review... \n";
259:      cout << "From:\t\t" << PostMessage.GetSender() << endl;
260:      cout << "To:\t\t" << PostMessage.GetRecipient() << endl;
261:      cout << "Subject:\t" << PostMessage.GetSubject() << 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 Liberty
To:             Stacey Liberty
Subject:        Saying Hello
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>On line 4, <TT>pDate</TT> is type-defined
to 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 such
as <TT>January 1, 1900</TT>). In this program, this is a placeholder; you would expect
to 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> classes
and 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 only
the fundamental functionality of this class, and you would expect to flesh this out
once your program is better understood. These objects represent essential components
in 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, and
so forth.</P>
<P>It is the <TT>pAddress</TT> object

⌨️ 快捷键说明

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