• c++ day 4


    1、仿照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<<"析构函数"<
    33. }
    34. //拷贝赋值函数
    35. myString & operator=(const myString &other)
    36. {
    37. if(this != &other)
    38. {
    39. this->size = other.size;
    40. //判断原来空间释放
    41. if(this->str !=NULL)
    42. {
    43. delete this->str;
    44. }
    45. this->str = new char(*other.str);
    46. }
    47. cout<<"Stu::拷贝赋值函数"<
    48. return *this;
    49. }
    50. //判空函数
    51. bool str_empty()
    52. {
    53. return !strcmp("",str);
    54. }
    55. //size函数
    56. int str_size()
    57. {
    58. return strlen(this->str);
    59. }
    60. //c_str函数
    61. char *s_c_str()
    62. {
    63. return str;
    64. }
    65. //at函数
    66. char &at(int pos)
    67. {
    68. return str[pos];
    69. }
    70. //加号运算符重载
    71. const myString operator+(const myString &R)const
    72. {
    73. myString c;
    74. c.str=strcat(this->str,R.str);
    75. c.size=this->size+R.size;
    76. return c;
    77. }
    78. //加等于运算符重载
    79. myString & operator+=(const myString &R)
    80. {
    81. this->str=strcat(this->str,R.str);
    82. this->size+=R.size;
    83. return *this;//返回自身的引用
    84. }
    85. //关系运算符重载(>)
    86. bool operator>(const myString &R)const
    87. {
    88. return strcmp(this->str,R.str);
    89. }
    90. //中括号运算符重载
    91. char &operator[](int index)
    92. {
    93. return str[index];
    94. }
    95. //定义遍历
    96. void show()
    97. {
    98. cout<<"字符串:"<
    99. cout<<"实际长度:"<
    100. }
    101. };
    102. int main()
    103. {
    104. myString c1("hello ");
    105. c1.show();
    106. myString c2("world");
    107. c2.show();
    108. myString c3=c1+c2;
    109. c3.show();
    110. if(c1>c2)
    111. {
    112. cout<<"yes"<
    113. }
    114. else
    115. {
    116. cout<<"no"<
    117. }
    118. c3[0]='*';
    119. c3.show();
    120. c3+=c2;
    121. c3.show();
    122. cout<<"size="<str_size()<
    123. return 0;
    124. }

    2、思维导图

  • 相关阅读:
    .NET的键盘Hook管理类,用于禁用键盘输入和切换
    Rust 登上了开源头条「GitHub 热点速览」
    短视频账号矩阵系统saas管理私信回复管理系统
    js 内存泄露,几种常涉及到的内存泄露
    优化软件测试成本的 7 个步骤
    【Java面试】Spring中 BeanFactory和FactoryBean的区别
    vue的diff算法
    MQTT 基础--MQTT 协议简介 :第 1 部分
    实践 缓存
    办公软件WPS与Office的区别
  • 原文地址:https://blog.csdn.net/A18801772899/article/details/132817697