C++仿照string类实现自定义My_strng类

代码演示

#include <iostream>
#include <cstring>

using namespace std;

class My_string
{
private:
    char *cstr;
    int len;
public:
    //无参构造
    My_string():cstr(NULL),len(0){}
    
    //有参构造
    My_string(const char* str)
    {
        len = strlen(str);
        cstr = new char[len+1]();
        strcpy(cstr,str);
    }
    
    //(深拷贝)拷贝构造
    My_string(const My_string &other)
    {
        len = other.len;
        cstr = new char[len+1]();
        strcpy(cstr,other.cstr);
        
    }
    
    //(深拷贝)拷贝赋值(=)
    My_string& operator=(const My_string &other)
    {
        if(this != &other)
        {
            len = other.len;
            cstr = new char[len+1]();
            strcpy(cstr,other.cstr);
        }
        
        return *this;  //返回自身引用
    }
    
    //运算符重载(+)
    My_string operator+(const My_string &r)const
    {
        My_string temp;
        temp.len = this->len+r.len;
        temp.cstr = new char[temp.len+1]();
        strcat(temp.cstr,this->cstr);
        strcat(temp.cstr,r.cstr);
        
        //返回新的类对象
        return temp;
    }
    
    //运算符重载(==)
    bool operator==(const My_string &r)const
    {
        return !strcmp(this->cstr,r.cstr);
    }
    
    //运算符重载(!=)
    bool operator!=(const My_string &r)const
    {
        return strcmp(this->cstr,r.cstr);
    }
    
    //运算符重载(>)
    bool operator>(const My_string &r)const
    {
        return strcmp(this->cstr,r.cstr)>0?1:0;
        
    }
    
    //运算符重载(>=)
    bool operator>=(const My_string &r)const
    {
        return strcmp(this->cstr,r.cstr)>=0?1:0;
    }
    
    //运算符重载(<)
    bool operator<(const My_string &r)const
    {
        return strcmp(this->cstr,r.cstr)<0?1:0;
        
    }
    
    //运算符重载(<=)
    bool operator<=(const My_string &r)const
    {
        return strcmp(this->cstr,r.cstr)<=0?1:0;
    }
    
    //析构函数
    ~My_string()
    {
        if(cstr!=NULL)
        {
            delete []cstr;
        }
        cout<<"析构函数"<< " 析构地址为:"<< this <<endl;
    }
    
    //判断是否为空
    bool empty()
    {
        return len==0?1:0;
    }
    
    //返回字符串的长度
    int size()
    {
        return len;
    }
    
    //定位函数
    char &at(int index)
    {
        static char ch = -1;
        if(index<0 || index >= len)
        {
            cout<<"定位错误!"<<endl;
            return ch;
        }
        return *(cstr+index);
        
    }
    
    //转化为C风格字符串
    char* c_str()
    {
        return cstr;
    }
    
    void show()
    {
        cout<<"cstr = "<<cstr<<endl;
        cout<<"len = "<<len<<endl;
        cout<< "类对象地址为:"<< this <<endl<<endl;
    }
    
    friend ostream &operator<<(ostream &L, const My_string &R);
    
    friend istream &operator>>(istream &L,My_string &R);
};

//运算符重载(<<)
ostream &operator<<(ostream &L, const My_string &R)
{
    L<<R.cstr;
    return L;
}

//运算符重载(>>)
istream &operator>>(istream &L,My_string &R)
{
    R.cstr = new char[256];
    L>>R.cstr;
    R.len = strlen(R.cstr);
    return L;
}

int main()
{
    My_string m1 = "How";
    m1.show();
    
    My_string m2(" old");
    m2.show();
    
    My_string m3("qqqq");
    m3 = m1+m2;
    m3.show();
    
    My_string m4 = " are you!";
    m4.show();
    
    My_string m5;
    m5 = m1+m4;
    m5.show();
    
    if(m1>m2)
    {
        cout<<"真"<<endl;
    }
    else
    {
        cout<<"假"<<endl;
    }
    
    cout<<m5<<endl;
    
    My_string m6;
    cin>>m6;
    m6.show();
    
    
    return 0;
}

运行示例 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值