• C++(day4)


    思维导图

    封装Mystring

    1. #include
    2. #include
    3. using namespace std;
    4. class Mystring{
    5. public:
    6. //无参构造函数
    7. Mystring():size(10){
    8. str=new char[size];
    9. strcpy(str,"");
    10. cout<<"无参构造函数"<
    11. }
    12. //有参构造函数
    13. Mystring(const char *s){
    14. size=strlen(s);
    15. str=new char[size+1];
    16. strcpy(str,s);
    17. cout<<"有参构造函数"<
    18. }
    19. //拷贝构造函数
    20. Mystring(const Mystring &other){
    21. this->size=other.size;
    22. this->str=new char[this->size];
    23. strcpy(this->str,other.str);
    24. cout<<"拷贝构造函数"<
    25. }
    26. //析构函数
    27. ~Mystring(){
    28. delete []str;
    29. cout<<"析构函数"<
    30. }
    31. //拷贝赋值函数
    32. Mystring &operator=(const Mystring &other){
    33. if(this!=&other){
    34. this->size=other.size;
    35. strcpy(this->str,other.str);
    36. }
    37. cout<<"拷贝赋值函数"<
    38. return *this;
    39. }
    40. //判空函数
    41. bool empty()const{
    42. return !strlen(this->str);
    43. }
    44. //size函数
    45. int strsize()const{
    46. return strlen(this->str);
    47. }
    48. //c_str函数
    49. char *c_str(){
    50. return this->str;
    51. }
    52. //at函数
    53. char &at(int pos){
    54. return *(this->str+pos-1);
    55. }
    56. //加号运算符重载
    57. Mystring operator+(const Mystring &R)const{
    58. Mystring temp;
    59. strcat(temp.str,this->str);
    60. strcat(temp.str,R.str);
    61. return temp;
    62. }
    63. //加等于运算符重载
    64. Mystring &operator+=(const Mystring &R){
    65. strcat(this->str,R.str);
    66. return *this;
    67. }
    68. //关系运算符重载(>)
    69. bool operator>(const Mystring &R)const{
    70. if(strcmp(this->str,R.str)>0){
    71. return true;
    72. }
    73. else{
    74. return false;
    75. }
    76. }
    77. //中括号运算符重载
    78. char &operator[](int pos)const{
    79. return *(this->str+pos-1);
    80. }
    81. //展示函数
    82. void show(){
    83. cout<
    84. }
    85. private:
    86. char *str; //字符串首地址
    87. int size; //字符串大小
    88. };
    89. int main()
    90. {
    91. Mystring str1("hello");
    92. str1.show();
    93. Mystring str2("world");
    94. str2.show();
    95. Mystring str3;
    96. if(str3.empty()){
    97. cout<<"str3现在为空,字符串长度为"<strsize()<
    98. }
    99. str3=str1;
    100. str3.show();
    101. if(!str3.empty()){
    102. cout<<"str3现在不为空,字符串长度为"<strsize()<
    103. }
    104. Mystring str4=str2;
    105. str4.show();
    106. str4+=str3;
    107. str4.show();
    108. cout<<"str4字符串第7位是"<at(7)<<",str4字符串第13位是"<13]<
    109. cout<c_str()<
    110. if(str3>str2){
    111. cout<<"str3>str2"<
    112. }
    113. else{
    114. cout<<"str3<
    115. }
    116. return 0;
    117. }

  • 相关阅读:
    service 详解
    阿里面试官惊了,Nginx配置一键生成
    leetcode — JavaScript专题(八):间隔取消、使用方法链的计算器、判断对象是否为空、记忆函数 II、设计可取消函数
    Kubernetes:(九)coredns(浪不动了)
    机器人编程初学者教程书:开启智能之旅的全方位指南
    模型训练无效bug记录
    1-12Vmware新增SSH功能
    Java多线程之 理解重排序
    牛客网---活动运营刷题笔记
    MySQL | 单行函数 |时间函数 | 系统函数
  • 原文地址:https://blog.csdn.net/qq_53268516/article/details/132816519