#include
using namespace std;
class MyString
{
public:
void Zero();
MyString();
~MyString();
void zeroStr(){if(m_string) {delete m_string;m_string=0;}}
MyString(const char* str);
MyString(MyString & str);
MyString & operator=(const MyString & str);//这里用的是引用返回,所用一定要用this指针
MyString & operator=(const char* c_str);//这里用的是引用返回,所用一定要用this指针
char operator[](const unsigned int index);
MyString strcat(const MyString & str);
MyString strcat(const char * c_str);
bool operator>(const MyString &str);
/*其余部分因时间关系,我就不写了,不过可以参考以上的方法和视图*/
char* getString();
int length();
private:
char * m_string; //指向字符串首地址
unsigned int m_size; //字符串长度
};
void MyString::Zero()
{
if(!m_string)
return;
delete m_string;
m_string=0;//清零很重要
}
MyString::MyString()
{
m_size=255;
m_string=new char[255];
*m_string='\0';
cout<<"我是\t无参数构造函数"< cout<<"m_string的值是:\t"< } MyString::~MyString() { Zero(); cout<<"析构函数运行了"< } MyString::MyString(const char* str) { m_size=strlen(str)+1; m_string=new char[m_size]; memcpy(m_string,str,m_size); cout<<"我是\t字符串构造函数"< cout<<"m_string的值是:\t"< } MyString::MyString(MyString & str) { m_size=str.m_size; m_string=new char[m_size]; memcpy(m_string,str.m_string,m_size); cout<<"我是\t浅层拷贝"< cout<<"m_string的值是:\t"< } MyString & MyString::operator=(const MyString & str) { m_size=str.m_size; m_string=new char[m_size]; memcpy(m_string,str.m_string,m_size); cout<<"我是\t对象赋值运算"< cout<<"m_string的值是:\t"< return *this; } MyString & MyString::operator=(const char* c_str) { m_size=strlen(c_str)+1; m_string=new char[m_size]; memcpy(m_string,c_str,m_size); cout<<"我是\t字符串赋值运算"< cout<<"m_string的值是:\t"< return *this; } char MyString::operator[](const unsigned int index) { cout<<"我是\t取值运算"< if(index return '\0'; } bool MyString::operator>(const MyString &str) { cout<<"我是\t比较运算>"< if(*m_string>*(str.m_string)) return 1; return 0; } char* MyString::getString() { return m_string; } int MyString::length() { return m_size; } MyString MyString::strcat(const MyString & str) { char temp[255]={0}; ::strcpy(temp,m_string); ::strcat(temp,str.m_string); MyString m_str(temp); return m_str;//这用的是匿名对象做为返回值,还有其它方法.你根据自己需要自己改. } int main(int argc, char* argv[]) { /*这里是测试部分*/ MyString str("I like C++"); MyString strtest; strtest="You love girls"; strtest>str? cout<<"真的很大"< MyString three=strtest.strcat(str);//一个方法会引用多种方式\构造\挎贝\重写,自行研究. cout<<"three 是这样的:"< cout<<"打印第一个字符:\t"< /*这里是测试部分*/ return 0; } //希望能对你有所帮助。
#include
using namespace std;
class mystring
{
public:
mystring();
~mystring(){}
mystring(const char *str);
mystring(mystring &str);
mystring& operator =(const mystring& str);
mystring& operator =(const char *c_str);
char operator [](const unsigned int index);
mystring strcat (const mystring& str);
mystring strcat (const char *c_str);
mystring substring(int i,int j);//截取子链
mystring substring(int i);
bool operator >(const mystring& str);
bool operator >(const char *c_str);
bool operator >=(const mystring& str);
bool operator >=(const char *c_str);
bool operator <(const mystring& str);
bool operator <(const char *c_str);
bool operator <=(const mystring& str);
bool operator <=(const char *c_str);
mystring strcopy(mystring src);
mystring strcopy(char *str);
char *getstring();
int length();
void clear();
private:
char *m_string;
unsigned int m_size;
public:
friend ostream& operator <<(ostream& ostr,mystring& str)
{
ostr<
}
friend istream& operator >>(istream& istr,mystring& str)
{
char temp[255];
istr>>temp;
str=temp;
return istr;
}
};
mystring::mystring()
{
m_string=NULL;
m_size=0;
}
mystring::mystring(const char *str)
{
int i=0;
char *strings;
m_size=0;
while (str[m_size]!='\0')
{
m_size++;
}
m_string=new char[m_size+1];
strings=m_string;
while(str[i]!='\0')
{
*strings=str[i];
strings++;
i++;
}
*strings='\0';
}
mystring::mystring(mystring &str)
{
char *strings,*strings1;
strings1=str.m_string;
m_size=str.m_size;
strings=new char[m_size];
m_string=strings;
while(*strings1!='\0')
{
*strings=*strings1;
strings1++;
strings++;
}
*strings='\0';
}
char *mystring::getstring()
{
return m_string;
}
int mystring::length()
{
return m_size;
}
void mystring::clear()
{
if( !m_string )
{
delete []m_string;
m_string = NULL;
}
m_size = 0;
}
mystring& mystring::operator =(const mystring& str)
{
int i=0;
char *strings;
clear();
m_size=str.m_size;
strings=new char[m_size];
m_string=strings;
while(str.m_string[i]!='\0')
{
*strings=str.m_string[i++];
strings++;
}
*strings='\0';
return *this;
}
mystring& mystring::operator =(const char *c_str)
{
int i=0;
char *mm_string;
cout<
i++;
clear();
m_string=new char[i];
mm_string=m_string;
while (c_str[i]!='\0')
{
*mm_string=c_str[i];
i++;
mm_string++;
}
*mm_string='\0';
return *this;
}
char mystring::operator [](const unsigned int index)
{
return(m_string[index]);
}
mystring mystring::strcat (const mystring& str)
{
int i,j=0;
char *strings1,*strings2;
i=str.m_size+m_size;
strings1=new char[i+1];
strings2=strings1;
while (m_string[j]!='\0')
{
*strings1=m_string[j];
strings1++;
j++;
}
j=0;
while(str.m_string[j]!='\0')
{
*strings1=str.m_string[j];
strings1++;
j++;
}
*strings1='\0';
return mystring(strings2);
}
mystring mystring::strcat (const char *c_str)
{
int i=0,j=0;
char *strings1,*strings2;
while(c_str[i++]!='\0');
i+=m_size;
strings1=new char[i+1];
strings2=strings1;
while (m_string[j]!='\0')
{
*strings1=m_string[j];
strings1++;
j++;
}
j=0;
while(c_str[j]!='\0')
{
*strings1=c_str[j];
strings1++;
j++;
}
*strings1='\0';
return mystring(strings2);
}
mystring mystring::substring(int i,int j)
{
char *strings=new char[j];
int k=0;
while (k<=j-1)
strings[k++]=m_string[i];
strings[k]='\0';
return mystring(strings);
}
mystring mystring::substring(int i)
{
return substring(i,m_size-i+1);
}
bool mystring::operator >(const char *c_str)
{
unsigned int i=0;
bool flag;
while(m_string[i]!='\0'&&c_str[i]!='\0')
{
if (m_string[i]==c_str[i])
{
i++;
continue;
}
else if (m_string[i]>c_str[i])
{
flag=true;
break;
}
else
{
flag=false;
break;
}
}
if (i>=m_size) flag=false;
else flag=m_string[i]>c_str[i];
return flag;
}
bool mystring::operator >(const mystring& str)
{
mystring a(m_string);
return (a>str.m_string);
}
bool mystring::operator >=(const char *c_str)
{
unsigned int i=0;
bool flag;
while(m_string[i]!='\0'&&c_str[i]!='\0')
{
if (m_string[i]==c_str[i])
{
i++;
continue;
}
else if (m_string[i]>c_str[i])
{
flag=true;
break;
}
else
{
flag=false;
break;
}
}
if (m_string[i]=='\0'&&c_str[i]=='\0') flag=true;
else flag=m_string[i]>c_str[i];
return flag;
}
bool mystring::operator >=(const mystring& str)
{
mystring a(m_string);
return (a>=str.m_string);
}
bool mystring::operator <(const char *c_str)
{
mystring a(m_string);
return !(a>=c_str);
}
bool mystring::operator <(const mystring& str)
{
mystring a(m_string);
return !(a>=str.m_string);
}
bool mystring::operator <=(const mystring& str)
{
mystring a(m_string);
return !(a>str.m_string);
}
bool mystring::operator <=(const char *c_str)
{
mystring a(m_string);
return !(a>c_str);
}
mystring mystring::strcopy(char *str)
{
clear();
int i=0;
while (str[i]!='\0')
{
m_string[i]=str[i];
i++;
}
m_string[i]='\0';
m_size=i;
return *this;
}
mystring mystring::strcopy(mystring src)
{
clear();
int i=0;
while (src.m_string[i]!='\0')
{
m_string[i]=src.m_string[i];
i++;
}
m_string[i]='\0';
m_size=i;
return *this;
}
int main()
{
char asds[]="mmmm",bsd[]="mmmmmmmmm";
mystring a(asds);
mystring b(bsd);
a.strcopy(b);
cout< return 0;
}
you are so lucky!
这个要由高手来了