代码实现
#include <iostream>
#include <string.h>
using namespace std;
class my_string
{
private:
char *str;
int len;
public:
my_string(){}
my_string(char *q,int i)
{
str=new char[128];
strcpy(str,q);
len = i;
cout<<"this is a has parament constroctor"<<endl;
}
my_string(const my_string &O)
{
str=new char[128];
this->len = O.len;
strcpy(str,O.str);
}
my_string& operator=(const my_string &O)
{
if(this!=&O)
{
len = O.len;
str = new char[128];
strcpy(str,O.str);
}
return *this;
}
bool my_empty()
{
if(*str='\0')
return true;
else
return false;
}
int my_size()
{
int i=0;
while(*(str+i)!=0)
{
i++;
}
return i;
}
char *my_str()
{
return str;
}
};
int main()
{
my_string str1("hello world!",10);
cout<<"str1:"<<str1.my_str()<<endl;
my_string str2("h",10);
cout<<"str2:"<<str2.my_str()<<endl;
my_string str3=str1;
cout<<"str3"<<str3.my_str()<<endl;
my_string str4;
str4=str3;
my_string str5("",0);
if(str5.my_empty())
cout<<" no str5"<<endl;
else
cout<<"have str5"<<endl;
if(str4.my_empty())
cout<<"no str4"<<endl;
else
cout<<"have str4"<<endl;
int len=str1.my_size();
cout<<"the lenth of str1:"<<len<<endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果