• C++ 实现运算符重载


    代码:

    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)+1;
    20. str = new char[size];
    21. strcpy(str, s);
    22. }
    23. //拷贝构造
    24. myString(char *s,int i):str(new char(*s)),size(i){
    25. strcpy(this->str,s);
    26. this->size = i;
    27. cout<<"拷贝构造函数"<
    28. }
    29. //析构函数
    30. ~myString(){
    31. delete str;
    32. }
    33. //拷贝赋值函数
    34. myString &operator = (const myString &other){
    35. if(&other != this){
    36. this->str = new char[size];
    37. memset(str,0,size);
    38. strcpy(str,other.str);
    39. }
    40. return *this;
    41. }
    42. //判空函数
    43. bool empty(){
    44. if(strlen(str) == 0)
    45. return false;
    46. else
    47. return true;
    48. }
    49. //size函数
    50. int mysize(){
    51. return size;
    52. }
    53. //c_str函数
    54. const char* c_str(){
    55. return str;
    56. }
    57. //at函数
    58. char &at(int pos){
    59. if(pos>=size || pos<0){
    60. cout<<"访问越界"<
    61. }
    62. return str[pos];
    63. }
    64. //加号运算符重载
    65. const myString operator+(const myString &s)const{
    66. myString m;
    67. strcpy(m.str,this->str);
    68. strcat(m.str,s.str);
    69. m.size = strlen(m.str);
    70. return m;
    71. }
    72. //加等于运算符重载
    73. const myString operator+=(const myString &s){
    74. strcat(this->str,s.str);
    75. this->size = strlen(this->str);
    76. return *this;
    77. }
    78. //关系运算符重载(>)
    79. bool operator >(const myString &s)const{
    80. if(this->size < s.size){
    81. return false;
    82. }
    83. else{
    84. for(int i=0;i<this->size;i++){
    85. if(*(this->str+i)<*(s.str+i)){
    86. return false;
    87. }
    88. }
    89. }
    90. return true;
    91. }
    92. //中括号运算符重载
    93. char & operator[](const int pos)const{
    94. if(pos>=size || pos<0){
    95. cout<<"访问越界"<
    96. }
    97. return *(str+pos);
    98. }
    99. };
    100. int main(){
    101. //展示字符串
    102. myString s1("ssssyyyy");
    103. myString s2("wwwwhhhh");
    104. cout<c_str()<<" "<c_str()<
    105. //重载加法运算符
    106. myString s3;
    107. cout<mysize()<
    108. s3=s1+s2;
    109. cout<c_str()<
    110. //at运算
    111. cout<at(3)<
    112. //加等于
    113. s3+=s1;
    114. cout<c_str()<
    115. cout<mysize()<
    116. //关系运算
    117. if(s3>s1){
    118. cout<<"s3>s1"<
    119. }
    120. else
    121. cout<<"s3<
    122. //中括号
    123. cout<3]<<" "<6]<< " "<8]<
    124. return 0;
    125. }

     

  • 相关阅读:
    [AIGC] 使用Google的Guava库中的Lists工具类:常见用法详解
    Linux第一个小程序——进度条
    nginx负载均衡和高可用
    CSS 常用样式background背景属性
    【CNN-SVM回归预测】基于CNN-SVM实现数据回归预测附matlab代码
    毫无基础的人如何入门 Python ?
    使用Visual Studio Code 进行Python编程(一)
    Linux下的Docker安装,以Ubuntu为例
    说一下 JVM 有哪些垃圾回收器?
    Python 使用PIL读取图像自动旋转exif信息
  • 原文地址:https://blog.csdn.net/2301_77665369/article/details/133521398