功能
創新互聯成立與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元起,快前往官網查看詳情吧
網站標題:【c++實戰】之封裝字符串類-創新互聯
文章路徑:http://vcdvsql.cn/article2/cdgpic.html
成都網站建設公司_創新互聯,為您提供移動網站建設、網站營銷、響應式網站、電子商務、定制開發、網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯