#include<iostream>
#include<string.h>
using namespace std;
class STR
{
public:
STR(char *str="") //字符串构造
{
m_str=new char[strlen(str)+1];
strcpy(m_str,str);
}
STR(char c) //字符构造
{
m_str=new char[2];
m_str[0]=c;
m_str[1]='\0';
}
STR(char c,int n) //n个相同字符构造
{
m_str=new char[n+1];
memset(m_str,c,n);
m_str[n]='\0';
}
STR(STR &s) //拷贝构造函数
{
m_str=new char[strlen(s.m_str)+1];
strcpy(m_str,s.m_str);
}
~STR() //析构函数
{
if(m_str!=NULL)
{
delete []m_str;
m_str=NULL;
}
}
STR& operator=(STR &s) //对象与对象的赋值
{
if(this==&s)
return *this;
if(m_str!=NULL)
delete []m_str;
m_str=new char[strlen(s.m_str)+1];
strcpy(m_str,s.m_str);
return *this;
}
STR& operator=(char *str) //字符串赋值给对象
{
if(m_str!=NULL)
delete []m_str;
m_str=new char[strlen(str)+1];
strcpy(m_str,str);
return *this;
}
STR& operator=(char c) //字符赋值给对象
{
if(m_str!=NULL)
delete []m_str;
m_str=new char[2];
m_str[0]=c;
m_str[1]='\0';
return *this;
}
STR operator+(STR &s) //两个对象相加
{
char *p=new char[strlen(m_str)+strlen(s.m_str)+1];
strcpy(p,m_str);
strcat(p,s.m_str);
STR t=p;
delete []p;
p=NULL;
return t;
}
STR& operator+=(STR &s) //两个对象相加,并将结果放入第一个对象中
{
char *p=new char[strlen(m_str)+strlen(s.m_str)+1];
strcpy(p,m_str);
strcat(p,s.m_str);
if(m_str!=NULL)
delete []m_str;
m_str=new char[strlen(m_str)+strlen(s.m_str)+1];
strcpy(m_str,p);
delete []p;
p=NULL;
return *this;
}
char& operator[](int n) //重载下标
{
if(n>=0 && n<strlen(m_str))
return m_str[n];
}
bool operator==(STR &s)
{
return strcmp(m_str,s.m_str)==0;
}
bool operator!=(STR &s)
{
return !(*this==s);
}
bool operator>(STR &s)
{
return strcmp(m_str,s.m_str)==1;
}
operator char*()
{
return m_str;
}
friend ostream& operator<<(ostream &out,STR &s);
void print()
{
cout<<m_str<<endl;
}
private:
char *m_str;
};
ostream& operator<<(ostream &out,STR &s)
{
out<<s.m_str;
return out;
}
void main()
{
STR s("123456");
STR a(s); //调用拷贝构造
STR c('m');
STR b('a',3);
c=b; //赋值
s="safgv";
b='1';
a=s+b;
a+=s;
s.print();
a.print();
c.print();
b.print();
cout<<a[3]<<endl;
cout<<(a==b)<<endl;
cout<<(a!=b)<<endl;
cout<<(a>b)<<endl;
cout<<(char*)a<<endl;
#include<string.h>
using namespace std;
class STR
{
public:
STR(char *str="") //字符串构造
{
m_str=new char[strlen(str)+1];
strcpy(m_str,str);
}
STR(char c) //字符构造
{
m_str=new char[2];
m_str[0]=c;
m_str[1]='\0';
}
STR(char c,int n) //n个相同字符构造
{
m_str=new char[n+1];
memset(m_str,c,n);
m_str[n]='\0';
}
STR(STR &s) //拷贝构造函数
{
m_str=new char[strlen(s.m_str)+1];
strcpy(m_str,s.m_str);
}
~STR() //析构函数
{
if(m_str!=NULL)
{
delete []m_str;
m_str=NULL;
}
}
STR& operator=(STR &s) //对象与对象的赋值
{
if(this==&s)
return *this;
if(m_str!=NULL)
delete []m_str;
m_str=new char[strlen(s.m_str)+1];
strcpy(m_str,s.m_str);
return *this;
}
STR& operator=(char *str) //字符串赋值给对象
{
if(m_str!=NULL)
delete []m_str;
m_str=new char[strlen(str)+1];
strcpy(m_str,str);
return *this;
}
STR& operator=(char c) //字符赋值给对象
{
if(m_str!=NULL)
delete []m_str;
m_str=new char[2];
m_str[0]=c;
m_str[1]='\0';
return *this;
}
STR operator+(STR &s) //两个对象相加
{
char *p=new char[strlen(m_str)+strlen(s.m_str)+1];
strcpy(p,m_str);
strcat(p,s.m_str);
STR t=p;
delete []p;
p=NULL;
return t;
}
STR& operator+=(STR &s) //两个对象相加,并将结果放入第一个对象中
{
char *p=new char[strlen(m_str)+strlen(s.m_str)+1];
strcpy(p,m_str);
strcat(p,s.m_str);
if(m_str!=NULL)
delete []m_str;
m_str=new char[strlen(m_str)+strlen(s.m_str)+1];
strcpy(m_str,p);
delete []p;
p=NULL;
return *this;
}
char& operator[](int n) //重载下标
{
if(n>=0 && n<strlen(m_str))
return m_str[n];
}
bool operator==(STR &s)
{
return strcmp(m_str,s.m_str)==0;
}
bool operator!=(STR &s)
{
return !(*this==s);
}
bool operator>(STR &s)
{
return strcmp(m_str,s.m_str)==1;
}
operator char*()
{
return m_str;
}
friend ostream& operator<<(ostream &out,STR &s);
void print()
{
cout<<m_str<<endl;
}
private:
char *m_str;
};
ostream& operator<<(ostream &out,STR &s)
{
out<<s.m_str;
return out;
}
void main()
{
STR s("123456");
STR a(s); //调用拷贝构造
STR c('m');
STR b('a',3);
c=b; //赋值
s="safgv";
b='1';
a=s+b;
a+=s;
s.print();
a.print();
c.print();
b.print();
cout<<a[3]<<endl;
cout<<(a==b)<<endl;
cout<<(a!=b)<<endl;
cout<<(a>b)<<endl;
cout<<(char*)a<<endl;
}
运行结果: