成都创新互联网站制作重庆分公司

【c++实战】之封装字符串类-创新互联

功能

创新互联成立与2013年,先为友谊等服务建站,友谊等地企业,进行企业商务咨询服务。为友谊企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
  1. 无参构造字符串
  2. 有参构造字符串
  3. 拷贝构造字符串
  4. =运算符重载,字符串之间进行赋值
  5. []运算符重载,通过下标寻找字符
  6. +运算符重载,字符串之间进行拼接
  7. 自定义追加成员函数,在原字符串上追加其他字符串
  8. get函数,返回私有属性指针
  9. 析构函数,先清理内存空间,后释放内存
  10. <<运算符重载,获取指向字符串首地址的指针输出字符串

抽象类简介

属性

char* m_data;

指向字符串首地址的指针

成员函数

  • 无参构造函数:
MyString()
{
    this->m_data=new char[1]();
    this->m_data[0]='\0';
    cout<< "mystring void structure"<< endl;
}
  • 有参构造函数(在c++中字符串一定是常量类型,相当于c语言中的常量区):
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;
}
  • 自定义append追加成员函数
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函数
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://cxhlcq.com/article/cdgpic.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部