C++ 封装mystring类

代码

#include <iostream>
#include <cstring>
using namespace std;
class MyString
{
public:
    MyString();
    MyString(const char *str);
    MyString(const MyString &obj);

    //[]重载
    char & operator[](int Index);


    //=重载
    MyString & operator=(const char *str);
    MyString & operator=(const MyString &obj);


    //字符串拼接 重载+号
    MyString operator+(const char *str);
    MyString operator+(const MyString &obj);


    //字符串比较
    bool operator==(const char *str);
    bool operator==(const MyString &obj);
    void StringShow();
private:
    char *pString;
    int m_size;
};

MyString::MyString()
{
    this->pString = NULL;
    this->m_size = 0;
}

MyString::MyString(const char *str)
{
    this->m_size = strlen(str) + 1;
    this->pString = new char[m_size];
    strcpy(this->pString,str);
}

MyString::MyString(const MyString &obj)
{
    this->m_size = obj.m_size;
    this->pString = new char[m_size];
    strcpy(this->pString,obj.pString);
}
//[]重载
char& MyString::operator[](int Index)
{
    static char ch = -1;
    if(Index >= 0 && Index < this->m_size)
    {
        return this->pString[Index];
    }
    else
    {
        cout<<"越界访问!"<<endl;
        return ch;
    }
}

//=重
MyString & MyString::operator=(const char *str)
{
    this->m_size = strlen(str) + 1;
    if(this->pString != NULL)
    {
        delete []this->pString;
        this->pString = new char[m_size];
    }
    else
    {
        this->pString = new char[m_size];
    }
    strcpy(this->pString,str);

}
MyString & MyString::operator=(const MyString &obj)
{
    this->m_size = obj.m_size;
    if(this->pString != NULL)
    {
        delete []this->pString;
        this->pString = new char[m_size];
    }
    else
    {
        this->pString = new char[m_size];
    }
    strcpy(this->pString,obj.pString);    
}


//字符串拼接 重载+号
MyString MyString::operator+(const char *str)
{
    if(this->pString != NULL)
    {
        char *s = new char[this->m_size + 1];
        strcpy(s,this->pString);
        delete []this->pString;
        this->pString = new char[this->m_size + strlen(str) + 1];
        strcpy(this->pString,s);
        strcat(this->pString,str);
        this->m_size += strlen(str);
        delete []s;
    }
    else
    {
        this->m_size = strlen(str) + 1;
        this->pString = new char[m_size];
        strcpy(this->pString,str); 
    }
    return *this;
}
MyString MyString::operator+(const MyString &obj)
{
    if(this->pString != NULL)
    {
        char *s = new char[this->m_size + 1];
        strcpy(s,this->pString);
        delete []this->pString;
        this->pString = new char[this->m_size + strlen(obj.pString) + 1];
        strcpy(this->pString,s);
        strcat(this->pString,obj.pString);
        this->m_size += strlen(obj.pString);
        delete []s;
    }
    else
    {
        this->m_size = strlen(obj.pString) + 1;
        this->pString = new char[m_size];
        strcpy(this->pString,obj.pString); 
    }
    return *this;    
}


//字符串比较
bool MyString::operator==(const char *str)
{
    if(strcmp(this->pString,str) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool MyString::operator==(const MyString &obj)
{
    if(strcmp(this->pString,obj.pString) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }    
}

void MyString::StringShow()
{
    cout<<"字符串的长度为:"<<this->m_size - 1<<endl;
    cout<<"字符串的内容为:"<<this->pString<<endl;
}

int main(int argc, char const *argv[])
{
    MyString s1;
    MyString m1("helloworld");
    m1.StringShow();


    MyString m2(m1);
    m2.StringShow();
    cout<<m2[0]<<endl;
    cout<<m2[3]<<endl;
    cout<<m2[30]<<endl;
    
    m2 = "nihao najing";
    m2.StringShow();
    MyString m3;
    //m3 = m2 = m1; 
    //m1.StringShow();
    //m2.StringShow();
    //m3.StringShow();
    MyString m4;
    m4 = m4 + "helloworld";
    m4.StringShow();
    MyString m5;
    m5 = m4 + "hahahhaha";
    m4.StringShow();
    MyString m6("88888888");
    m6 = m6 + m5;
    m6.StringShow();    
    if(m6 == m5)
    {
        cout<<"m6 = m5"<<endl;
    }
    else
    {
        cout<<"m6 != m5 "<<endl;
    }
    return 0;
}







终端

judy@judy-virtual-machine:~/桌面/2022/0303$ g++ mystring.cpp -o mystring
^[[Ajudy@judy-virtual-machine:~/桌面/2022/0303$ ./mystring
字符串的长度为:10
字符串的内容为:helloworld
字符串的长度为:10
字符串的内容为:helloworld
h
l
越界访问!
�
字符串的长度为:12
字符串的内容为:nihao najing
字符串的长度为:10
字符串的内容为:helloworld
字符串的长度为:19
字符串的内容为:helloworldhahahhaha
字符串的长度为:27
字符串的内容为:88888888helloworldhahahhaha
m6 != m5 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值