实现mystring类

 

 

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <cstring>
using namespace std;

class Mstring
{
    public:
        Mstring()
        {
            str=nullptr;
            len=0;
            cout<<"无参构造函数"<<endl;
        }
        Mstring(const char* const s)
        {
            len=strlen(s);
            str=new char[len+1];
            strcpy(str,s);
            cout<<"有参构造函数"<<endl;
        }
        ~Mstring()
        {
            if(str != nullptr)
            {
                delete []str;
                str=nullptr;
            }
            cout<<this<<endl;
            cout<<"析构函数"<<endl;
        }
    
        Mstring(const Mstring &other)
        {
            len=other.len;
            str=new char[len+1];
            strcpy(str,other.str);
            cout<<"深拷贝构造函数"<<endl;
        }

        //赋值拷贝函数
        Mstring& operator=(const Mstring &other)  //传引用&
        {
            if(&other != this)//如果自己赋值给自己(判断不等于自己,进行赋值)
            {
                len=other.len;
                if(nullptr != str) //防止泄露,要判断是否有这个空间
                {
                    delete []str;
                    str=nullptr;
                }
                str=new char[len+1];
                strcpy(str,other.str);
            }
            cout<<"赋值拷贝函数"<<endl;
            return *this;
        }

        //data()
        char* data()
        {
            return str;
        }

        //size()
        int size()
        {
            return len;
        }
        //empty()
        bool empty()
        {
            return 0==len;
        }

        //at()
        char& at(int index)
        {
            if(index<0 || index>=len)
            {
                cout<<index<<endl;
                return str[0];
            }
            return str[index];
        }

        //字符串拼接
        const Mstring operator+(const Mstring& R)const
        {
            Mstring s;
            s.len=this->len+R.len;
            std::strcpy(str,R.str);
            std::strcat(str,R.str);
            return s;
        }
        
         const bool operator==(const Mstring& R)const  //==
        {
            return std::strcmp(str,R.str)==0;
        }

         const bool operator!=(const Mstring& R)const  //!=
        {
            return std::strcmp(str,R.str)!=0;
        }
         
         const bool operator<(const Mstring& R)const  //<
        {
            return std::strcmp(str,R.str)<0;
        }

         const bool operator<=(const Mstring& R)const  //<=
        {
            return std::strcmp(str,R.str)<=0;
        }
         const bool operator>(const Mstring& R)const  //>
        {
            return std::strcmp(str,R.str)>0;
        }
         const bool operator>=(const Mstring& R)const  //>=
        {
            return std::strcmp(str,R.str)>=0;
        }

        friend ostream& operator<<(ostream& L,const Mstring& R);
        friend istream& operator>>(istream& L,Mstring& R);

        private:
        int len;
        char* str;

};

//重载<<
ostream& operator<<(ostream& L,const Mstring& R)
{
    L<<" str="<<R.str<<endl;
    return L;
}


//重载>>
istream& operator>>(istream& L,Mstring& R)
{
    cout<<"please input>>>"<<endl;
    char s[200];
    L>>s;
    R.len=strlen(s);
    if(nullptr != R.str)
    {
        delete[] R.str;
        R.str=nullptr;
    }
    R.str=new char[R.len+1];
    strcpy(R.str,s);
    return L;
}


int main(int argc, const char *argv[])
{
    Mstring s;
    cin>>s;
    cout<<s;
    Mstring s1("hello");
    Mstring s2="world";
    Mstring s3=s1+s2;
    cout<<s1<<s2<<s3;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值