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

📄 driverprogram.cpp

📁 24学时攻克C++光盘源代码 深入浅出 方便实用
💻 CPP
字号:
 // Listing 22.3

 #include <iostream>

 #include <string.h>

 

 typedef unsigned long pDate;

 enum SERVICE { PostMaster, Interchange, 

     CompuServe, Prodigy, AOL, Internet };

 

 class String

 {

 public:

     // constructors

     String();

     String(const char *const);

     String(const String &);

     ~String();

 

     // overloaded operators

     char & operator[](int offset);

     char operator[](int offset) const;

     String operator+(const String&);

     void operator+=(const String&);

     String & operator= (const String &);

     friend std::ostream & operator<<

 		(std::ostream& theStream, String& theString);

     // General accessors

     int GetLen()const { return itsLen; }

     const char * GetString() const { return itsString; }

     // static int ConstructorCount;

 

 private:

     String (int);         // private constructor

     char * itsString;

     int itsLen;

 };

 

 // default constructor creates string of 0 bytes

 String::String()

 {

     itsString = new char[1];

     itsString[0] = '\0';

     itsLen=0;

     // std::cout << "\tDefault string constructor\n";

     // ConstructorCount++;

 }

 

 // private (helper) constructor, used only by

 // class methods for creating a new string of

 // required size.  Null filled.

 String::String(int len)

 {

     itsString = new char[len+1];

     int i;

     for ( i = 0; i<=len; i++)

         itsString[1] = '\0';

     itsLen=len;

     // std::cout << "\tString(int) constructor\n";

     // ConstructorCount++;

 }

 

 // Converts a character array to a String

 String::String(const char * const cString)

 {

     itsLen = strlen(cString);

     itsString = new char[itsLen+1];

     int i;

     for ( i = 0; i<itsLen; i++)

         itsString[i] = cString[i];

     itsString[itsLen]='\0';

     // std::cout << "\tString(char*) constructor\n";

     // ConstructorCount++;

 }

 

 // copy constructor

 String::String (const String & rhs)

 {

     itsLen=rhs.GetLen();

     itsString = new char[itsLen+1];

     int i;

     for (i = 0; i<itsLen;i++)

         itsString[i] = rhs[i];

     itsString[itsLen] = '\0';

     // std::cout << "\tString(String&) constructor\n";

     // ConstructorCount++;

 }

 

 // destructor, frees allocated memory

 String::~String ()

 {

     delete [] itsString;

     itsLen = 0;

     // std::cout << "\tString destructor\n";

 }

 

 String& String::operator=(const String & rhs)

 {

     if (this == &rhs)

         return *this;

     delete [] itsString;

     itsLen=rhs.GetLen();

     itsString = new char[itsLen+1];

     int i;

     for (i = 0; i<itsLen;i++)

         itsString[i] = rhs[i];

     itsString[itsLen] = '\0';

     return *this;

     // std::cout << "\tString operator=\n";

 }

 

 //non constant offset operator, returns

 // reference to character so it can be changed

 char & String::operator[](int offset)

 {

     if (offset > itsLen)

         return itsString[itsLen-1];

     else

         return itsString[offset];

 }

 

 // constant offset operator for use

 // on const objects (see copy constructor!)

 char String::operator[](int offset) const

 {

     if (offset > itsLen)

         return itsString[itsLen-1];

     else

         return itsString[offset];

 }

 

 // creates a new string by adding current

 // string to rhs

 String String::operator+(const String& rhs)

 {

     int  totalLen = itsLen + rhs.GetLen();

     String temp(totalLen);

     int i,j;

     for (i = 0; i<itsLen; i++)

         temp[i] = itsString[i];

     for (j = 0; j<rhs.GetLen(); j++, i++)

         temp[i] = rhs[j];

     temp[totalLen]='\0';

     return temp;

 }

 

 // changes current string, returns nothing

 void String::operator+=(const String& rhs)

 {

     int rhsLen = rhs.GetLen();

     int totalLen = itsLen + rhsLen;

     String  temp(totalLen);

     int i,j;

     for ( i = 0; i<itsLen; i++)

         temp[i] = itsString[i];

     for ( j = 0; j<rhs.GetLen(); j++, i++)

         temp[i] = rhs[i-itsLen];

     temp[totalLen]='\0';

     *this = temp;

 }

 

 // int String::ConstructorCount = 0;

 

 std::ostream& operator<<( 

                          std::ostream& theStream,

                         String& theString)

 {

     theStream << theString.GetString();

     return theStream;

 }

 

 class pAddress

 {

 public:

     pAddress(SERVICE theService,

             const String& theAddress,

             const String& theDisplay):

         itsService(theService),

         itsAddressString(theAddress),

         itsDisplayString(theDisplay)

     {}

     // pAddress(String, String);

     // pAddress();

     // pAddress (const pAddress&);

     ~pAddress(){}

     friend std::ostream& operator<<( 

 	  std::ostream& theStream, pAddress& theAddress);

     String& GetDisplayString() 

 	{ return itsDisplayString; }

 private:

     SERVICE itsService;

     String itsAddressString;

     String itsDisplayString;

 };

 

 std::ostream& operator<<

   ( std::ostream& theStream, pAddress& theAddress)

 {

     theStream << theAddress.GetDisplayString();

     return theStream;

 }

 

 class PostMasterMessage

 {

 public:

 //  PostMasterMessage();

 

     PostMasterMessage(const pAddress& Sender,

                       const pAddress& Recipient,

                       const String& Subject,

                       const pDate& creationDate);

 

     ~PostMasterMessage(){}

 

     void Edit(); // invokes editor on this message

 

     pAddress& GetSender()  { return itsSender; }

     pAddress& GetRecipient()  { return itsRecipient; }

     String& GetSubject()  { return itsSubject; }

     //  void SetSender(pAddress& );

     // other member accessors

 

     // operator methods here, including operator equals

     // and conversion routines to turn PostMaster messages

     // into messages of other formats.

 

 private:

     pAddress itsSender;

     pAddress itsRecipient;

     String  itsSubject;

     pDate itsCreationDate;

     pDate itsLastModDate;

     pDate itsReceiptDate;

     pDate itsFirstReadDate;

     pDate itsLastReadDate;

 };

 

 PostMasterMessage::PostMasterMessage(

     const pAddress& Sender,

     const pAddress& Recipient,

     const String& Subject,

     const pDate& creationDate):

     itsSender(Sender),

     itsRecipient(Recipient),

     itsSubject(Subject),

     itsCreationDate(creationDate),

     itsLastModDate(creationDate),

     itsFirstReadDate(0),

     itsLastReadDate(0)

 {

     std::cout << "Post Master Message created. \n";

 }

 

 void PostMasterMessage::Edit()

 {

     std::cout << "PostMasterMessage edit function called\n";

 }

 

 

 int main()

 {

     pAddress Sender(

         PostMaster, "jliberty@PostMaster", "Jesse Liberty");

     pAddress Recipient(

         PostMaster, "sliberty@PostMaster","Stacey Liberty");

     PostMasterMessage PostMasterMessage(

         Sender, Recipient, "Saying Hello", 0);

     std::cout << "Message review... \n";

     std::cout << "From:\t\t" 

         << PostMasterMessage.GetSender() << std::endl;

     std::cout << "To:\t\t" 

         << PostMasterMessage.GetRecipient() << std::endl;

     std::cout << "Subject:\t" 

         << PostMasterMessage.GetSubject() << std::endl;

     return 0;

 }

⌨️ 快捷键说明

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