1、完成下面类(已经过修改)
#include <iostream>
#include <cstring>
using namespace std;
class myString
{
private:
char *str; //记录c风格的字符串
int size; //记录字符串的实际长度
public:
//无参构造
myString():size(10)
{
str = new char[size]; //构造出一个长度为10的字符串
strcpy(str,"");
cout << "myString的无参构造" << endl;
}
//有参构造
myString(const char *s)
{
size = strlen(s);
str = new char[size+1];
strcpy(str,s);
cout << "myString的有参构造" << endl;
}
//拷贝构造
myString(const myString &other):str(new char[other.size]),size(other.size)
{
strcpy(str,other.str);
cout << "myString的拷贝构造函数" << endl;
}
//析构函数
~myString()
{
delete []str;
}
//判空函数
bool myempty()
{
string s1 = str;
return s1.empty();
}
//size函数
int mysize()
{
string s1 = str;
return s1.size();
}
//c_str函数
const char *myc_str()
{
return str;
}
//at函数
char &at(int pos)
{
string s1 = str;
if(pos<0 || pos>=size-1)
{
cout << "访问越界" << endl;
}
return s1.at(pos);
}
void show()
{
cout << "str = " << str << endl;
cout << "size = " << size << endl;
}
};
int main()
{
myString s1;
s1.show();
myString s2("Hello World");
s2.show();
myString s3(s2);
return 0;
}