C
chicky
#include
#include
#include
class Tstring
{
private:
char *str;
int size;
public:
Tstring();
~Tstring();
Tstring & operator=(char *);
Tstring & operator=(const Tstring &);
friend int operator==(Tstring &,Tstring &);
friend int operator!=(Tstring &,Tstring &);
friend Tstring operator+(Tstring &,Tstring &);
friend Tstring operator-(Tstring &,Tstring &);
friend ostream & operator<<(ostream &,Tstring &);
friend istream & operator>>(istream &,Tstring &);
};
//------------------------------------------------------构造析构函数-------------
Tstring::Tstring()
{
str=NULL;
}
Tstring::~Tstring()
{
delete str;
}
//------------------------------------------------------赋值函数----------------
Tstring & Tstring::operator=(char *s)
{
size=strlen(s)+1;
str=new char[size];
if(str==NULL)
{
cout<<"Tstring::operator=(char *)--Error!"<str;
len=s1.size+s2.size-1;
temp->str=new char[len];
if(temp->str==NULL)
{
cout<<"Tstring operator+(Tstring &,Tstring &)--Error!"<size=len;
strcpy(temp->str,s1.str);
strcat(temp->str,s2.str);
return *temp;
}
Tstring operator-(Tstring &s1,Tstring &s2)
{
Tstring *temp;
temp=new Tstring;
delete [] temp->str;
int len;
len=s1.size-s2.size+1;
temp->str=new char[len];
if(temp->str==NULL)
{
cout<<"Tstring operator-(Tstring &,Tstring &)--Error!"<str[i]=s1.str[i];
temp->str[n]='\0';
strcat(temp->str,q);
return *temp;
}
//------------------------------------------------------输入输出函数------------
istream & operator>>(istream &istr,Tstring &s)
{
char *x;
int n;
cout<<"Input string length:";
istr>>n;
x=new char[n];
if(x==NULL)
{
cout<<"istream & operator>>(istream &,Tstring &)--Error!"<>x[i];
x[n]='\0';
s=x;
delete [] x;
return istr;
}
ostream & operator<<(ostream &ostr,Tstring &s)
{
ostr<>a;
cout<<"String-1 is:"<>b;
cout<<"String-2 is:"<>d;
cout<<"String-3 is:"<
小鸡无毛飞不高