C++ : 封装一个字符串类:代码实现,注释详细

本代码使用C++编写程序实现,用到了构造函数、拷贝构造、运算符重载函数(+、=、[ ]、<<)等等。

#include <iostream>
#include <cstring>

using namespace std;
class MyString
{
private:
    char* m_data;
public:
    //默认无参构造
    MyString(){
        this->m_data = new char[1];
        this->m_data[0]='\0';
    }
    //使用外部C风格字符串初始化类中属性
    MyString(const char* c_str)
    {
        int len = strlen(c_str);
        this->m_data = new char[len+1];
        memcpy(this->m_data,c_str,len);
        this->m_data[len] = '\0';
    }
    //字符串类的拷贝构造
    MyString(const MyString& other)
    {
        int len = strlen(other.m_data);
        this->m_data = new char[len+1];
        memcpy(this->m_data,other.m_data,len);
        this->m_data[len] = '\0';
    }
    //字符串类的=号运算符函数的实现
    MyString& operator = (const MyString& other)
    {
        if(this == &other)
        {
            return *this;
        }
        int len = strlen(other.m_data);
        if(this->m_data != nullptr)
        {
            delete []m_data;
            this->m_data = new char[len+1];
        }else{
            this->m_data = new char[len+1];
        }
        memcpy(this->m_data,other.m_data,len);
        this->m_data[len] = '\0';
        return *this;
    }
    //字符长度函数逻辑
    int size()
    {
        return strlen(this->m_data);
    }
    //字符串中的中括号运算符函数,此处没用引用是因为 char为1字节,而引用为8字节
    char operator[](int index)
    {
        if(index < 0 || index > this->size())
        {
            cout <<"下标越界"<<endl;
            return -1;
        }
        return this->m_data[index];
    }
    //实现+运算符重载函数
    MyString& operator + (const MyString& other)
    {
        //拼接字符串的代码逻辑
//      strncat(this->m_data,other.m_data,strlen(other.m_data));
        //拼接字符串的另种方法,相当于自写实现strncat的功能
        int len = strlen(this->m_data) + strlen(other.m_data);
        char *temp = new char[len+1];
        memcpy(temp,this->m_data,strlen(this->m_data));
        memcpy(temp+strlen(this->m_data),other.m_data,strlen(other.m_data));
        temp[len] = '\0';
        delete []this->m_data;
        this->m_data = temp;
        return *this;
    }
    //获取字符指针的get方法
    char* getM_data()const
    {
        return this->m_data;
    }

};
//实现 << 运算符函数
ostream& operator << (ostream& cout,const MyString& other)
{
    cout << other.getM_data();
    return cout;
}

int main()
{
    MyString stu1 = "zhangsan";
    MyString stu2 = "lisi";
    cout << stu1 <<endl; //cout输出函数只可以为内置类型。
                         //若没有运算符重载,cout就不认的 stu1
    operator <<(cout,stu1) <<endl; //跟上行一样的功能,另一种写法
    cout << stu1[0] <<endl;//访问stu1中的第0个元素

    cout <<stu1 + stu2 << endl; //字符串的拼接,此时stu1为 zhangsanlisi
    cout <<stu1.operator+(stu2) << endl; //功能跟上行一样,拼接字符串的另一种写法

    stu1 = stu2; //将stu2赋给stu1 测试 = 号运算符函数
    cout << stu1 <<endl;
    return 0;
}

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值