// This is the header file for the String 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 STRING_C #define STRING_C #include #include using namespace std; class String { // friend functions updated 2/16/2000 friend bool operator== (const String&, const String&); friend ostream& operator<<(ostream&, const String&); private: char * str_; int length_; String (int); public: String (); String (char); String (const char *); String (const String &); ~String (); int length (); int indexOf (char, int = 0); bool isSubstring (const String&); String concat (const String&); void printStr (); // updated 2/16/2000 String& operator=(const String&); }; // 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 String::String() { length_ = 0; str_ = new char [1]; str_[0] = '\0'; } inline String::String(char ch) { length_ = 1; str_ = new char [2]; str_[0] = ch; str_[1] = '\0'; } inline int String::length() { return length_; } inline void String::printStr() { cout << "Length: " << length_ << ", Value: \"" << str_ <<"\""; } #endif