【c++實(shí)戰(zhàn)】之封裝字符串類-創(chuàng)新互聯(lián)

功能

創(chuàng)新互聯(lián)成立與2013年,先為友誼等服務(wù)建站,友誼等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為友誼企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
  1. 無(wú)參構(gòu)造字符串
  2. 有參構(gòu)造字符串
  3. 拷貝構(gòu)造字符串
  4. =運(yùn)算符重載,字符串之間進(jìn)行賦值
  5. []運(yùn)算符重載,通過(guò)下標(biāo)尋找字符
  6. +運(yùn)算符重載,字符串之間進(jìn)行拼接
  7. 自定義追加成員函數(shù),在原字符串上追加其他字符串
  8. get函數(shù),返回私有屬性指針
  9. 析構(gòu)函數(shù),先清理內(nèi)存空間,后釋放內(nèi)存
  10. <<運(yùn)算符重載,獲取指向字符串首地址的指針輸出字符串

抽象類簡(jiǎn)介

屬性

char* m_data;

指向字符串首地址的指針

成員函數(shù)

  • 無(wú)參構(gòu)造函數(shù):
MyString()
{
    this->m_data=new char[1]();
    this->m_data[0]='\0';
    cout<< "mystring void structure"<< endl;
}
  • 有參構(gòu)造函數(shù)(在c++中字符串一定是常量類型,相當(dāng)于c語(yǔ)言中的常量區(qū)):
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;
}
  • 拷貝構(gòu)造函數(shù):
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;
}
  • =運(yùn)算符重載函數(shù)
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;
}
  • []運(yùn)算符重載函數(shù)
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];
}
  • +運(yùn)算符重載函數(shù)
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;
}
  • 自定義append追加成員函數(shù)
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;
}
  • get函數(shù)
char* get_my_data()const
{
    return this->m_data;
}
  • 析構(gòu)函數(shù)
~MyString()
{
    delete []m_data;
    this->m_data=nullptr;
    cout<< "mystring destruct"<< endl;
}
  • >>運(yùn)算符重載函數(shù)
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;//無(wú)參構(gòu)造
    str1="yao";//=運(yùn)算符函數(shù)
    MyString str2="liang";//隱式傳參構(gòu)造
    MyString str3("hello");//顯示傳參構(gòu)造
    MyString str4=str3;//拷貝構(gòu)造
    cout<< str3.get_my_data()<< endl;//MyString類型的指針轉(zhuǎn)化為char類型指針輸出
    cout<< str3<< endl;//<<運(yùn)算符函數(shù)
    cout<< str4[0]<< endl;//[]運(yùn)算符函數(shù)
    cout<< str1+str2<< endl;//+運(yùn)算符函數(shù)&&<<運(yùn)算符函數(shù)
    cout<< str1.append(str2)<< endl;//追加成員函數(shù)&&<<運(yùn)算符函數(shù)
    cout<< str1<< endl;//追加后結(jié)果發(fā)生改變&&<< 運(yùn)算符函數(shù)
    return 0;
}

運(yùn)行結(jié)果分析
在這里插入圖片描述

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)站標(biāo)題:【c++實(shí)戰(zhàn)】之封裝字符串類-創(chuàng)新互聯(lián)
文章路徑:http://muchs.cn/article2/cdgpic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷、響應(yīng)式網(wǎng)站電子商務(wù)、定制開(kāi)發(fā)、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)