• c++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. strcpy(this->str,other.str);
    27. this->size=other.size;
    28. cout<<"拷贝构造函数"<
    29. }
    30. //析构函数
    31. ~myString()
    32. {
    33. delete str;
    34. cout<<"析构函数:"<<this<
    35. }
    36. //拷贝赋值函数
    37. myString & operator=(const myString &other)
    38. {
    39. if(this != &other) //确定不是自己给自己赋值
    40. {
    41. this->size = other.size;
    42. //判断原来指针空间释放被清空
    43. if(this->str != NULL)
    44. {
    45. delete this->str;
    46. }
    47. this->str = new char(*other.str);
    48. }
    49. cout<<"拷贝赋值函数"<
    50. return *this; //返回自身引用
    51. }
    52. //判空函数
    53. bool empty()
    54. {
    55. return 0==size;
    56. }
    57. //size函数
    58. int mystring_size()
    59. {
    60. return strlen(str);
    61. }
    62. //c_str函数
    63. char *c_str()
    64. {
    65. return this->str;
    66. }
    67. //at函数
    68. char &at(int pos)
    69. {
    70. return str[pos-1];
    71. }
    72. //加号运算符重载
    73. const myString operator+(const myString &R)
    74. {
    75. myString c;
    76. // 计算合并后的字符串长度
    77. c.size=this->size+R.size;
    78. // 复制第一个字符串到结果字符串
    79. memcpy(c.str,this->str,this->size);
    80. // 复制第二个字符串到结果字符串
    81. memcpy(c.str+this->size,R.str,R.size+1);
    82. return c;
    83. }
    84. //加等于运算符重载
    85. myString & operator+=(const myString &R)
    86. {
    87. // 复制第二个字符串到第一个字符串后
    88. memcpy(this->str+this->size,R.str,R.size+1);
    89. // 计算合并后的字符串长度
    90. this->size=this->size+R.size;
    91. return *this;
    92. }
    93. //关系运算符重载(>)
    94. bool operator>(const myString &R)const
    95. {
    96. return strcmp(this->str,R.str)>0;
    97. }
    98. //中括号运算符重载
    99. char & operator[](int index)
    100. {
    101. return this->str[index];
    102. }
    103. };
    104. int main()
    105. {
    106. myString s1("hello");
    107. //判空函数
    108. if(s1.empty())
    109. {
    110. cout<<"函数为空"<
    111. }
    112. else
    113. {
    114. cout<<"函数不为空"<
    115. }
    116. //打印s1的长度
    117. cout<<"size="<mystring_size()<
    118. //打印s1字符串内容
    119. cout<<"s1="<c_str()<
    120. //调用&at函数,打印s1[1]的内容
    121. cout<<"s1[1]="<at(2)<
    122. myString s2("world");
    123. //打印s2字符串内容
    124. cout<<"s2="<c_str()<
    125. //调用+=运算符重载函数
    126. s2+=s1;
    127. //打印更新之后s2的字符串内容
    128. cout<<"s2="<c_str()<
    129. //调用+号运算符重载
    130. myString s3=s1+s2;
    131. //打印s3字符串内容
    132. cout<<"s3="<c_str()<
    133. //调用[]运算符重载
    134. cout<<"s3[5]="<6]<
    135. return 0;
    136. }

  • 相关阅读:
    C++ primer 5th笔记
    TF-IDF(Term Frequency-Inverse Document Frequency)
    LeetCode 87 双周赛
    【算法入门&二叉树】从先中后序的遍历到用中后序列构造二叉树|如何抵挡递归法该死的魅力
    进军多项式(三):Chirp Z-Transform
    powershell美化
    华为ACL实验
    java精品旅游项目管理系统计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    探索计算机的I/O控制方式:了解DMA控制器的作用与优势
    「入门篇」初识JVM (下下) - GC
  • 原文地址:https://blog.csdn.net/wdc857/article/details/132817309