【c++实战】之封装字符串类

功能

  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 <iostream>
#include <string.h>
using 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;
}

运行结果分析
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程远泊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值