• C++ Day4


    目录

    仿照string类,完成myString 类

    思维导图


    仿照string类,完成myString 类

    1. #include
    2. #include
    3. using namespace std;
    4. class myString
    5. {
    6. private:
    7. char *str; //记录c风格的字符串
    8. int size; //记录字符串的实际长度
    9. public:
    10. //无参构造
    11. myString():size(10)
    12. {
    13. str = new char[size]; //构造出一个长度为10的字符串
    14. strcpy(str,""); //赋值为空串
    15. cout<<"无参构造"<
    16. }
    17. //有参构造
    18. myString(const char *s) //string s("hello world")
    19. {
    20. size = strlen(s);
    21. str = new char[size+1];
    22. strcpy(str, s);
    23. cout<<"有参构造"<
    24. }
    25. //拷贝构造
    26. myString(const myString &other)
    27. {
    28. this->str=new char[other.size+1];
    29. strcpy(str,other.str);
    30. this->size=other.size;
    31. cout<<"拷贝构造"<
    32. }
    33. //析构函数
    34. ~myString()
    35. {
    36. delete str;
    37. cout<<"析构函数"<
    38. }
    39. //拷贝赋值函数
    40. myString &operator=(const myString &other)
    41. {
    42. if(this!=&other)
    43. {
    44. this->size=other.size;
    45. if(this->str!=NULL)
    46. {
    47. delete this->str;
    48. }
    49. this->str=new char[other.size+1];
    50. strcpy(str,other.str);
    51. cout<<"拷贝赋值"<
    52. }
    53. return *this;
    54. }
    55. //判空函数
    56. bool empty()
    57. {
    58. return 0==size;
    59. }
    60. //size函数
    61. int my_size()
    62. {
    63. cout<<strlen(str)<
    64. return strlen(str);
    65. }
    66. //c_str函数
    67. char * my_c_str()
    68. {
    69. return str;
    70. }
    71. //at函数
    72. char &at(int pos)
    73. {
    74. return str[pos];
    75. }
    76. //加号运算符重载
    77. const myString operator+(const myString &other)const
    78. {
    79. myString c;
    80. c.str=new char[size+other.size];
    81. strcpy(c.str,str);
    82. strcat(c.str,other.str);
    83. c.size=size+other.size;
    84. return c;
    85. }
    86. //加等于运算符重载
    87. myString &operator+=(const myString &other)
    88. {
    89. strcat(str,other.str);
    90. size+=other.size;
    91. return *this;
    92. }
    93. //关系运算符重载(>)
    94. bool operator>(const myString &other)const
    95. {
    96. if(strcmp(str,other.str)>0)
    97. {
    98. return true;
    99. }
    100. return false;
    101. }
    102. //中括号运算符重载
    103. char & operator[](int i)
    104. {
    105. if(i >= 0 && i< size)
    106. {
    107. return this->str[i];
    108. }
    109. else
    110. {
    111. cout<<"数组越界"<
    112. }
    113. }
    114. void show()
    115. {
    116. cout<
    117. }
    118. };
    119. int main()
    120. {
    121. char arr[10]="hello";
    122. myString s1(arr); //有参构造
    123. myString s2=s1; //拷贝构造
    124. myString s3; //无参构造
    125. s3=s2; //拷贝赋值函数
    126. s1.my_size();
    127. s2.my_size(); //size
    128. s3.my_size();
    129. s1.at(1)='o'; //at()函数
    130. s1.show();
    131. myString s4=s1+s2; //加法运算符重载
    132. s4.show();
    133. myString s5("world"); //有参构造
    134. s4+=s5; //+=运算符重载
    135. s4.show();
    136. s4.my_size();
    137. if(s5>s1)
    138. {
    139. cout<<"s5>s1"<
    140. }else
    141. {
    142. cout<<"s5<=s1"<
    143. }
    144. cout<6]<
    145. return 0;
    146. }

    思维导图

  • 相关阅读:
    10款轻量型的嵌入式GUI库分享
    安卓APP源码和设计报告——仿淘宝水果商城
    手撕链式二叉树(二)—【C语言】
    Java --- SpringMVC的HttpMessageConverter
    以数智化驱动为核心,构建研发效能增长动力
    R语言:利用biomod2进行生态位建模
    实现一个动态规划算法,解决背包问题
    vue面试经常会问的那些题
    seata案例搭建
    NC14700 追债之旅 (拆点+最短路)
  • 原文地址:https://blog.csdn.net/weixin_58469613/article/details/132817926