//File : Mystring.cpp //Author : Andre Long //Due : October 20, 2004 //Description : This file holds the function definitions of overloaded operators // for the "MyString" class #include #include #include"MyString.h" using namespace std; //***************************************************************************** // 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 C-string * // Returns the str member of the calling object * //*************************************************************************** char *MyString::operator =(const char *right) { if(len!=0) { delete[] str; } len = strlen(right); str = new char[len + 1]; strcpy(str, right); return str; } //*************************************************************************** // Overloaded += operator * // Called when operand on the right is another Mystring object * // Concatenates the str member of the 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 operan on the right is a string * // Concatenates the str member of the right to the str member of the calling* // object * // Returns the str member of the calling object * //*************************************************************************** char *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 str; } //*************************************************************************** // Overloaded == operator * // Called when the operand on the right is a MyString object * // Returns true if right.str is the same as str * //*************************************************************************** int 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 * //*************************************************************************** int 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 * //*************************************************************************** int 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 * //*************************************************************************** int 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 str is greater than right.str * //*************************************************************************** bool MyString::operator >(MyString &right) { bool status; if(strcmp(str, right.getValue()) > 0) { status = true; } else { status = false; } return status; } //*************************************************************************** // Overloaded > operator * // Called when the operand on the right is a string * // Returns true id str is greater than right * //*************************************************************************** bool MyString::operator >(const char *right) { bool status; if(strcmp(str, right) > 0) { status = true; } else { status = false; } return status; } //*************************************************************************** // Overloaded < operator * // Called when the operand on the right is a MyString object * // Returns true if str is less than right.str * //*************************************************************************** bool MyString::operator <(MyString &right) { bool status; if(strcmp(str, right.getValue()) < 0) { status = true; } else { status = false; } return status; } //*************************************************************************** // 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) { bool status; if(strcmp(str, right) < 0 ) { status = true; } else { status = false; } return status; } //*************************************************************************** // Overloaded >= operator * // Called when the operand on the right is a MyString object * // Returns true if str is greater than or equal to right.str * //*************************************************************************** bool MyString::operator >=(MyString &right) { bool status; if(strcmp(str, right.getValue()) >= 0) { status = true; } else { status = false; } return status; } //*************************************************************************** // 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) { bool status; if(strcmp(str, right) >=0) { status = true; } else { status = false; } return status; } //*************************************************************************** // Overloaded <= operator * // Called when the operand on the right is a MyString object * // Returns true if str is greater than or equal to right * //*************************************************************************** bool MyString::operator <=(MyString &right) { bool status; if(strcmp(str, right.getValue()) <= 0) { status = true; } else { status = false; } return status; } //*************************************************************************** // 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) { bool status; if(strcmp(str, right) <=0) { status = true; } else { status = false; } return status; } //*************************************************************************** // 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) { string buffer; getline(strm, buffer); obj.str = new char[buffer.length()+1]; obj.len = buffer.length(); strcpy(obj.str, buffer.c_str()); return strm; }