1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
namespace cs_mystring
{
class MyString
{
public:
MyString();
MyString(const char* inString);
MyString(const MyString& right);
~MyString();
friend std::ostream& operator << (std::ostream& out, const MyString& right);
friend std::istream& operator >> (std::istream& strm, MyString& right);
char& operator [] (int index);
char operator [] (int index) const;
friend bool operator < (const MyString& left, const MyString& right);
friend bool operator <= (const MyString& left, const MyString& right);
friend bool operator > (const MyString& left, const MyString& right);
friend bool operator >= (const MyString& left, const MyString& right);
friend bool operator == (const MyString& left, const MyString& right);
friend bool operator != (const MyString& left, const MyString& right);
MyString operator = (const MyString& right);
size_t length()const;
void read(std::istream& readString, char stopRead);
friend const MyString operator + (const MyString& left, const MyString& right);
private:
char* ptr;
static const int MAX_INPUT_SIZE = 127;
};
}
// in main
// test works on
MyString s1("hello "), s2("world");
cout << s1 + s2 << endl << endl;
}
MyString::MyString()
{
ptr = new char[1];
strcpy(ptr, "");
}
MyString::MyString(const char* inString)
{
ptr = new char[strlen(inString) + 1];
strcpy(ptr, inString);
}
MyString::~MyString()
{
delete[] ptr;
}
MyString MyString::operator = (const MyString & right)
{
if (this != &right) {
delete[] ptr;
ptr = new char[strlen(right.ptr) + 1];
strcpy(ptr, right.ptr);
}
return *this;
}
|