#pragma warning (disable:4996)
#include<iostream>
using namespace std;
class String
{
protected:
char* str;
public:
String();
String(const char* content);
String(const String& cstr);
~String();
void set(const char* content);
int length() const;
void print() const;
String& operator=(const String& cstr);
String& operator=(const char* cstr);
char& operator[](int index);
operator char* ();
friend String operator+(const String& cstr1, const String& cstr2);
friend String operator+(const String& cstr1, const char* cstr2);
friend String operator+(const char* cstr1, const String& cstr2);
friend String& operator++(String& cstr);
friend String operator++(String& cstr, int);
};
String::String()
{
str = 0;
}
String::String(const char* content)
{
int len = strlen(content);
str = new char[len + 1];
strcpy(str, content);
}
String::String(const String& cstr)
{
int len = strlen(cstr.str);
str = new char[len + 1];
strcpy(str, cstr.str);
}
String::~String()
{
if (str != 0)
delete[]str;
}
void String::set(const char* content)
{
int len = strlen(content);
str = new char[len + 1];
strcpy(str, content);
}
int String::length() const
{
int len = strlen(str);
return len;
}
void String::print() const
{
cout << "字符串为:" << str << endl;
}
String& String::operator=(const String& cstr)
{
if (str != 0)
delete[]str;
int len = strlen(cstr.str);
str = new char[len + 1];
strcpy(str, cstr.str);
return *this;
}
String& String::operator=(const char* cstr)
{
if (str != 0)
delete[]str;
int len = strlen(cstr);
str = new char[len + 1];
strcpy(str, cstr);
return *this;
}
char& String::operator[](int index)
{
return str[index];
}
String::operator char* ()
{
return str;
}
String operator+(const String& cstr1, const String& cstr2)
{
String cp1;
int len = strlen(cstr1.str) + strlen(cstr1.str);
cp1.str = new char[len+1];
strcpy(cp1.str, cstr1.str);
strcat(cp1.str, cstr2.str);
return cp1;
}
String operator+(const String& cstr1, const char* cstr2)
{
String cp2;
int len = strlen(cstr1.str) + strlen(cstr2);
cp2.str = new char[len+1 ];
strcpy(cp2.str, cstr1.str);
strcat(cp2.str, cstr2);
return cp2;
}
String operator+(const char* cstr1, const String& cstr2)
{
String cp3;
int len = strlen(cstr1) + strlen(cstr2.str);
cp3.str = new char[len + 1];
strcpy(cp3.str, cstr1);
strcat(cp3.str, cstr2.str);//先复制用strcpy,将\0复制最后面,然后strcat的内容将\0覆盖最后结尾加了一个\0.(理论感觉可以,但实际操作却有的成功,有的失败了)
return cp3;
}
String& operator++(String& cstr)
{
int len = strlen(cstr.str);
for (int i = 0; i < len; i++)
{
if (cstr.str[i] >= 'a' && cstr.str[i] <= 'z')
cstr.str[i] = cstr.str[i] - 32;
else
cstr.str[i] = cstr.str[i];
}
return cstr;
}
String operator++(String& cstr, int)
{
String cp4(cstr.str);
int len = strlen(cstr.str);
for (int i = 0; i < len; i++)
{
if (cstr.str[i] >= 'a' && cstr.str[i] <= 'z')
cstr.str[i] = cstr.str[i] - 32;
else
cstr.str[i] = cstr.str[i];
}
return cp4;
}
int main()
{
char* nstr;
char zemb[7] = "hdjj";
nstr = zemb;
String temp1("Sgd");
String temp2("bhd");//把这里改成“gdhfjkvb”则无法运行成功
String temp7, temp8;
temp7 = temp1;
temp8 = nstr;
temp7.print();
temp8.print();
String temp3 = temp1 + temp2;
temp3.print();
String temp4;
temp4 = temp1 + nstr;
temp4.print();
String temp5;
temp5 = nstr + temp1;
temp5.print();
++temp1;
temp1.print();
String temp6;
temp6 = temp2++;
temp6.print();
return 0;
}