谢谢。

解决方案 »

  1.   

    #include <iostream>
    using namespace std ;
    class StrType
     {
    friend ostream& operator<<(ostream& stream, StrType& o);
    friend istream& operator>>(istream& stream, StrType& o);
    friend StrType operator+(char *s, StrType &o);
     // concatenate a quoted string with a StrType object public:
    StrType():_p(0),_size(0){};
    StrType(char *str);//constructor with quoted string parameter
    StrType(const StrType &o); // copy constructor
    ~StrType();//destructor
    StrType operator=(StrType& o); // assign a StrType object
    StrType operator=(char* s); // assign a quoted string
    StrType operator+(StrType& o); // concatenate a StrType object
    StrType operator+(char* s); // concatenate a quoted string
    void show()
    {
      cout<<_p<<endl;
    }
    int operator==(StrType& o);
    int operator!=(StrType& o);
    // operations between StrType objects and quoted strings
    int operator==(char* s);
    int strsize(); // return size of string
    //operator char* (); // conversion to char *
    private:
    char* _p;
    int _size;
    };StrType::StrType(char *str):_size(strlen(str)+1)
    {
    _p = new char[_size+1];
    strcpy(_p,str);
    }
    StrType::StrType(const StrType &o):_size(o._size){
    _p = new char[_size+1];
    strcpy(_p,o._p);
    }
    StrType::~StrType()
    {
    if(_p)
      delete [] _p;
    }
    StrType StrType::operator =(StrType &o)
    {
    if(*this != o)
    {
    delete [] this->_p;
    this->_p = new char[o._size+1];
    strcpy(this->_p,o._p);
    }
    return *this;
    }
    StrType StrType::operator=(char * str)
    {
    delete [] this->_p;
    this->_p = new char[strlen(str)+1];
    strcpy(this->_p,str);
    return *this;
    }
    StrType StrType::operator+(StrType &o)
    {
    StrType s;
    s._size = _size+o._size-1;
    s._p = new char[s._size];
    strcpy(s._p,this->_p);
    strcat(s._p,o._p);
    return s;
    }
    StrType StrType::operator+(char *str)
    {
    StrType s;
    s._size = _size+strlen(str);
    s._p = new char[s._size];
    strcpy(s._p,this->_p);
    strcat(s._p,str);
    return s;
    }int StrType::operator==(StrType & o)
    {
    return strcmp(this->_p,o._p);
    }
    int StrType::operator!=(StrType &o)
    {
    return !(operator==(o));
    }int StrType::operator ==(char * str)
    {
    return strcmp(this->_p,str);
    }int StrType::strsize()
    {
    return this->_size - 1;
    }ostream& operator<<(ostream& stream, StrType& o)
    {
    stream<<o._p;
    return stream;
    }
    istream& operator>>(istream& stream, StrType& o)
    {
    stream>>o._p;
    return stream;
    }
    StrType operator+(char *s, StrType &o)
    {
    char * temp = new char[strlen(s)+o._size+1];
    strcpy(temp,s);
    strcat(temp,o._p);
    return StrType(temp);
    }
      

  2.   

    Class string
    {
    Char * str;

    public:
    String(char *s=0);
    String(const String & s);

    ~string();

    string & string::operator=(string & s);

    }String::String(char *s=0);
    {
    If(s)
    {
    str=new char[strlen(s)+1];
    Strcpy(str,s);
    }
    Else str=0
    }String::String(const String & s)
    {
    str=new char[strlen(s.str)+1];
    Strcpy(str,s.str);
    }
    String::~string()
    {
    If(str)
    {
    delete[]str;
    }
    }
    String & string::operator=(string & s)
    {
    if(this != &s)
    {
    If(str)       
    {
    delete[]str;
    }

    Str=new char[strlen(s.str)+1];
    Strcpy(str, s.str);
    Return *this ;

    }

    }