重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
功能
创新互联成立与2013年,先为友谊等服务建站,友谊等地企业,进行企业商务咨询服务。为友谊企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。抽象类简介
属性:
char* m_data;
指向字符串首地址的指针
成员函数:
MyString()
{
this->m_data=new char[1]();
this->m_data[0]='\0';
cout<< "mystring void structure"<< endl;
}
MyString(const char* c_str)
{
int len=strlen(c_str);
this->m_data=new char[len+1];
memmove(this->m_data,c_str,len);
this->m_data[len]='\0';
cout<< "mystring exit structure"<< endl;
}
MyString(const MyString& other)
{
int len=strlen(other.m_data);
this->m_data=new char[len+1];
memmove(this->m_data,other.m_data,len);
this->m_data[len]='\0';
cout<< "mystring copy function"<< endl;
}
MyString& operator=(const MyString& other)
{
cout<< "mystring operator= function"<< endl;
if(this==&other){
return *this;
}
int len=strlen(other.m_data);
if(nullptr!=this->m_data)
{
delete [] m_data;
}
this->m_data=new char[len+1]();
memmove(this->m_data,other.m_data,len);
this->m_data[len]='\0';
return *this;
}
char operator[](int index)
{
cout<< "mystring operator[] function"<< endl;
if(index<0||index>=strlen(this->m_data))
{
cout<< "cross the border"<< endl;
}
return this->m_data[index];
}
MyString operator+(const MyString& other)
{
cout<< "mystring operator+ function"<< endl;
int my_len=strlen(this->m_data);
int other_len=strlen(other.m_data);
char* temp=new char[my_len+other_len+1]();
memmove(temp,this->m_data,my_len);
memmove(temp+my_len,other.m_data,other_len);
temp[my_len+other_len]='\0';
MyString temp_str=temp;
delete [] temp;
return temp_str;
}
MyString& append(const MyString& other)
{
cout<< "mystring append function"<< endl;
int my_len=strlen(this->m_data);
int other_len=strlen(other.m_data);
char* temp=new char[my_len]();
memmove(temp,this->m_data,my_len);
delete []this->m_data;
this->m_data=new char[my_len+other_len+1]();
memmove(m_data,temp,my_len);
delete [] temp;
memmove(m_data+my_len,other.m_data,other_len);
this->m_data[my_len+other_len+1]='\0';
return *this;
}
char* get_my_data()const
{
return this->m_data;
}
~MyString()
{
delete []m_data;
this->m_data=nullptr;
cout<< "mystring destruct"<< endl;
}
>>
运算符重载函数ostream& operator<< (ostream& cout,const MyString& other)
{
cout<< "operator<< function"<< endl;
cout<< other.get_my_data();
return cout;
}
程序源码:
#include#includeusing namespace std;
class MyString
{
private:
char* m_data;
public:
MyString()
{
this->m_data=new char[1]();
this->m_data[0]='\0';
cout<< "mystring void structure"<< endl;
}
MyString(const char* c_str)
{
int len=strlen(c_str);
this->m_data=new char[len+1];
memmove(this->m_data,c_str,len);
this->m_data[len]='\0';
cout<< "mystring exit structure"<< endl;
}
MyString(const MyString& other)
{
int len=strlen(other.m_data);
this->m_data=new char[len+1];
memmove(this->m_data,other.m_data,len);
this->m_data[len]='\0';
cout<< "mystring copy function"<< endl;
}
MyString& operator=(const MyString& other)
{
cout<< "mystring operator= function"<< endl;
if(this==&other){
return *this;
}
int len=strlen(other.m_data);
if(nullptr!=this->m_data)
{
delete [] m_data;
}
this->m_data=new char[len+1]();
memmove(this->m_data,other.m_data,len);
this->m_data[len]='\0';
return *this;
}
char operator[](int index)
{
cout<< "mystring operator[] function"<< endl;
if(index<0||index>=strlen(this->m_data))
{
cout<< "cross the border"<< endl;
}
return this->m_data[index];
}
MyString operator+(const MyString& other)
{
cout<< "mystring operator+ function"<< endl;
int my_len=strlen(this->m_data);
int other_len=strlen(other.m_data);
char* temp=new char[my_len+other_len+1]();
memmove(temp,this->m_data,my_len);
memmove(temp+my_len,other.m_data,other_len);
temp[my_len+other_len]='\0';
MyString temp_str=temp;
delete [] temp;
return temp_str;
}
MyString& append(const MyString& other)
{
cout<< "mystring append function"<< endl;
int my_len=strlen(this->m_data);
int other_len=strlen(other.m_data);
char* temp=new char[my_len]();
memmove(temp,this->m_data,my_len);
delete []this->m_data;
this->m_data=new char[my_len+other_len+1]();
memmove(m_data,temp,my_len);
delete [] temp;
memmove(m_data+my_len,other.m_data,other_len);
this->m_data[my_len+other_len+1]='\0';
return *this;
}
char* get_my_data()const
{
return this->m_data;
}
~MyString()
{
delete []m_data;
this->m_data=nullptr;
cout<< "mystring destruct"<< endl;
}
};
ostream& operator<< (ostream& cout,const MyString& other)
{
cout<< "operator<< function"<< endl;
cout<< other.get_my_data();
return cout;
}
int main()
{
MyString str1;//无参构造
str1="yao";//=运算符函数
MyString str2="liang";//隐式传参构造
MyString str3("hello");//显示传参构造
MyString str4=str3;//拷贝构造
cout<< str3.get_my_data()<< endl;//MyString类型的指针转化为char类型指针输出
cout<< str3<< endl;//<<运算符函数
cout<< str4[0]<< endl;//[]运算符函数
cout<< str1+str2<< endl;//+运算符函数&&<<运算符函数
cout<< str1.append(str2)<< endl;//追加成员函数&&<<运算符函数
cout<< str1<< endl;//追加后结果发生改变&&<< 运算符函数
return 0;
}
运行结果分析:
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧