#include<iostream>
#include<string.h>
using namespace std;
class mystring
{
public:
mystring()
{
this->buf=new char('\0');
}
mystring(const char* str)
{
this->buf=new char[strlen(str)+1];
strcpy(buf,str);
}
mystring(const mystring& s)
{
this->buf=new char[strlen(s.buf)+1];
strcpy(buf,s.buf);
}
int length()
{
int ilen=0;
while(buf[ilen++]!='\0');
return ilen-1;
}
char operator[](int index)
{
return buf[index];
}
mystring& operator=(const mystring& s)
{
strcpy(buf,s.buf);
return *this;
}
friend mystring operator+(const mystring& s,const mystring& s1);
friend ostream& operator<<(ostream&,mystring);
~mystring()
{
delete []buf;
}
private:
char* buf;
};
ostream& operator<<(ostream& out,mystring m)
{
out<<m.buf;
return out;
}
mystring operator+(const mystring& s,const mystring s1)
{
mystring a;
a.buf=new char[strlen(s.buf)+strlen(s1.buf)+1];
strcpy(a.buf,s.buf);
strcat(a.buf,s1.buf);
return a;
}
完成string类(构造函数,拷贝,析构,重载运算符)
最新推荐文章于 2022-05-21 10:30:41 发布