仿照string类,完成myString 类
#include <iostream>
#include <cstring>using namespace std;class myString{
private:char *str;int size;
public:myString():size(10){str = new char[size];strcpy(str,"");}myString(const char*s){size = strlen(s);str = new char[size+1];strcpy(str,s);}//拷贝构造myString(const myString &other):str(new char(*other.str)),size(other.size){cout<<"拷贝构造"<<endl;}//析构函数~myString(){delete str;cout<<"析构函数"<<this<<endl;}//拷贝赋值函数myString & operator=(const myString &other){if(this != &other) //确定不是自己给自己赋值{this->size = other.size;//判断原来指针空间释放被清空if(this->str != NULL){delete this->str;}this->str = new char(other.size);strcpy(str,other.str);//如果指针空间大小一致 //*this->ptr = *other.ptr;}cout<<"Stu:: 拷贝赋值函数"<<endl;return *this; //返回自身引用}void show(){cout<<size<<endl;cout<<str<<endl;}//判空函数int empty(){return str[1]==0;}//size函数int getsize(){int i=0;while(*(str+i)){i++;}size = i;return i;}//c_str函数const char* c_str(){char *word=new char[size+1];for(int i=0;i<size;i++){word[i]=*(str+i);}return word;}//at函数char &at(int pos){if(pos<0||pos>size){cout<<"输入的位置有误请重新输入"<<endl;}return *(str+pos);}friend const myString operator +(const myString &L,const myString &R);//加等于运算符重载myString & operator += (const myString &R){strcat(this->str,R.str);this->size+=R.size;return *this;}//关系运算符重载(>)bool operator > (const myString &R)const{return strcmp( this->str,R.str);}//中括号运算符重载char & operator[](int index){return *(str+index);}
};
//加号运算符重载
const myString operator+(const myString &L,const myString &R){myString c;strcpy(c.str,L.str);strcat(c.str,R.str);c.size=R.size+L.size;return c;
}int main()
{myString str("114514");myString str1("1919810");myString str3=str+str1;myString str4;str4=str;str4+=str1;str4[6]='a';if(str>str1){cout<<"大"<<endl;}else{cout<<"小"<<endl;}str4.show();return 0;
}