模仿string类,编写自己的My_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((char*)str),len(strlen(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()
    {
        if(len == 0)
        {
            delete []cstr;
            cstr = NULL;
        }
    //    cout<<"析构函数"<<this<<endl;           //打印this对应的类对象地址,检验类对象是否都正常结束
    }

    //判断是否为空
    bool empty()
    {
        return len==0?1:0;
    }

    //返回字符串的长度
    int size()
    {
        return len;
    }

    //定位函数
    char &at(int index)
    {
        return cstr[index];
    }

    //转化为C风格字符串
    char* c_str()
    {
        return cstr;
    }

    //运算符重载
    //operator+ 连接两个字符串
    My_string &operator+(const My_string &R)
    {
        if(cstr != NULL)
        {
            My_string temp = *this;
            delete [] cstr;
            cstr = new char[len+R.len+1];
            strcpy(cstr, temp.cstr);
            strcat(cstr, R.cstr);

        }
        else
        {
            cstr = new char[R.len+1];
            strcpy(cstr, R.cstr);
        }
        len +=R.len;

        return *this;
    }

    //operator= 将另一个字符串赋值给本字符串
    My_string &operator=(const My_string &R)
    {
        if(cstr != NULL)
            delete [] cstr;
        cstr = new char[R.len+1];
        strcpy(cstr, R.cstr);

        return *this;
    }
    My_string &operator=(const char* s)
    {
        if(cstr != NULL)
            delete [] cstr;
        cstr = new char[strlen(s)];
        strcpy(cstr, s);

        return *this;
    }

    //operator== 判断两个字符串是否相等
    bool operator==(const My_string &R)const
    {
        return !strcmp(cstr, R.cstr);
    }
    bool operator==(const char* s)const
    {
        return !strcmp(cstr, s);
    }
    bool operator ==(const string &s)const
    {
        return !strcmp(cstr, s.c_str());
    }


    //operator!= 判断两个字符串是否不相等
    bool operator!=(const My_string &R)const
    {
        return strcmp(cstr, R.cstr);
    }

    //operator> 判断字符串1是否大于字符串2
    bool operator>(const My_string &R)const
    {
        return strcmp(cstr, R.cstr) >0 ? 1:0;
    }

    //operator< 判断字符串1是否小于字符串2
    bool operator<(const My_string &R)const
    {
        return strcmp(cstr, R.cstr) <0 ? 1:0;
    }

    //operator>= 判断字符串1是否大于或等于字符串2
    bool operator>=(const My_string &R)const
    {
        return strcmp(cstr, R.cstr) >=0 ? 1:0;
    }

    //operator<= 判断字符串1是否小于或等于字符串2
    bool operator<=(const My_string &R)const
    {
        return strcmp(cstr, R.cstr) <=0 ? 1:0;
    }

    //operator[] 返回对应下标的字符
    char &operator[](int i)
    {
        if(i<0 || i>=len)
        {
            static char c = ' ';
            cout<<"数组下标越界"<<endl;
            return c;
        }
        return cstr[i];
    }

    //友元函数
    friend ostream &operator<<(ostream &o,const My_string &s);

    //友元函数
    friend istream &operator>>(istream &i,My_string &s);
};

//重载提取和插入运算符
//operator<< 输出字符串的内容
ostream &operator<<(ostream &o,const My_string &s)
{
    if(s.cstr == NULL)
        o<<"字符串为空";
    else
        o<<s.cstr;
    return o;
}

//operator>> 从终端输入到字符串
istream &operator>>(istream &i,My_string &s)
{
    if(s.cstr != NULL)
        delete [] s.cstr;
    s.cstr = new char[280];
    i>>s.cstr;
    s.len = strlen(s.cstr);
    return i;
}

int main()
{
    My_string s1("hello");      //调用有参构造函数,将字符串赋值给s1
    My_string s2 = s1;          //调用拷贝构造函数,将s1作为参数传给s2
    My_string s3(s1);           //调用拷贝构造函数,将s1作为参数传给s3
    My_string s4;               //系统自动调用无参构造,此时s4的len=0,cstr=NULL

    //打印各个类的地址
    cout<<"&s1 = "<<&s1<<"   &s2 = "<<&s2<<"   &s3s = "<<&s3<<"   &s4 = "<<&s4<<endl;
    cout<<"------------------------------------------"<<endl;

    //分别打印s1,s2,s3的字符串
    cout<<"s1 = "<<s1.c_str()<<endl;
    cout<<"s2 = "<<s2.c_str()<<endl;
    cout<<"s3 = "<<s3.c_str()<<endl;
    cout<<"------------------------------------------"<<endl;

    //调用定位函数、获取字符串长度以及判断s3是否为空
    cout<<"s3[4] = "<<s3.at(4)<<"   s3.size = "<<s3.size()<<"   s3.empty = "<<s3.empty()<<endl;

    //判断s4是否为空
    cout<<"s4.empty = "<<s4.empty()<<endl;

    //打印输出看,是否真的转为C风格字符串
    char* str = s3.c_str();
    cout<<"char* str = "<<str<<endl;
    cout<<"------------------------------------------"<<endl;

    //operator+ 连接两个字符串
    My_string r1("world");
    cout<<"s1 + r1 = "<<(s1+r1).c_str()<<endl;

    //operator= 将另一个字符串赋值给本字符串
    s2 = s1;
    cout<<"s2 = "<<s2.c_str()<<endl;
    cout<<"------------------------------------------"<<endl;

    //operator!= 判断两个字符串是否相等
    cout<<"s2 == s1 ? "<<boolalpha<<(s2==s1)<<endl;
    cout<<"s2 == s3 ? "<<boolalpha<<(s2==s3)<<endl;
    cout<<"s2 == helloworld  ?  "<<boolalpha<<(s2=="helloworld")<<endl;
    cout<<"------------------------------------------"<<endl;

    //operator!= 判断两个字符串是否不相等
    cout<<"s2 != s1 ? "<<boolalpha<<(s2!=s1)<<endl;
    cout<<"s2 != s3 ? "<<boolalpha<<(s2!=s3)<<endl;
    cout<<"------------------------------------------"<<endl;

    //operator 判断字符串1与字符串2的大小关系
    cout<<"s3 >  s1 ? "<<boolalpha<<(s3>s1)<<endl;
    cout<<"s3 <  s1 ? "<<boolalpha<<(s3<s1)<<endl;
    cout<<"s3 >= s1 ? "<<boolalpha<<(s3>=s1)<<endl;
    cout<<"s3 <= s1 ? "<<boolalpha<<(s3<=s1)<<endl;
    cout<<"------------------------------------------"<<endl;

    //operator[] 返回对应下标的字符
    cout<<"s3[4] = "<<s3[4]<<endl;
    cout<<"s3[4]+1 = "<<s3[4]+1<<endl;
    cout<<"s3[5] = "<<s3[5]<<endl;

    //重载提取和插入运算符
    //operator<< 输出字符串的内容
    My_string o;
    cout<<"s1= "<<o<<endl;
    cout<<"s1= "<<s1<<endl;

    //operator>> 从终端输入到字符串
    My_string in;
    cout<<"请输入字符串"<<endl;
    cin>>in;
    cout<<"in = "<<in<<endl;


    return 0;
}

测试结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值