// This is the header file for the MyString class as defined in // EECS 370. This follows closely the code written in the Drake // text "Object Oriented Programming with C++ and Smalltalk" on // page 697. #ifndef MYSTRING_C #define MYSTRING_C #include #include using namespace std; class MyString { // friend functions updated 2/16/2000 friend bool operator== (const MyString&, const MyString&); friend ostream& operator<<(ostream&, const MyString&); private: char * str_; int length_; MyString (int); public: MyString (); MyString (char); MyString (const char *); MyString (const MyString &); ~MyString (); int length (); int indexOf (char, int = 0); bool isSubstring (const MyString&); MyString concat (const MyString&); void printStr (); // updated 2/16/2000 MyString& operator=(const MyString&); MyString operator! () const; char& operator[] (int); char operator[] (int) const; }; // The inline methods of the class are written in the .h file. // Most methods of this class could be inline methods; however, // only a few are written as inline to show how to write both // inline and regular mehtods. inline MyString::MyString() { length_ = 0; str_ = new char [1]; str_[0] = '\0'; } inline MyString::MyString(char ch) { length_ = 1; str_ = new char [2]; str_[0] = ch; str_[1] = '\0'; } inline int MyString::length() { return length_; } inline void MyString::printStr() { cout << "Length: " << length_ << ", Value: \"" << str_ <<"\""; } #endif