2022.8.24 作业

本文介绍了如何使用C++自定义类mystring,实现无参构造、析构、有参构造、拷贝构造和拷贝赋值等方法,以及重载+、+=、比较运算符,以及成员访问和取值运算符。通过实例展示了字符串操作和比较的实用技巧。
摘要由CSDN通过智能技术生成

#include <iostream>

using namespace std;

class mystring {
public:
    //无参构造函数
    mystring()
    {
        len=0;
        str=nullptr;
    }

    //析构函数
    ~mystring()
    {
        if(str!=nullptr){
            delete []str;
        }
    }

    //有参构造函数
    mystring(const char *_str)
    {
        len=mystrlen(_str);
        str=new char[len+1];
        mystrcpy(str,_str);
    }

    //拷贝构造函数
    mystring(const mystring &operation)
    {
        len=operation.len;
        str=new char[len+1];
        mystrcpy(str,operation.str);
    }

    //拷贝复制函数
    mystring &operator=(const mystring &operation)
    {
        if(this!=&operation){
            if(str!=nullptr){
                delete [](this->str);
            }
            len=operation.len;
            str=new char[len+1];
            mystrcpy(str,operation.str);
        }
        return *this;
    }

    //+运算符重载
    const mystring &operator+(const mystring &operation)
    {
        char *p=new char[len+operation.len+1];
        mystrcpy(p,str);
        mystrcat(p,operation.str);
        delete []str;
        str=p;
        len=mystrlen(str);
        return *this;
    }

    //+=运算符重载
    const mystring &operator+=(const mystring &operation)
    {
        char *p=new char[len+operation.len+1];
        mystrcpy(p,str);
        mystrcat(p,operation.str);
        delete []str;
        str=p;
        len=mystrlen(str);
        return *this;
    }

    //>运算符重载
    bool operator>(const mystring &operation)
    {
        if(mystrcmp(this->str,operation.str)>0)
            return true;
        else
            return false;
    }

    //>=运算符重载
    bool operator>=(const mystring &operation)
    {
        if(mystrcmp(this->str,operation.str)>=0)
            return true;
        else
            return false;
    }

    //<运算符重载
    bool operator<(const mystring &operation)
    {
        if(mystrcmp(this->str,operation.str)<0)
            return true;
        else
            return false;
    }

    //<=运算符重载
    bool operator<=(const mystring &operation)
    {
        if(mystrcmp(this->str,operation.str)<=0)
            return true;
        else
            return false;
    }

    //==运算符重载
    bool operator==(const mystring &operation)
    {
        if(mystrcmp(this->str,operation.str)==0)
            return true;
        else
            return false;
    }

    //!=运算符重载
    bool operator!=(const mystring &operation)
    {
        if(mystrcmp(this->str,operation.str)!=0)
            return true;
        else
            return false;
    }

    //[]去成员运算符重载
    char operator[](const int index) const
    {
        return (this->str)[index];
    }

    //<<取值运算符重载
    friend ostream &operator<<(ostream &,mystring &);

    bool myEmpty()
    {
        return str==nullptr?true:false;
    }

    int mySize()
    {
        return len;
    }

    const char *myStr()
    {
        return (const char *)str;
    }
private:
    char *str;
    int len;

    char *mystrcpy(char *dest,const char *src)
    {
        char *ch=dest;
        while(*src!='\0'){
            *(ch++)=*(src++);
        }
        *ch='\0';
        return dest;
    }

    int mystrlen(const char *src)
    {
        const char *ch=src;
        while(*ch!='\0'){
            ch++;
        }
        return ch-src;
    }

    char *mystrcat(char *dest,const char *src)
    {
        char *ch=dest;
        while(*(++ch)!='\0');
        while(*src!='\0'){
            *(ch++)=*(src++);
        }
        *ch='\0';
        return dest;
    }

    int mystrcmp(const char *dest,const char *src)
    {
        while(*dest!='\0' && *src!='\0' && *dest==*src){
            dest++;
            src++;
        }
        return *dest-*src;
    }
};

ostream &operator<<(ostream &output,mystring &operation)
{
    output<<operation.str;
    return output;
}

int main()
{
    mystring s1("hello"),s2(" world"),s3;
    s3=s1+s2;
    s1+=s2;
    cout<<s1<<endl;
    cout<<s3<<endl;

    cout<<(s1>s3?"s1>s3":"s1<=s3")<<endl;
    cout<<(s1==s3?"s1==s3":"s1!=s3")<<endl;
    cout<<(s1<=s3?"s1<=s3":"s1>s3")<<endl;

    cout<<s2[3]<<endl;

    cout<<s2<<endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值