#inclutream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
class Mystring
{
public:
Mystring()
{
str = nullptr;
len = 0;
cout<<"无参构造函数"<<endl;
}
Mystring(const char* s)
{
len = strlen(s);
str = new char[len];
strcpy(str,s);
cout<<"有参构造函数"<<endl;
}
//拷贝构造函数
//拷贝赋值函数
//析构函数
~Mystring()
{
if(nullptr != str)
{
delete []str;
}
cout<<"析构函数"<<endl;
}
void data()
{
cout<<"s="<<str<<endl;
}
void size()
{
cout<<"s的长度="<<len<<endl;
}
void length()
{
cout<<"s的长度="<<len<<endl;
}
void empty()
{
if(strcmp(str,"")==0)
{
cout<<"True"<<endl;
return;
}
else
{
cout<<"False"<<endl;
return;
}
}
//at()
void at(int n)
{
cout<<str[n]<<endl;
if(n>len-1)
{
cout<<"out of range"<<endl;
}
}
private:
char *str;
int len;
};
int main(int argc, const char *argv[])
{
Mystring s("nihao");
s.data();
s.size();
s.length();
s.empty();
s.at(1);
return 0;
}
C++实现Mystring
最新推荐文章于 2024-11-10 14:57:19 发布