• C++&QT day4


    仿照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. }
    16. //有参构造
    17. myString(const char *s) //string s("hello world")
    18. {
    19. size = strlen(s);
    20. str = new char[size+1];
    21. strcpy(str, s);
    22. }
    23. //拷贝构造
    24. myString(const myString &other):str(new char(*other.str)),size(other.size)
    25. {
    26. cout<<"拷贝构造函数"<
    27. }
    28. //析构函数
    29. ~myString()
    30. {
    31. delete str;//释放成员指针的空间
    32. cout<<"mySting::析构函数"<
    33. }
    34. //拷贝赋值函数
    35. myString &operator=(const myString &other)
    36. {
    37. if(this!=&other)
    38. {
    39. this->size=other.size;
    40. if(this->str!=NULL)
    41. {
    42. delete this->str;
    43. }
    44. this->str=new char(*other.str);
    45. }
    46. cout<<"myString::拷贝赋值函数"<
    47. }
    48. //判空函数
    49. bool my_empty()
    50. {
    51. if(0==my_size())
    52. {
    53. return true;
    54. }
    55. return false;
    56. }
    57. //size函数
    58. int my_size()
    59. {
    60. if(*str!=0)
    61. {
    62. str++;
    63. size++;
    64. }
    65. return size;
    66. }
    67. //c_str函数
    68. char *c_str()
    69. {
    70. return this->str;
    71. }
    72. //at函数
    73. char &at(int pos)
    74. {
    75. if(pos >= 0 && pos< size)
    76. {
    77. return this->str[pos];
    78. }
    79. else
    80. {
    81. cout<<"数组越界"<
    82. }
    83. }
    84. //加号运算符重载
    85. const myString operator+ (const myString &R)const
    86. {
    87. myString c;
    88. c.str=strcat(this->str, R.str);
    89. return c;
    90. }
    91. //加等于运算符重载
    92. myString operator+=(const myString &other)
    93. {
    94. strcat(str,other.str);
    95. size+=other.size;
    96. return *this;
    97. }
    98. //关系运算符重载(>)
    99. bool operator> (const myString &R)const
    100. {
    101. if(strcmp(this->str,R.str) > 0)
    102. {
    103. return true;
    104. }
    105. else
    106. return false;
    107. }
    108. //中括号运算符重载
    109. char & operator[](int i)
    110. {
    111. if(i >= 0 && i< size)
    112. {
    113. return this->str[i];
    114. }
    115. else
    116. {
    117. cout<<"数组越界"<
    118. }
    119. }
    120. };
    121. int main()
    122. {
    123. myString mystr("hello");
    124. myString mystr1("world");
    125. myString mystr2=mystr+mystr1;
    126. cout<at(1)<
    127. cout<1]<
    128. return 0;
    129. }

    思维导图

  • 相关阅读:
    Java web应用性能分析之【自定义prometheus监控指标】
    【最小的调整次数】python实现-附ChatGPT解析
    Promethus(一)概览
    加密市场“至暗时刻”已经过去,Zebec迎来上涨主升浪
    TeeChart NET for Blazor v2022
    如何使用ChatGPT来辅助写简历
    虚幻引擎4利用粒子系统实现物体轨迹描绘2- 消除轨迹
    RPA是什么?推荐让电商运营10倍提效的自动化工具
    JQuery
    单个vue echarts页面
  • 原文地址:https://blog.csdn.net/m0_59031281/article/details/132817077