C++自己写类 和 运算符重载函数

1.各类函数要求

1:析构函数,释放buf指向的堆空间

2:编写 append(const mystring r) 为当前字符串尾部,拼接新的字符串r

3:编写 isEqual(const mystring r) 判断当前字符串和 字符串 r是否相等

4.为mystring类补充深拷贝功能的拷贝构造函数

5.为mystring类补充: swap(另一个对象) 的函数,实现交换2个对象的字符串 mystring str = "hello" mystring ptr = "world" str.swap(ptr) str == "world",ptr == "hello"

2.各类运算符重载要求

为之前写的mystring类,添加 + 功能

:本质就是append str1 = "hello" str2 = "world" str3 = str1 + str2 == "helloworld"

+= 功能

= 深拷贝功能

== 功能

!= 功能

++ 功能,字符串中所有字符的ASCII自增1

\] 下表运算符 operator\[\](int index) 返回该下标上的字符 str = "hello" cout \<\< str\[0\] ;终端输出 h cout 输出mystring功能 cin 输入mystring功能 ```cpp using namespace std; class mystring { char * buf; //基本数据类型 public: mystring(const char * str); //绑定一个构造函数 mystring(); //绑定另外一个构造函数 ~mystring(); //析构函数 void show(); //绑定显示方法 void setmystring(const mystring r); // 设置字符串 const char * getmystring()const; //获取当前字符串 void append(const mystring r); //为当前字符串尾部拼接字符串r bool isEqual(const mystring r); //字符串比较否相等 void swapmystring(mystring& r); //字符串交换 mystring(const mystring& r); //深拷贝构造函数 friend mystring operator+(mystring& ,const mystring& ); //加法运算符 friend mystring operator+=(mystring& ,const mystring &); //复合运算 mystring operator=(const mystring & r) //深拷贝功能 由于等号=不能放在外面 参数不和 需要写成类函数方法 { int len = strlen(r.buf); this->buf = new char[len+1]; strcpy(buf,r.buf); return *this; } friend bool operator==(const mystring& ,const mystring& ); //字符串相等函数 friend bool operator!=(const mystring& ,const mystring& ); //字符串不等 //++ 自增功能重载成字符串中的所有字符ASKII加1 mystring& operator++()//前++ { for(int i =0;buf[i] != '\0';i++) { buf[i]++; } return *this; //返回本身 } mystring operator++(int)//后++ { mystring temp = *this; ++(*this); return temp; //返回未加前 } char& operator[](int index) //下标运算符[] { return buf[index]; } friend ostream& operator<<(ostream& ,const mystring&); //cout输出mystring friend istream& operator>>(istream& ,mystring&); //cin输入mtstring }; //+运算符重载成字符串拼接函数 mystring operator+(mystring& l,const mystring& r) { mystring temp; temp.buf = new char[strlen(l.buf)+strlen(r.buf)]; strcpy(temp.buf,l.buf); temp.append(r); return temp; } //+=运算符重载成字符串追加函数 mystring operator+=(mystring& l,const mystring &r) { l.append(r); return l; } // == 运算符重载成字符串相等函数 bool operator==(const mystring& l,const mystring& r) { if(strcmp(l.buf,r.buf) == 0) { return 1; } return 0; } //!= 运算符重载成字符串不等函数 bool operator!=(const mystring&l,const mystring& r) { if(strcmp(l.buf,r.buf) != 0) { return 1; } return 0; } //<<因为<> istream& operator>>(istream& in,mystring& r) { in >> r.buf; return in; } mystring::~mystring() //析构函数 { free(buf); } mystring::mystring() //无参构造函数 { buf = (char *)calloc(1,1); } mystring::mystring(const char * str) //有参构造函数 { int len = strlen(str); buf = (char *)calloc(1,len+1); strcpy(buf,str); } void mystring::show() //打印函数 { cout << buf << endl; } void mystring::setmystring(const mystring r) //设置当前字符串 { free(this->buf); int len = strlen(r.buf); this->buf = (char *)calloc(1,len+1); strcpy(this->buf,r.buf); } const char* mystring::getmystring()const //获取当前字符串 { return buf; } void mystring::append(const mystring r) //追加函数 { int m = strlen(this->buf); int n = strlen(r.buf); int len = m+n; char* temp = buf; buf = (char* )calloc(1,len+1); strcpy(buf,temp); free(temp); strcat(buf,r.buf); } bool mystring::isEqual(const mystring r) //判断是否相等 { return strcmp(buf,r.buf) == 0; } void mystring::swapmystring(mystring& r) //字符串交换函数 { char* temp = this->buf; this->buf = r.buf; r.buf = temp; } mystring::mystring(const mystring& r) //深拷贝构造函数 { int len = strlen(r.buf); buf = (char* )calloc(1,len+1); strcpy(buf,r.buf); } int main() { //+测试 mystring str1 = "hello"; mystring str2 = "world!"; mystring str3 = str1 + str2; str3.show(); //+=测试 mystring str4 = "godbay"; mystring str5 = "nono"; str4 += str5; str4.show(); //=测试 mystring str6 ="nihao"; str6 = "nibuhao"; str6.show(); //==测试 if(str6 == str5) { cout << "相等" << endl; } else { cout << "不等" << endl; } //!=测试 if(str5 != str6) { cout << "不相等" << endl; } else { cout << "相等" << endl; } //[]下标运算符测试 cout << str1[0] << endl; //测试cout cin mystring str8; mystring str9 = "下班下班,回家睡觉了"; cin >> str8; cout << str9 << endl; /*str.show(); ptr.setmystring(str); ptr.show(); ptr.setmystring("godbay,world!"); ptr.show(); str.append(ptr); str.show(); if(str.isEqual(ptr)) { cout << "str = ptr" << endl; } */ return 0; } ```

相关推荐
mozun20201 小时前
VS BUG(6) LINK : fatal error LNK1158: 无法运行“rc.exe”
c++·bug·vs·链接器·资源文件
whoarethenext2 小时前
初始https附带c/c++源码使用curl库调用
服务器·c++·qt·https·curl
cloues break.3 小时前
C++进阶----多态
开发语言·c++
Despacito0o4 小时前
C++核心编程:类与对象全面解析
开发语言·c++
CodeWithMe6 小时前
【C++】线程池
开发语言·c++
wuqingshun3141597 小时前
蓝桥杯 2. 确定字符串是否是另一个的排列
数据结构·c++·算法·职场和发展·蓝桥杯
hu_yuchen7 小时前
C++:BST、AVL、红黑树
开发语言·c++
炯哈哈7 小时前
【上位机——MFC】视图
开发语言·c++·mfc·上位机
我也不曾来过17 小时前
继承(c++版 非常详细版)
开发语言·c++
1白天的黑夜19 小时前
贪心算法-2208.将数组和减半的最小操作数-力扣(LeetCode)
c++·算法·leetcode·贪心算法