// 38总结 #include <iostream> using namespace std; class String { public: String(); //默认的构造函数 ~String(){ delete []str; len=0; cout<<"析构函数执行"<<endl; } String(const char*const ch); //构造带值的string int getlen()const { return len;} //读取长度 //const char *getstr()const{ return str;} //读取字符串 //重载输出函数 friend ostream&operator<<(ostream &o, const String &s) { o<<s.str; return o; } friend istream&operator>>(istream &i, String &s) { i>>s.str; return i; } //当运算符重载函数定义为成员函数时,二元运算符只带一个参数 //将这个函数定义为友元函数即可 friend bool operator<(const String&str1, const String&str2) { if(strcmp(str1.str, str2.str) < 0){ return 1; }else return 0; } friend bool operator>(const String&str1, const String&str2) { if(strcmp(str1.str, str2.str) > 0){ return 1; }else return 0; } friend bool operator==(const String&str1, const String&str2) { if(strcmp(str1.str, str2.str) == 0){ return 1; }else return 0; } //这里是可以修改的 char &operator[](unsigned short int length); char operator[](unsigned short int length)const; //复制构造函数 String (const String&r); //重载赋值运算符= String &operator=(const String &s); String operator+(const String&s); //重载相加运算符 void operator+=(const String&s); //重载相加运算符 private: String(unsigned short int); //构造带值的string unsigned short int len; char *str; }; //创建一个空的str变量 String::String() { len = 0; str = new char[1]; str[0] = '\0'; }; String::String(const char*const ch) { cout<<"带一个参数的构造函数"<<endl; len = strlen(ch); str = new char[len+1]; for(int i=0; i<len; i++){ str[i] = ch[i]; } str[len] = '\0'; }; String::String(unsigned short int lenght) { cout<<"带一个int参数的构造函数"<<endl; len = lenght; str = new char[len+1]; for(int i=0; i<=len; i++){ str[i] = '\0'; } }; char & String::operator[](unsigned short int length) { if(length > len){ return str[len-1]; //返回可见字符的值 }else{ return str[length]; } }; char String::operator[](unsigned short int length)const { cout<<"下标运算符const执行"<<endl; if(length > len){ return str[len-1]; //返回可见字符的值 }else{ return str[length]; } }; String::String (const String&rs) { len = rs.getlen(); str = new char[len+1]; for(int i=0; i<len; i++){ str[i] = rs[i]; //这里因为我们构造一个新对象并且用旧对象来为它赋值,很明显,不会修改旧对象的值,所以旧对象rs在调用operator[]const函数的时候,不用将指定字符串的地址返回,只需要要按值返回这个字符即可 //第二次重载的operator[]运算符函数,按值返回的一个字符,同时在函数体前面加了一个const //表示该函数可以操作const对象,也就是rs //这样由于2个同名的函数,它的类型不同,一个可操作const对象,一个不可以,这样就可以做到了对函数的重载 } str[len]='\0'; cout<<"复制构造函数完成:"<<str<<endl; }; String& String::operator=(const String &s) { cout<<"operator=执行"<<endl; if(this == &s) { return *this; }else{ delete []str; //删除左边的字符串 len = s.getlen(); str = new char[len+1]; for(int i=0; i<len; i++){ str[i] = s[i]; } str[len] = '\0'; } return *this; //注意按引用返回,也就是别名 } String String::operator+(const String &s) { cout<<"operator+执行......"<<endl; int total = len + s.getlen(); String temp(total); int i, j; for(i=0; i<len; i++){ temp[i] = str[i]; } for(j=0; j<s.getlen(); j++, i++){ temp[i] = s[j]; } return temp; } void String::operator+=(const String&s) { cout<<"这里执行的是相+=运算符"<<endl; int total = len + s.getlen(); String temp(total); int i, j; for(i=0; i<len; i++){ temp[i] = str[i]; } for(j=0; j<s.getlen(); j++,i++){ temp[i] = s[j]; } *this = temp; } int main() { //1 可对字符串进行初始化 String s1 = "hello wordl"; //2 可计算字符串的长度 cout<<"s1字符串长度:"<<s1.getlen()<<endl; //3 可对字符串进行赋值操作 String s2; s2 = s1; //4 重载了字符串的输出 cout<<s1<<s2<<endl; //5 通过重载输入字符串,输入字符串 //cin>>s1>>s2; //cout<<s1<<s2<<endl; //6 可将char字符串赋值给String char ch[11] = "not at all"; s1 = ch; cout<<s1<<endl; //7 可向构造对象那样构造字符串 String s3; String s4("mother"); //8 可用下标运算符来操作字符串 cout<<s4<<endl; cout<<s4[0]<<endl; //9 可以比较 s1 > s2; //10 两个对象可以相加 s1="aaa"; s2="bbb"; s3 = s1 + s2; cout<<"s3:"<<s3<<endl; //12可以进行+=操作 s1 += s2; cout<<"s1:"<<s1<<endl; String s5("a"); s1 = s5; cout<<"s1:"<<s1<<endl; return 0; }