• 2023/9/11 qt&c++


    1. #include
    2. #include
    3. using namespace std;
    4. class myString
    5. {
    6. private:
    7. char *str;
    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)
    18. {
    19. size = strlen(s);
    20. str = new char[size+1];
    21. strcpy(str,s);
    22. }
    23. //拷贝构造
    24. myString(const myString &other):size(other.size)
    25. {
    26. str = new char [size];
    27. strcpy(this->str,other.str);
    28. cout<<"拷贝构造函数"<
    29. }
    30. //析构函数
    31. ~myString()
    32. {
    33. delete[] this->str;
    34. cout<<"析构函数"<
    35. }
    36. //拷贝赋值函数
    37. myString &operator = (const myString &other)
    38. {
    39. size = other.size;
    40. strcpy(str,other.str);
    41. cout<<"拷贝赋值函数"<
    42. return *this;
    43. }
    44. //判空函数
    45. bool str_empty(const char *str) const
    46. {
    47. if(str ==NULL||*str=='\0')
    48. {
    49. return true;
    50. }
    51. else
    52. return false;
    53. }
    54. //size函数
    55. int str_size(const char *str)const
    56. {
    57. return sizeof(str);
    58. }
    59. //c_str函数
    60. const char *c_str() const
    61. {
    62. return str;
    63. }
    64. //at函数
    65. char &at(int pos)
    66. {
    67. return str[pos];
    68. }
    69. //成员函数版实现加号运算符重载
    70. myString operator+(const myString &R)const
    71. {
    72. myString new_string = *this;
    73. delete[] new_string.str;
    74. int len =strlen(this->str)+strlen(R.str)+1;
    75. new_string.str = new char[len];
    76. strcpy(new_string.str,this->str);
    77. strcat(new_string.str,R.str);
    78. return new_string;
    79. }
    80. //成员函数版实现加等于运算符重载
    81. myString &operator+=(const myString &R)
    82. {
    83. int len = strlen(str)+strlen(R.str)+1;
    84. char *s =this->str;
    85. str = nullptr;
    86. delete [] str;
    87. str = new char [len];
    88. strcpy(this->str,s);
    89. strcat(this->str,R.str);
    90. return *this;
    91. }
    92. //关系运算符重载
    93. bool operator>(const myString &R)const
    94. {
    95. //先求出长度
    96. int len1 = strlen(this->str);
    97. int len2 = strlen(R.str);
    98. int minlen =(len1
    99. for(int i=0;i
    100. {
    101. if(this->str[i]>R.str[i])
    102. {
    103. return true;
    104. }
    105. else if(this->str[i]
    106. {
    107. return false;
    108. }
    109. }
    110. return len1>len2;
    111. }
    112. //成员函数版实现中括号运算符重载
    113. char & operator[](int index)
    114. {
    115. if(index>=0&&index
    116. {
    117. return str[index];
    118. }
    119. }
    120. //展示函数
    121. void show()
    122. {
    123. cout<<"str = "<" size = "<
    124. }
    125. };
    126. int main()
    127. {
    128. myString s1("hello");
    129. s1[0]='H';
    130. s1.show();
    131. myString s2("world");
    132. s2.show();
    133. myString s6 =s1;
    134. s6.show();
    135. myString s3;
    136. s3 = s1 + s2;
    137. cout<c_str()<
    138. myString s4("hahaha");
    139. s4+=s1;
    140. cout<c_str()<
    141. if(s3>s2)
    142. {
    143. cout<<"yes"<
    144. }
    145. else
    146. cout<<"no"<
    147. myString s5("daydayup");
    148. s5.show();
    149. return 0;
    150. }

  • 相关阅读:
    Python 自定义模块和包设计英语生词本(文件版)
    26-sparkstreaming
    《深度学习工业缺陷检测》专栏介绍 & CSDN独家改进实战
    基于Python实现的图形绘制系统
    c 语言基础:L1-047 装睡
    2 万字 + 30 张图 | 细聊 MySQL undo log、redo log、binlog 有什么用?
    【SwiftUI模块】0005、SwiftUI-粘性动画指示器引导页
    领导不懂IT技术,分不清报表和BI,看完这篇文章就懂了
    腾讯、飞书等在线表格自动化编辑--python
    线粒体 ClpP 介导的蛋白水解作用可选择性诱导癌细胞死亡
  • 原文地址:https://blog.csdn.net/cscssacd/article/details/132818388