• day4_C++


    思维导图

    请添加图片描述

    重载

    #include 
    #include 
    using namespace std;
    
    
    class MyString
    {
    private:
        char *str;
        int size;
    public:
        MyString():size(10)
        {
            str = new char[size];
            strcpy(str,"");
        }
    
        MyString(const char *s)
        {
            size = strlen(s);
            str = new char (size+1);
            strcpy(str,s);
        }
    
        //拷贝构造函数
        MyString(const MyString & other):size(other.size)
        {
            this->str = new char [size];
            strcpy(this->str,other.str);
        }
    
        //析构函数
        ~MyString()
        {
            delete this->str;
            cout<<"析构完成"<size=other.size;
            }
            /*释放本身指针成员原来的指向*/
            if(this->str!=NULL)
            {
                delete this->str;
            }
            /* 开辟空间 并重新指向新的堆区空间*/
            this->str = new char [size];
            strcpy(this->str,other.str);
    
            return *this;
        }
    
        //判空函数 无参数
        void is_empty()
        {
            int len = strlen(this->str);
            if(len == 0)
                cout<<"空,无内容"<str);
        }
    
        //c_str函数
        char *c_str()
        {
            return this->str;
        }
    
        //at函数
        char my_at(int mark)
        {
            if(mark>size || mark<0)
            {
                cout<<"越界"<str[mark];
        }
    
        //加号运算符重载
        const MyString operator+(const MyString & R) const
        {
            int len = this->ssize()+R.ssize()+1;
            MyString tempstr ;
            if(tempstr.str!=nullptr){
                delete tempstr.str;
                tempstr.size = 0;
            }
            if(NULL == R.str)
            {
                tempstr.str = new char [this->ssize()];
                strcpy(tempstr.str,this->str);
                tempstr.size = this->size;
                return tempstr ;
            }
            tempstr.str = new char [len];
            tempstr.size = len-1;
            strcpy(tempstr.str,this->str);
            strcat(tempstr.str,R.str);
            return tempstr;
        }
    
        //加等于运算符重载
        MyString operator+=(const MyString & R)
        {
            int len = this->ssize()+R.ssize()+1;
            if(len == 0)
                return *this;
            this->size = len-1;
            char * pstr = new char[len];
            strcpy(pstr,this->str);
            strcat(pstr,R.str);
            delete this->str;
            this->str = pstr;
            return *this;
        }
    
        //关系运算符重载
        bool operator>(const MyString & R) const
        {
            if(strcmp(this->str,R.str)>0)
                return true;
        }
    
        //中括号运算符重载
        char operator[](const int& mark) const
        {
            char s = this->str[mark];
            return s;
        }
    
    
        void out()
        {
            cout<s5)
        {
            cout<<"s8>s5"<
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
  • 相关阅读:
    SQL Server 2019企业版和标准版的区别?
    【Linux operation 44】Linux(red hat)查看看内核版本方法
    MATLAB——RBF、GRNN和PNN神经网络案例参考程序
    IOS常见的bug
    Linux——Xshell、Xftp实现Linux远程登录与应用
    微擎模块 志汇超级外卖餐饮小程序5.7.5开源版 后台模块+前端小程序源码
    Android抓包工具——Fiddler
    安全文件传输如何进行管控,从而促进业务的有序发展?
    高并发系统设计架构方案
    【Matterport3D模拟器安装详细教程】适用于离散视觉语言导航任务的环境部署与安装
  • 原文地址:https://blog.csdn.net/weixin_45790676/article/details/132819765