![]() |
#2
yang04012012-08-03 11:15
|

#include <iostream>
#include <string>
using namespace std;
class st
{
public:
st();
st(const char * const);
st(const st &);
~st();
char & operator[](int offset);
char operator[](int offset)const;
st operator+(const st);
void operator +=(const st);
st &operator =(const st &);
int getlen()const{return itslen;}
const char * getst()const {return itsst;}
private:
st(int);
char * itsst;
unsigned short itslen;
};
st::st()
{
itsst=new char[1];
itsst[0]='\0';
itslen=0;
cout << "\t default st cinstructor\n";
}
st::st(int len)
{
itsst=new char[len+1];
for(int i=0;i<len; i++)
itsst[i]='\0';
itslen=len;
cout << "\t st(int) constructor\n";
}
st::st(const char * const cst)
{
itslen=strlen(cst); //获得字符的长度
itsst = new char[itslen+1];
for(int i=0; i< itslen; i++)
itsst[i]=cst[i];
itsst[itslen]='\0';
cout << "\tst(char * )constructot\n";
}
st::st(const st & rhs)
{
itslen=rhs.getlen();
itsst =new char[itslen+1];
for(int i=0; i<itslen; i++)
itsst[i]=rhs[i];
itsst[itslen]='\0';
cout <<"\t st(st &) constructor";
}
st::~st()
{
delete [] itsst;
itslen=0;
cout << "\t st destructor";
}
st& st::operator =(const st & rhs)
{
if(this == &rhs)
return *this;
delete [] itsst;
itslen =rhs.getlen();
itsst= new char[itslen +1];
for(int i=0;i<itslen; i++)
itsst[i]=rhs[i];
itsst[itslen]= '\0';
return *this;
cout << "\tst operator =\n";
}
char st::operator[](int offset)const
{
if(offset > itslen)
return itsst[itslen-1];
else
return itsst[offset];
}
char& st::operator[](int offset)
{
if(offset > itslen)
return itsst[itslen-1];
else
return itsst[offset];
}
st st::operator +(const st rhs)
{
int totallen = itslen + rhs.getlen();
st temp(totallen);
int i,j;
for(i=0; i<itslen; i++)
temp[i]=itsst[i];
for(j=0;j<rhs.getlen();j++,i++)
temp[i]=rhs[j];
temp[totallen]='\0';
return temp;
cout << "\tst operator+\n";
}
void st::operator+=(const st rhs)
{
unsigned short rhslen=rhs.getlen();
unsigned short totallen=itslen+rhslen;
st temp(totallen);
int i,j;
for(int i=0; i<itslen;i++)
temp[i]=itsst[i];
for(j=0; j<rhs.getlen(); j++,i++)
temp[i]=rhs[j];
temp[totallen]='\0';
*this =temp;
}
#include <string>
using namespace std;
class st
{
public:
st();
st(const char * const);
st(const st &);
~st();
char & operator[](int offset);
char operator[](int offset)const;
st operator+(const st);
void operator +=(const st);
st &operator =(const st &);
int getlen()const{return itslen;}
const char * getst()const {return itsst;}
private:
st(int);
char * itsst;
unsigned short itslen;
};
st::st()
{
itsst=new char[1];
itsst[0]='\0';
itslen=0;
cout << "\t default st cinstructor\n";
}
st::st(int len)
{
itsst=new char[len+1];
for(int i=0;i<len; i++)
itsst[i]='\0';
itslen=len;
cout << "\t st(int) constructor\n";
}
st::st(const char * const cst)
{
itslen=strlen(cst); //获得字符的长度
itsst = new char[itslen+1];
for(int i=0; i< itslen; i++)
itsst[i]=cst[i];
itsst[itslen]='\0';
cout << "\tst(char * )constructot\n";
}
st::st(const st & rhs)
{
itslen=rhs.getlen();
itsst =new char[itslen+1];
for(int i=0; i<itslen; i++)
itsst[i]=rhs[i];
itsst[itslen]='\0';
cout <<"\t st(st &) constructor";
}
st::~st()
{
delete [] itsst;
itslen=0;
cout << "\t st destructor";
}
st& st::operator =(const st & rhs)
{
if(this == &rhs)
return *this;
delete [] itsst;
itslen =rhs.getlen();
itsst= new char[itslen +1];
for(int i=0;i<itslen; i++)
itsst[i]=rhs[i];
itsst[itslen]= '\0';
return *this;
cout << "\tst operator =\n";
}
char st::operator[](int offset)const
{
if(offset > itslen)
return itsst[itslen-1];
else
return itsst[offset];
}
char& st::operator[](int offset)
{
if(offset > itslen)
return itsst[itslen-1];
else
return itsst[offset];
}
st st::operator +(const st rhs)
{
int totallen = itslen + rhs.getlen();
st temp(totallen);
int i,j;
for(i=0; i<itslen; i++)
temp[i]=itsst[i];
for(j=0;j<rhs.getlen();j++,i++)
temp[i]=rhs[j];
temp[totallen]='\0';
return temp;
cout << "\tst operator+\n";
}
void st::operator+=(const st rhs)
{
unsigned short rhslen=rhs.getlen();
unsigned short totallen=itslen+rhslen;
st temp(totallen);
int i,j;
for(int i=0; i<itslen;i++)
temp[i]=itsst[i];
for(j=0; j<rhs.getlen(); j++,i++)
temp[i]=rhs[j];
temp[totallen]='\0';
*this =temp;
}