// mystring.h -- class definition #include <iostream> #include<string> using namespace std; class String { private: char * str; // pointer to string int len; // length of string static int num_strings; // number of objects static const int CINLIM = 80; // cin input limit public: // constructors and other methods String(const char * s); // constructor String(); // default constructor String(const String &); // copy constructor ~String(); // destructor int length () const { return len; } // overloaded operator methods String & operator=(const String &); String & operator=(const char *); char & operator[](int i); const char & operator[](int i) const; // overloaded operator friends friend bool operator<(const String &st, const String &st2); friend bool operator>(const String &st1, const String &st2); friend bool operator==(const String &st, const String &st2); friend ostream & operator<<(ostream & os, const String & st); friend istream & operator>>(istream & is, String & st); // static function static int HowMany(); }; // mystring.cpp -- String class methods // 初始化静态类成员num_strings int String::num_strings = 0; // static method int String::HowMany() { return num_strings; } // class methods,要求动态分配字符串内存空间 String::String(const char * s) { len = strlen(s); if(!s) str = 0; else { str = new char[len+1]; strcpy(str,s); } } String::String() { len = 4; str = new char[1]; str[0] = '\0'; num_strings++; } String::String(const String & st):len(st.len) { if(!st.str) str = 0; else { str = new char[len+1]; strcpy(str,st.str); } } String::~String() // necessary destructor { delete str; } // overloaded operator methods // assign a String to a String String & String::operator=(const String & st) { if (this == &st) return *this; delete [] str; len = st.len; if(!st.str) str = 0; else { str = new char[len + 1]; std::strcpy(str, st.str); } return *this; } // assign a C string to a String String & String::operator=(const char * s) { delete [] str; len = std::strlen(s); if(!s) str = 0; else { str = new char[len + 1]; std::strcpy(str, s); } return *this; } // read-write char access for non-const String char & String::operator[](int i) { if(i >=0 && i <= strlen(str)) return str[i]; } // read-only char access for const String const char & String::operator[](int i) const { return static_cast<const char>(str[i]); //此处与上一空内容一样 } // overloaded operator friends bool operator<(const String &st1, const String &st2) { return (std::strcmp(st1.str, st2.str) < 0); } bool operator>(const String &st1, const String &st2) { return (std::strcmp(st1.str, st2.str) > 0); } bool operator==(const String &st1, const String &st2) { if(strlen(st1.str) != strlen(st2.str)) return false; return strcmp(st1.str,st2.str) ? false:true; } // simple String output ostream & operator<<(ostream & os, const String & st) { os << st.str <<" " << st.len << " " << st.num_strings << std::endl; return os; } // quick and dirty String input istream & operator>>(istream & is, String & st) { is >> st.len >> st.str; return is; } int main() { String s1 = "abd"; String s2 = s1; cout << s2[1]; }