# 2023/3/329 运算符重载

2023/3/329 运算符重载

使用C++封装String,并为其重载运算符

#include <iostream>
#include <cstring>
#include <exception>
using namespace std;

class MyExcption : public std ::exception
{

private:
    std::string message;

public:
    MyExcption(const char* msg): message(msg) {}

    virtual const char* what() const throw()
    {
        return message.c_str();
    }
};

class mystring
{
private:
    char *str;//记录C风格字符串
    int s_size;//记录字符串的实际长度
public:

    //无参构造
    mystring():s_size(0)
    {
        str = new char[s_size+1];
        *str = '\0';
        //cout << "无参构造" << endl;
    }


    //有参构造
    mystring(const char *s)
    {
        if(nullptr == s)
        {
            s_size = 0;
            str = new char[s_size+1];
            *str = '\0';
        }else
        {
            s_size = strlen(s);
            str = new char[s_size+1];
            strcpy(str,s);
        }

        //cout << "有参构造" << endl;
    }

    //析构函数
    ~mystring()
    {
        delete []str;
        //cout << "析构函数" << endl;
    }

    //判空函数
    bool is_empty()
    {
        if(0 == s_size)
        {
            return true;
        }else
        {
            return false;
        }
    }

    //size函数
    int s_len()
    {
        return s_size;
    }

    //c_str函数
    char *c_str()
    {
        return str;
    }

    //at函数
    char &at(int i)
    {

        char *c = str;
        try {
            if(i<0 && i>s_size)
            {
                throw MyExcption("下标越界");
            }
        }catch (const std::exception& e) {
            std::cerr << "Error:" << e.what() <<endl;

        }
        return *(c+i);

    }
    //拷贝构造
    mystring(const mystring & other)
    {
        s_size = other.s_size;
        str = new char[s_size+1];
        strcpy(str,other.str);

        //cout << "拷贝构造" << endl;
    }

    //拷贝赋值
    mystring & operator = (const mystring & other)
    {
        //排除给自己赋值的情况
        if(this != &other)
        {
            delete []str;
            this->s_size = other.s_size;
            this->str = new char[s_size+1];
            strcpy(this->str,other.str);

        }
        //cout << "拷贝赋值" << endl;

        return *this;

    }

    //成员函数实现[]运算符重构
    char &operator[](const int &i)const
    {
        return this->str[i];
    }

    //成员函数实现+运算符重构
    const mystring operator+(const mystring &other)const
    {
        mystring temp;
        temp.s_size = this->s_size + other.s_size;
        temp.str = new char[s_size+1];
        *temp.str = '\0';
        strcat(temp.str,this->str);
        strcat(temp.str,other.str);
        return temp;
    }

    //成员函数实现==运算符重构
    bool operator==(const mystring &other)const
    {
        if(strcmp(this->str,other.str) == 0)
        {
            return true;
        }else
        {
            return false;
        }
    }

    //成员函数实现!=运算符重构
    bool operator!=(const mystring &other)const
    {
        if(strcmp(this->str,other.str) != 0)
        {
            return true;
        }else
        {
            return false;
        }
    }

    //成员函数实现<运算符重构
    bool operator<(const mystring &other)const
    {
        if(strcmp(this->str,other.str) < 0)
        {
            return true;
        }else
        {
            return false;
        }
    }

    //成员函数实现>运算符重构
    bool operator>(const mystring &other)const
    {
        if(strcmp(this->str,other.str) > 0)
        {
            return true;
        }else
        {
            return false;
        }
    }

    //成员函数实现<=运算符重构
    bool operator<=(const mystring &other)const
    {
        if(strcmp(this->str,other.str) <= 0)
        {
            return true;
        }else
        {
            return false;
        }
    }

    //成员函数实现>=运算符重构
    bool operator>=(const mystring &other)const
    {
        if(strcmp(this->str,other.str) >= 0)
        {
            return true;
        }else
        {
            return false;
        }
    }

    //成员函数实现<=>运算符重构


//    //成员函数实现""运算符重构
//    mystring operator ""sv(const mystring &other)
//    {

//    }

    friend istream &operator>>(istream &L,mystring &other);

    friend ostream &operator<<(ostream &L,mystring &other);


};

//全局函数实现>>运算符重构
istream &operator>>(istream &L,mystring &other)
{
    char s[other.s_len()];
    cin >> s;
    strcpy(other.c_str(),s);

    return L;
}

//全局函数实现<<运算符重构
ostream &operator<<(ostream &L,mystring &other)
{
    L << other.c_str()<< endl;

    return L;
}

int main()
{
    mystring s1;
    cout << "s1 = " << s1.c_str() << endl;

    mystring s2("world");
    cout << "s2 = " << s2.c_str() << endl;

    cout << s1.is_empty() << endl;
    cout << s2.is_empty() << endl;

    int len = s2.s_len();
    for(int i=0;i<len;i++)
    {
        cout << s2.at(i) << "\t";
    }
    cout << endl;

    cout << " =: ";
    mystring s3;
    s3 = s2;
    cout << "s3 = " << s3.c_str() << endl;

    cout << " (): ";
    mystring s4(s2);
    cout << "s4 = " << s4.c_str() << endl;

    cout << " +: ";
    mystring s5;
    s5 = s2 + s3;
    cout << "s5 = " << s5;

    cout << "s5[1] = " << s5[1] << endl;

    cout << "s2 = " << s2;
    cout << "s3 = " << s3;

    cout << " >: ";
    if(s3 > s2)
    {
        cout << "yes" << endl;
    }else
    {
        cout << "no" << endl;
    }

    cout << " >=: ";
    if(s3 >= s2)
    {
        cout << "yes" << endl;
    }else
    {
        cout << "no" << endl;
    }

    cout << " ==: ";
    if(s2 == s3)
    {
        cout << "yes" << endl;
    }else
    {
        cout << "no" << endl;
    }

    cout << " <: ";
    if(s3 < s2)
    {
        cout << "yes" << endl;
    }else
    {
        cout << "no" << endl;
    }

    cout << " <=: ";
    if(s3 <= s2)
    {
        cout << "yes" << endl;
    }else
    {
        cout << "no" << endl;
    }

    cout << " !=: ";
    if(s3 != s2)
    {
        cout << "yes" << endl;
    }else
    {
        cout << "no" << endl;
    }
    return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值