To review c ++ knowledge, the realization of a simple string class, the class name CMyString
Environment Description: windows 7 64 Wei
Development tools: Visual Studio 2015
Header CMyString class CMyString.h
. 1 #include <the iostream> 2 . 3 #ifndef __C_MY_STRING__ . 4 #define __C_MY_STRING__ . 5 . 6 class CMyString . 7 { . 8 public : . 9 // default constructor 10 CMyString (); . 11 // constructor arguments 12 is CMyString ( const char * STR ); 13 // copy constructor 14 CMyString ( const CMyString & ); 15 // destructor 16 ~ CMyString (); . 17 18 is // overloaded assignment operator . 19 CMyString & operator = ( const CMyString & ); 20 is CMyString & operator = ( const char * ); 21 is // overloaded [] operator (modifiable) 22 is char & operator [] ( const int ); 23 // overloaded [] operator (not modified) 24 const char & operator [] ( const int ) const ; 25 // overload operator == 26 is BOOL operator == ( const CMyString &) const ; 27 // overloaded = operator! 28 BOOL operator = (! const CMyString &) const ; 29 // Overload> operator 30 BOOL operator > ( const CMyString &) const ; 31 is // overload <operator 32 BOOL operator <( const CMyString &) const ; 33 is // overloaded> = operator 34 is BOOL operator > = ( const& CMyString) const ; 35 // Overloaded> = operator 36 BOOL operator <= ( const CMyString &) const ; 37 [ // overloaded << operator 38 is Friend the ostream & STD :: operator << (STD :: the ostream &, const CMyString & ); 39 Private : 40 char * m_pdata; 41 is }; 42 is 43 is #endif // __ C_MY_STRING__!
CMyString class implementation file CMyString.cpp
. 1 #include " CMyString.h " 2 . 3 CMyString :: CMyString () . 4 { . 5 // Create an empty data, one byte space . 6 m_pdata = new new char [ . 1 ]; . 7 m_pdata [ 0 ] = ' \ 0 ' ; . 8 } . 9 10 CMyString :: CMyString ( const char * STR) . 11 { 12 is IF (STR) 13 is { 14 int len =:: strlen STD (STR); 15 m_pdata = new new char [len + . 1 ]; 16 STD :: strncpy (m_pdata, STR, len); . 17 m_pdata [len] = ' \ 0 ' ; 18 is } . 19 the else 20 is { 21 is / / Create an empty data, one byte space 22 is m_pdata = new new char [ . 1 ]; 23 is m_pdata [ 0 ] = ' \ 0 ' ; 24 } 25 } 26 is 27 CMyString::CMyString(const CMyString & inString) 28 { 29 int len = std::strlen(inString.m_pdata); 30 m_pdata = new char[len +1]; 31 std::strncpy(m_pdata, inString.m_pdata, len); 32 m_pdata[len] = '\0'; 33 } 34 35 CMyString::~CMyString() 36 { 37 delete[] m_pdata; 38 m_pdata = nullptr; 39 } 40 41 is CMyString CMyString :: & operator = ( const CMyString & InString) 42 is { 43 is // If the same object, without processing returns 44 is IF ( the this == & InString) 45 { 46 is return * the this ; 47 } 48 49 // use the reference objects by creating a temporary copy constructor 50 CMyString tmpString (InString); 51 is // modify data pointer, when the function terminates, tmpString object expires, the destructor will automatically call, the original data of the current object address freed 52 char * tmpdata = tmpString.m_pdata; 53 tmpString.m_pdata = m_pdata; 54 m_pdata = tmpData; 55 56 return *this; 57 } 58 59 CMyString & CMyString::operator=(const char * str) 60 { 61 delete m_pdata; 62 if (str) 63 { 64 int len = std::strlen(str); 65 m_pdata = new char[len + 1]; 66 :: strncpy STD (m_pdata, STR, len); 67 m_pdata [len] = ' \ 0 ' ; 68 } 69 the else 70 { 71 is // Create an empty data, one byte space 72 m_pdata = new new char [ . 1 ]; 73 is m_pdata [ 0 ] = ' \ 0 ' ; 74 } 75 return * the this ; 76 } 77 78 char & CMyString :: operator [] (const int index) 79 { 80 return m_pdata[index]; 81 } 82 83 const char& CMyString::operator[](const int index) const 84 { 85 return m_pdata[index]; 86 } 87 88 bool CMyString::operator==(const CMyString & inString) const 89 { 90 return !std::strcmp(m_pdata, inString.m_pdata); 91 } 92 93 bool CMyString::operator!=(const CMyString & inString) const 94 { 95 return std::strcmp(m_pdata, inString.m_pdata); 96 } 97 98 bool CMyString::operator>(const CMyString & inString) const 99 { 100 return (std::strcmp(m_pdata, inString.m_pdata) > 0); 101 } 102 103 bool CMyString::operator<(const CMyString & inString) const 104 { 105 return (std::strcmp(m_pdata, inString.m_pdata) < 0); 106 } 107 108 bool CMyString::operator>=(const CMyString & inString) const 109 { 110 return (std::strcmp(m_pdata, inString.m_pdata) >= 0); 111 } 112 113 bool CMyString::operator<=(const CMyString & inString) const 114 { 115 return (std::strcmp(m_pdata, inString.m_pdata) <= 0); 116 } 117 118 std::ostream & operator<<(std::ostream & os, const CMyString & instring) 119 { 120 os << instring.m_pdata; 121 return os; 122 }
CMystring class test file testCMyString.cpp
1 #include <iostream> 2 #include "CMyString.h" 3 4 using namespace std; 5 int main() 6 { 7 CMyString myString1("abc"); 8 CMyString myString2("bcd"); 9 CMyString myString3(myString2); 10 myString3[2] = 'e'; 11 12 cout << myString1 << "\t" << myString2 << "\t" << myString3 << endl; 13 cout << myString1[2] << endl; 14 cout << (myString1 != myString2) << endl; 15 cout << (myString1 == myString2) << endl; 16 cout << (myString1 < myString2) << endl; 17 cout << (myString1 <= myString2) << endl; 18 cout << (myString1 > myString2) << endl; 19 cout << (myString1 >= myString2) << endl; 20 return 0; 21 }
Test Results: