#include "mystring.h" //************************************************** // Constructor to initialize the str member * // with a string constant. * //************************************************** MyString::MyString(char *sptr) { len = strlen(sptr); str = new char[len + 1]; strcpy(str, sptr); } //************************************************* // Copy constructor. * //************************************************* MyString::MyString(MyString &right) { str = new char[right.length() + 1]; strcpy(str, right.getValue()); len = right.length(); } //************************************************ // Overloaded = operator. Called when operand * // on the right is another MyString object. * // Returns the calling object. * //************************************************ MyString MyString::operator=(MyString &right) { if (len != 0) delete [] str; str = new char[right.length() + 1]; strcpy(str, right.getValue()); len = right.length(); return *this; } //*********************************************** // Overloaded = operator. Called when operand * // on the right is a string. * // Returns the calling object. * //*********************************************** MyString MyString::operator=(const char *right) { if (len != 0) delete [] str; len = strlen(right); str = new char[len + 1]; strcpy(str, right); return *this; } //************************************************** // Overloaded += operator. Called when operand * // on the right is another MyString object. * // Concatenates the str member of right to the * // str member of the calling object. * // Returns the calling object. * //************************************************** MyString MyString::operator+=(MyString &right) { char *temp = str; str = new char[strlen(str) + right.length() + 1]; strcpy(str, temp); strcat(str, right.getValue()); if (len != 0) delete [] temp; len = strlen(str); return *this; } //**************************************************** // Overloaded += operator. Called when operand * // on the right is a string. Concatenates the * // string right to the str member of * // the calling object. * // Returns the the calling object. * //**************************************************** MyString MyString::operator+=(const char *right) { char *temp = str; str = new char[strlen(str) + strlen(right) + 1]; strcpy(str, temp); strcat(str, right); if (len != 0) delete [] temp; return *this; } //********************************************************* // Overloaded == operator. * // Called when the operand on the right is a MyString * // object. Returns true if right.str is the same as str. * //********************************************************* bool MyString::operator==(MyString &right) { return !strcmp(str, right.getValue()); } //****************************************************** // Overloaded == operator. * // Called when the operand on the right is a string. * // Returns true if right is the same as str. * //****************************************************** bool MyString::operator==(const char *right) { return !strcmp(str, right); } //********************************************************* // Overloaded != operator. * // Called when the operand on the right is a MyString * // object. Returns true if right.str is not equal to str. * //********************************************************* bool MyString::operator!=(MyString &right) { return strcmp(str, right.getValue()); } //**************************************************** // Overloaded != operator. * // Called when the operand on the right is a string. * // Returns true if right is not equal to str. * //**************************************************** bool MyString::operator!=(const char *right) { return strcmp(str, right); } //***************************************************** // Overloaded != operator. * // Called when the operand on the right is a string. * // Returns true if str is greater than right.getValue.* //***************************************************** bool MyString::operator>(MyString &right) { if (strcmp(str, right.getValue()) > 0) return true; else return false; } //**************************************************** // Overloaded > operator. * // Called when the operand on the right is a string. * // Returns true if str is greater than right. * //**************************************************** bool MyString::operator>(const char *right) { if (strcmp(str, right) > 0) return true; else return false; } //*********************************************************** // Overloaded < operator. * // Called when the operand on the right is a MyString * // object. Returns true if str is less than right.getValue. * //*********************************************************** bool MyString::operator<(MyString &right) { if (strcmp(str, right.getValue()) < 0) return true; else return false; } //**************************************************** // Overloaded < operator. * // Called when the operand on the right is a string. * // Returns true if str is less than right. * //**************************************************** bool MyString::operator<(const char *right) { if (strcmp(str, right) < 0) return true; else return false; } //***************************************************** // Overloaded >= operator. * // Called when the operand on the right is a MyString * // object. Returns true if str is greater than or * // equal to right.getValue. * //***************************************************** bool MyString::operator>=(MyString &right) { if (strcmp(str, right.getValue()) >= 0) return true; else return false; } //********************************************************* // Overloaded >= operator. * // Called when the operand on the right is a string. * // Returns true if str is greater than or equal to right. * //********************************************************* bool MyString::operator>=(const char *right) { if (strcmp(str, right) >= 0) return true; else return false; } //********************************************************** // Overloaded <= operator. * // Called when the operand on the right is a MyString * // object. Returns true if right.str is less than or equal * // to str. * //********************************************************** bool MyString::operator<=(MyString &right) { if (strcmp(str, right.getValue()) <= 0) return true; else return false; } //******************************************************* // Overloaded <= operator. * // Called when the operand on the right is a string. * // Returns true if str is less than or equal to right. * //******************************************************* bool MyString::operator<=(const char *right) { if (strcmp(str, right) <= 0) return true; else return false; } //************************************************* // Overloaded stream insertion operator (<<). * //************************************************* ostream &operator<<(ostream &strm, MyString &obj) { strm << obj.str; return strm; } //************************************************* // Overloaded stream extraction operator (>>). * //************************************************* istream &operator>>(istream &strm, MyString &obj) { // Buffer to read string in. const int MAX_LEN = 256; char buffer[MAX_LEN]; // Read the string. strm.getline(buffer, MAX_LEN); // Invoke the convert constructor. obj = buffer; return strm; }