#pragma once

class Cstring{
Cstring():m_pData(new char[1]){ *m_pData='\0';} //默认构造

Cstring(const char *pStr):m_pData(new char[strlen(pStr)+1]){ strcpy(m_pData,pStr); }

Cstring(const Cstring &str):m_pData(new char[str.size()+1]){strcpy(m_pData,str.m_pData);} //赋值构造

friend ostream& operator<<(ostream& os,Cstring& str);
friend istream& operator>>(istream& is,Cstring& str);

~Cstring(){ delete [] m_pData;} //析构
public:
size_t size() const
{
return strlen(m_pData);
}
Cstring & operator = ( const Cstring & other);
Cstring & operator = ( const char * pchar);
Cstring & operator + ( const Cstring & other);
Cstring & operator + ( const Cstring & other) const;
private:
char * m_pData;
};

inline Cstring& Cstring ::operator =( const Cstring & other){
if (this != &other)
{
delete [] m_pData;
m_pData = new char[other.size()+1];
strcpy(m_pData,other.m_pData);
}
return *this;
}

Cstring & Cstring::operator = ( const char * pchar)
{
if (m_pData != NULL)
{
delete [] m_pData;
}
size_t sz = strlen(pchar);
m_pData = new char[sz+1];
strcpy(m_pData,pchar);
return *this;
}
inline Cstring& Cstring::operator +(const Cstring & other) //改变被加数
{
char * temp = m_pData;
m_pData = new char[strlen(m_pData)+strlen(other.m_pData)+1];
strcpy(m_pData,temp);
delete []temp;
strcat(m_pData,other.m_pData);
return *this;
}

inline Cstring& Cstring::operator +(const Cstring & other) const //不改变被加数
{
Cstring strnew(); //Cstring* pstrnew = new Cstring();
delete [] strnew.m_pData;
strnew.m_pData = new char[strlen(m_pData)+strlen(other.m_pData)+1];
strcpy(strnew.m_pData,m_pData);
strcat(strnew.m_pData,other.m_pData);
return strnew;
}
ostream& Cstring::operator<<(ostream& os,Cstring& str)
{
os<< str.m_pData;
return os;
}

istream& Cstring::operator>>(istream& is,Cstring& str)
{
char arrchar[100];
is.getline(arrchar,100);
str = arrchar;
return is;
}