自定义类对String类功能的模仿


#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)
    {
        cstr = new char[strlen(str)+1];
        strcpy(cstr,str);
        len=strlen(str);
    }
    //拷贝构造
    My_string(const  My_string &other)
    {
        cstr = new char[strlen(other.cstr)+1];
        strcpy(cstr,other.cstr);
        this->len=other.len;
    }
    //析构
    ~My_string()
    {
        delete []cstr;
    }
    //判断是否为空
    bool empty()
    {
        return len==0?1:0 ;
    }
    //返回字符串长度
    int size()
    {
        return len;
    }
    //定位函数
    char &at(int index)
    {
        if(index>(len-1))
        {
            cout<<"希望index小于"<<len<<endl;

        }
        else
        {
       return cstr[index];
        }
    }
    //转换为C风格字符串
    char* c_str()
    {
       return cstr;
    }
    //字符串赋值
    My_string &operator =(const My_string &s)
    {
          //判断左操作值是否为空
        if(this->cstr==NULL)
        {
       this->cstr = new char[strlen(s.cstr)+1];
        }
        strcpy(this->cstr,s.cstr);
        return *this;
    }
    //重载 +连接两个字符串  一
    const My_string operator +(const My_string &s)
    {
        static My_string temp;
        temp.cstr = new char[strlen(s.cstr)+strlen(this->cstr)+1];
        strcpy(temp.cstr,this->cstr);
        strcat(temp.cstr,s.cstr);
        temp.len=strlen(s.cstr)+strlen(this->cstr)+1;
        return temp;
    }
    //重载+ 连接一个个字符串和一个字符
    const My_string operator +(const char &c)
    {
        static My_string temp;
        //给temp类对象申请存储字符串的空间
        temp.cstr = new char[strlen(this->cstr)+2];
        strcpy(temp.cstr,this->cstr);
        temp.cstr[strlen(this->cstr)]=c;
        temp.cstr[strlen(this->cstr)+1]=0;
        temp.len=strlen(this->cstr)+2;
        return temp;
    }

    //字符串 == 重载
    bool operator ==(const My_string &s)
    {
        return strcmp(this->cstr,s.cstr)==0?1:0;
    }
    //字符串 != 重载
    bool operator !=(const My_string &s)
    {
        return strcmp(this->cstr,s.cstr);
    }
    // >重载
    bool operator >(const My_string &s)
    {
       return strcmp(this->cstr,s.cstr)>0? 1:0;
    }
    // < 重载
    bool operator <(const My_string &s)
    {
       return strcmp(this->cstr,s.cstr)<0? 1:0;
    }
    //>=重载
    bool operator >=(const My_string &s)
    {
       return strcmp(this->cstr,s.cstr)>0||strcmp(this->cstr,s.cstr)==0 ? 1:0;
    }
    // <=>重载


   friend ostream &operator <<(ostream &L, const My_string &s);
   friend ostream &operator >>(ostream &L, const My_string &s);
};
// << 输出重载
ostream &operator <<(ostream &L, const My_string &s)
{
    L<<s.cstr;
    return L;
}
//>> 输入重载
ostream &operator >>(ostream &L, const My_string &s)
{
    L>>s.cstr;
    return L;
}
int main()
{
    My_string s1("aaaa");
    //My_string s2(s1);
    //cout<<s1.size()<<endl;
    //cout<<s1.at(1)<<endl;
   // strcat(s1.c_str(),"hao");
   //cout<<s1.empty()<<endl;
    cout<<"s1="<<s1<<endl;
    My_string s2("bbbb");
    My_string s3;
    My_string s4;
    //验证字符串连接一个字符
    char c='3';
    s3=s1+c;
    cout<<"s3="<<s3<<endl;
    //验证两个字符串连接
    s3=s3+s2;
    cout<<"s3="<<s3<<endl;
    //验证字符串赋值
    s4=s1;
    cout<<"s4="<<s4<<endl;
    if(s4==s1)
    {
        cout<<"s4和s1相等"<<endl;
    }
    if(s4!=s2)
    {
        cout<<"s4和s1不相等"<<endl;
    }
    if(s1>s2)
    {
       cout<<"s1大于s2"<<endl;
    }
    if(s1<s3)
    {
       cout<<"s1小于s3"<<endl;
    }
    if(s1>=s2)
    {
       cout<<"s1大于或者等于s2"<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值