ex1604.cpp

来自「teach yourself C++ in 21 days 第五版」· C++ 代码 · 共 52 行

CPP
52
字号
#include <iostream.h>
#include <string.h>

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 ostream& operator<<( ostream& theStream,String& theString);
      friend istream& operator>>( istream& 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;
      unsigned short itsLen;
};

ostream& operator<<( ostream& theStream,String& theString)
{
    theStream << theString.GetString();
    return theStream;
}

istream& operator>>( istream& theStream,String& theString)
{
    theStream >> theString.itsString;
    return theStream;
}

int main()
{
   String theString("Hello world.");
   cout << theString;
   cin >> theString;
   cout << theString;
   return 0;
}

⌨️ 快捷键说明

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