#include <iostream>
#include <string.h>
using namespace std;
class Mystring{
friend istream& operator >>(istream &in,const Mystring& another);
friend ostream& operator <<(ostream &out,const Mystring& another);
public:Mystring(const char *s = nullptr);
Mystring(const Mystring& another);
Mystring& operator =(const Mystring& another);
~Mystring();
const Mystring operator +(const Mystring& another);
private:char *_str;
};
istream& operator >>(istream &in,const Mystring& another)
{in >> another._str;
return in;
}
ostream& operator <<(ostream &out,const Mystring& another)
{out << another._str << endl;
return out;
}
Mystring::Mystring(const char *s)
{if(s == nullptr)
{_str = new char[1];
*_str = '\0';
}else
{_str = new char[strlen(s)+1];
strcpy(_str,s);
}}
Mystring::Mystring(const Mystring& another)
{_str = new char[strlen(another._str)+1];
strcpy(this->_str,another._str);
}
Mystring& Mystring::operator =(const Mystring& another)
{if(this == &another)
return *this;
delete [] this->_str;
this->_str = new char[strlen(another._str)+1];
strcpy(this->_str,another._str);
return *this;
}
Mystring::~Mystring()
{delete [] _str;
}
const Mystring Mystring::operator +(const Mystring& another)
{int len = strlen(this->_str)+strlen(another._str);
Mystring ms;
delete [] ms._str;
ms._str = new char[len+1]{0};
strcat(strcat(ms._str,this->_str),another._str);
return ms;
}
版权声明:本文为ch_frank原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。