C++:类中的特殊关键字,运算重载符

打印 上一主题 下一主题

主题 984|帖子 984|积分 2962

1.My_string类中重载以下的运算符:
+、[] 、>、<、==、>=、<=、!=、+=、输入输出(>>、<<)
主函数:
  1. #include <iostream>
  2. #include "my_string.h"
  3. using namespace std;
  4. int main()
  5. {
  6.     My_string s1("cat");
  7.     My_string s2(" fash");
  8.     My_string s3=s1+s2;
  9.     s3.show();
  10.     cout<<"索引:"<<s3[5]<<endl;
  11.     My_string s4("beef");
  12.     My_string s5(" milk");
  13.     s4+=s5;
  14.     s4.show();
  15.     My_string s6("Abc");
  16.     My_string s7("abc");
  17.     cout<<(s6>s7)<<endl;
  18.     cout<<(s6<s7)<<endl;
  19.     return 0;
  20. }
复制代码
my_string.h
  1. #ifndef MY_STRING_H
  2. #define MY_STRING_H
  3. #include <iostream>
  4. #include <cstring>
  5. using namespace std;
  6. class My_string
  7. {
  8. private:
  9.     char *ptr;
  10.     int size;
  11.     int len;
  12. public:
  13.     //无参构造
  14.     My_string();
  15.     //有参构造
  16.     My_string(const char *src);
  17.     //析构函数
  18.     ~My_string();
  19.     My_string operator+(My_string &S);
  20.     char & operator[](int index);
  21.     bool operator>(const My_string &S) const;
  22.     bool operator<(const My_string &S) const;
  23.     bool operator==(const My_string &S) const;
  24.     bool operator>=(const My_string &S) const;
  25.     bool operator<=(const My_string &S) const;
  26.     bool operator!=(const My_string &S) const;
  27.     My_string & operator+=(const My_string &S);
  28.     friend ostream& operator<<(ostream &out, const My_string &S);
  29.     friend istream& operator>>(istream &in, My_string &S);
  30.     void show();
  31. };
  32. #endif // MY_STRING_H
复制代码
my_string.cpp
  1. #include "my_string.h"
  2. //无参构造
  3. My_string::My_string():size(15)
  4. {
  5.     this->ptr=new char[size];
  6.     this->ptr[0]='\0';
  7.     this->len=0;
  8. }
  9. //有参构造
  10. My_string::My_string(const char *src)
  11. {
  12.     len=0;
  13.     for(int i=0;src[i]!='\0';i++)
  14.     {
  15.         len++;
  16.     }
  17.     size=len+1;
  18.     this->ptr=new char[size];
  19.     for(int i=0;i<len;i++)
  20.     {
  21.         ptr[i]=src[i];
  22.     }
  23.     ptr[len]='\0';
  24. }
  25. //析构函数
  26. My_string::~My_string()
  27. {
  28.     delete []ptr;
  29.     //cout<<"析构函数"<<this<<endl;
  30. }
  31. //重载 +操作符
  32. My_string My_string::operator+(My_string &S)
  33. {
  34.     int newlen=len+S.len;
  35.     char *newptr=new char[newlen+1];//分配新内存
  36.     strcpy(newptr,this->ptr);
  37.     strcat(newptr,S.ptr);
  38.     My_string temp(newptr);//创建临时对象
  39.     delete []newptr; // 释放临时内存
  40.     return temp;
  41. }
  42. //重载 [] 操作符
  43. char & My_string::operator[](int index)
  44. {
  45.     if(index<0 ||index>=len)
  46.     {
  47.         exit(EXIT_FAILURE);
  48.     }
  49.     else
  50.     {
  51.         return ptr[index-1];
  52.     }
  53. }
  54. // 重载 > 操作符
  55. bool My_string::operator>(const My_string &S) const
  56. {
  57.     return strcmp(this->ptr, S.ptr) > 0;
  58. }
  59. // 重载 < 操作符
  60. bool My_string::operator<(const My_string &S) const
  61. {
  62.     return strcmp(this->ptr, S.ptr) < 0;
  63. }
  64. // 重载 == 操作符
  65. bool My_string::operator==(const My_string &S) const
  66. {
  67.     return strcmp(this->ptr, S.ptr) == 0;
  68. }
  69. // 重载 >= 操作符
  70. bool My_string::operator>=(const My_string &S) const
  71. {
  72.     return strcmp(this->ptr, S.ptr) >= 0;
  73. }
  74. // 重载 <= 操作符
  75. bool My_string::operator<=(const My_string &S) const
  76. {
  77.     return strcmp(this->ptr, S.ptr) <= 0;
  78. }
  79. // 重载 != 操作符
  80. bool My_string::operator!=(const My_string &S) const
  81. {
  82.     return strcmp(this->ptr, S.ptr) != 0;
  83. }
  84. // 重载 += 操作符
  85. My_string & My_string::operator+=(const My_string &S)
  86. {
  87.     int newlen=len+S.len;
  88.     char *newptr = new char[newlen+1];
  89.     strcpy(newptr, this->ptr);
  90.     strcat(newptr, S.ptr);
  91.     delete[] this->ptr;
  92.     this->ptr = newptr; // 更新 ptr 指向新内存
  93.     this->len += newlen; // 更新长度
  94.     this->size = newlen + 1; // 更新容量
  95.     return *this;
  96. }
  97. ostream& operator<<(ostream &out, const My_string &S)
  98. {
  99.     out << S.ptr;
  100.     return out;
  101. }
  102. istream& operator>>(istream &in, My_string &S)
  103. {
  104.     delete[] S.ptr; // 释放已有内存
  105.     S.ptr = new char[S.size]; // 重新分配内存
  106.     in >> S.ptr; // 读取字符串
  107.     S.len = strlen(S.ptr); // 更新长度
  108.     return in;
  109. }
  110. void My_string::show()
  111. {
  112.     cout<<"*ptr="<<ptr<<endl;
  113.     cout<<"size="<<size<<endl;
  114.     cout<<"len="<<len<<endl;
  115. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

来自云龙湖轮廓分明的月亮

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表