【经典面试题】C++实现String类

Myth丶恋晨 2021-10-25 13:42 569阅读 0赞

String的默认构造函数,构造函数,拷贝构造函数,析构函数,赋值构造函数

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

发表评论

表情:
评论列表 (有 0 条评论,569人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C++经典面试

    1,关于动态申请内存 答:内存分配方式三种: (1)从静态存储区域分配:内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。 全局变量,st