仿照string类,写一个my_string类
class my_string
{
private:char *str;
int len;
publuc:
//无参构造
//有参构造
//拷贝构造
//拷贝赋值
//bool my_empty() 判空
//int my_size() 求长度
//char *my_str() 转化为c风格字符串
}
要求:前四个必须实现
#include <iostream>
#include <string>
using namespace std;
class student
{
private:
char *str;
int len;
public:
student(){}//无参构造函数
~student(){}//析构函数
student(char *p)//有参构造函数
{
len=my_size(p);
str=new char[len];
for(int i=0;i<len;i++)
{
str[i]=*(p+i);
}
}
student(student& s)//拷贝构造函数
{
this->len=s.len;
str=new char[len];
for(int i=0;i<len;i++)
{
str[i]=s.str[i];
}
}
student& operator=(student &R)
{
this->len=R.len;
this->str=new char[len];
for(int i=0;i<len;i++)
{
str[i]=R.str[i];
}
return *this;
}
void show()
{
cout<<"str="<<this->str<<endl;
}
//长度
int my_size(char *s)
{
int len=0;
while(*s!='\0')
{
len++;
s++;
}
return len+1;
}
//判空
bool my_empty()
{
return len==0?true:false;
}
};
int main()
{
student s1("hello");
s1.show();
student s2(s1);
s2.show();
student s3;
s3=s2;
s3.show();
if(s1.my_empty()==true)
{
cout<<"null"<<endl;
}
else
{
cout<<"full"<<endl;
}
return 0;
}