• c++day4


    作业

    仿照string类,完成myString 类

    class myString { private: char *str; //记录c风格的字符串 int size; //记录字符串的实际长度 public: //无参构造 myString():size(10) { str = new char[size]; //构造出一个长度为10的字符串 strcpy(str,""); //赋值为空串 } //有参构造 myString(const char *s) //string s("hello world") { size = strlen(s); str = new char[size+1]; strcpy(str, s); } //拷贝构造 //析构函数 //拷贝赋值函数 //判空函数 //size函数 //c_str函数 //at函数 char &at(int pos) //加号运算符重载 //加等于运算符重载 //关系运算符重载(>) //中括号运算符重载 };

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

     思维导图

  • 相关阅读:
    详解token已过期含义及解决方 token过期是否需要重新登录
    微服务是个坏主意吗?
    Flask 用户登录,表单提交
    LQ0191 切面条【填空题】
    Informer论文思维导图
    摩尔信使MThings的设备高级参数
    【无标题】This project has been opened by another efinity instance
    刷题数据结构实现类
    日期格式化 YYYY-MM-DD 出现时间偏移量
    深度学习——SAM(Segment-Anything)代码详解
  • 原文地址:https://blog.csdn.net/HYL1234511/article/details/132815994