1, MyString.h header
#pragma once #include <iostream> using namespace std; class MyString { public: MyString(); MyString(const char *p); MyString(const MyString& s); ~MyString(); public: MyString& operator=(const char* p); MyString & operator = ( const MyString & S); char & operator [] ( int index); // overloaded operators left shift, right shift operator, note the difference Friend the ostream & operator << (the ostream & OUT , MyString & S ); istream Friend & operator >> (istream & in , MyString & S); // heavy-duty double equal sign and inequality BOOL operator == ( const char * the p-); BOOL operator = (! const char * the p-); BOOL operator = = ( const MyString & S); BOOL operator ! = ( const MyString & S); // overload greater than and less than the operator int operator <( const char * P); int operator>(const char* p); int operator<(const MyString &s); int operator>(const MyString &s); // pointer to class alone, is called by s1.c_str () char * the c_str () { return m_p; } private: int m_len; char *m_p; };
2, MyString.cpp function implementation file
#define _CRT_SECURE_NO_WARNINGS #include "MyString.h" MyString::MyString() { m_len = 0 ; m_p = new char [m_len + 1 ]; strcpy(m_p, ""); } MyString::MyString(const char *p) { if (p == NULL) { m_len = 0 ; m_p = new char [m_len + 1 ]; strcpy(m_p, ""); } else { m_len = strlen(p); m_p = new char [m_len + 1 ]; strcpy(m_p, p); } } //MyString s3 = s2; MyString::MyString(const MyString& s) { m_len = s.m_len; m_p = new char [m_len + 1 ]; strcpy(m_p, s.m_p); } MyString::~MyString() { if (m_p != NULL) { delete[]m_p; m_p = NULL; m_len = 0 ; } } MyString& MyString::operator=(const char* p) { IF (! = nullptr a M_P) // release the old memory { Delete [] M_P; m_len = 0 ; } IF (p == NULL) // The new memory allocation p { m_len = 0 ; m_p = new char [m_len + 1 ]; strcpy(m_p, ""); } else { m_len = strlen(p); m_p = new char [m_len + 1 ]; strcpy(m_p, p); } return *this; } MyString& MyString::operator=(const MyString &s) { IF (! = nullptr a M_P) // release the old memory { Delete [] M_P; m_len = 0 ; } m_len = strlen(s.m_p); m_p = new char[s.m_len + 1]; strcpy(m_p, s.m_p); return *this; } char& MyString::operator[](int index) { return m_p[index]; } ostream& operator<<(ostream &out, MyString &s) { out << s.m_p; return out; } istream& operator>>(istream &in, MyString &s) { cin >> s.m_p; return in; } bool MyString::operator==(const char* p) { if (p == nullptr) { if (m_len != 0) { return false; } else return true ; } else { if (m_len == strlen(p)) { return !strcmp(m_p, p); } else return false ; } } bool MyString::operator!=(const char *p) { return !(*this == p); } bool MyString::operator==(const MyString& s) { if (m_len != s.m_len) return false; return strcmp(m_p,s.m_p); } bool MyString::operator!=(const MyString& s) { return !(*this == s); } //if(s3<"bb"") int MyString::operator<(const char* p){ return strcmp(m_p, p); } int MyString::operator>(const char* p) { return strcmp(p, m_p); } int MyString::operator<(const MyString &s) { return strcmp(m_p, s.m_p); } int MyString::operator>(const MyString &s) { return strcmp(s.m_p, m_p); }
3, test.cpp test file
#include <iostream> using namespace std; #include "MyString.h" //void main01() //{ // MyString s1; // MyString s2("s2"); // MyString s2_2 = NULL; // MyString s3 = s2; // // MyString s4 = "s4"; // s4 = s2; // s4[1] = '9'; // cout << s4[1] << endl; // cout << s4 << endl;//左移操作符的重载 // // system("pause"); //} void main() { MyString s1; MyString s2("s2"); MyString s3 = NULL; cout << (s2 > "s") << endl; cin >> s2; cout << s2 << endl; system("pause"); }