C++實現String類

來源:互聯網
上載者:User

標籤:

  1 #include<iostream>  2 #include<cstring>  3   4 class String  5 {  6     public:  7         String();  8         String(const char *str);  9         String(const String &rhs); 10         ~String(); 11  12         String &operator=(const String &rhs); 13         String operator+(const String &rhs); 14         char operator[](const unsigned int index); 15         bool operator==(const String &rhs); 16         friend std::ostream &operator<<(std::ostream &out, const String &rhs); 17     private: 18         char *m_data; 19 }; 20  21 String::String() 22 { 23     std::cout << "default constructor" << std::endl; 24     m_data = new char[1]; 25     m_data[0] = ‘\0‘; 26 } 27  28 String::String(const char *str) 29 { 30     std::cout << "non-default constructor" << std::endl; 31     if (NULL == str) 32     { 33         m_data = new char[1]; 34         m_data[0] = ‘\0‘; 35     } 36     else 37     { 38         m_data = new char[strlen(str)+1]; 39         strcpy(m_data, str); 40     } 41 } 42  43 String::String(const String &another) 44 { 45     std::cout << "copy constructor" << std::endl; 46     m_data = new char[strlen(another.m_data)+1]; 47     strcpy(m_data, another.m_data); 48 } 49  50 bool String::operator==(const String &rhs) 51 { 52     std::cout << "bool == " << std::endl; 53     int result = strcmp(m_data, rhs.m_data); 54     if (0 == result) 55         return true; 56     else 57         return false; 58 } 59  60 String &String::operator=(const String &rhs) 61 { 62     std::cout << "assign constructor" << std::endl; 63     if (this == &rhs) 64         return *this; 65     delete []m_data; 66     m_data = new char[strlen(rhs.m_data)+1]; 67     strcpy(m_data, rhs.m_data); 68     return *this; 69 } 70  71 String String::operator+(const String &rhs) 72 { 73     std::cout << "+" << std::endl; 74     String newString; 75     if (NULL == rhs.m_data) 76         newString = *this; 77     else if(NULL == m_data) 78         newString = rhs; 79     else 80     { 81         newString.m_data = new char[strlen(rhs.m_data)+strlen(m_data)+1]; 82         strcpy(newString.m_data, m_data); 83         strcat(newString.m_data, rhs.m_data); 84     } 85     return newString; 86 } 87  88 char String::operator[](const unsigned int index) 89 { 90     std::cout << "[]" << std::endl; 91     return m_data[index]; 92 } 93  94 std::ostream &operator<<(std::ostream &out, const String &rhs) 95 { 96     out << rhs.m_data; 97     return out; 98 } 99 100 String::~String()101 {102     std::cout << "destructor" << std::endl;103     delete []m_data;104 }105 106 int main(void)107 {108     const char *p = "hello, world";109     String s = "hello, world"; // 建構函式隱式轉換 調用非預設建構函式110     String s1(p); // 調用非預設建構函式111     String s2 = s1; // 調用非預設建構函式112     String s3; // 調用預設建構函式113     s3 = s1; // 調用賦值建構函式114     String s4 = s3 + s1; // 調用+運算子,同時調用預設建構函式115     bool flag(s1 == s2); // 調用==運算子116     std::cout << s << std::endl;117     std::cout << s1 << std::endl;118     std::cout << s2 << std::endl;119     std::cout << s3 << std::endl;120     std::cout << flag << std::endl;121     char result = s3[1]; // 調用[]運算子122     std::cout << result << std::endl;123     std::cout << s4 << std::endl;124 125     return 0;126 }

 

C++實現String類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.