day4_C++

day4_C++

思维导图

重载

复制代码
#include <iostream>
#include <cstring>
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<<"析构完成"<<endl;
    }

    //拷贝赋值
    MyString& operator=(const MyString& other)
    {
        /*判断参数是否是自身*/
        if(this!=&other)
        {
            this->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<<"空,无内容"<<endl;
        else
            cout<<"非空,有内容"<<endl;
    }

    //size函数 无参数
    int ssize() const
    {
        return strlen(this->str);
    }

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

    //at函数
    char my_at(int mark)
    {
        if(mark>size || mark<0)
        {
            cout<<"越界"<<endl;
            return NULL;
        }
        return this->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<<str<<endl;
    }
};


int main()
{
    MyString s1("lsm1314");
    s1.out();
    s1.is_empty();
    cout<<s1.ssize()<<endl;
    cout<<"c_str = "<<s1.c_str()<<endl;
    cout<<"at = "<<s1.my_at(4)<<endl;

    MyString s2 = s1;
    s2.out();

    MyString s3;
    s3=s1;
    s3.out();

    MyString s4;
    s4.is_empty();
    s4.out();

    MyString s5("hhh");
    MyString s6 = s1+s5;
    s6.out();

    MyString s7("eee");
    s6+=s7;
    s6.out();

    MyString s8("zzz");
    if(s8>s5)
    {
        cout<<"s8>s5"<<endl;
    }

    cout<<s1[6]<<endl;


    return 0;
}
相关推荐
橘颂TA8 分钟前
【测试】高效浏览器操作:基础功能与优化设置大全
c++·功能测试·职场和发展·测试·web测试
一只小小的芙厨11 分钟前
寒假集训笔记·以点为对象的树形DP
c++·算法
艾莉丝努力练剑31 分钟前
hixl vs NCCL:昇腾生态通信库的独特优势分析
运维·c++·人工智能·cann
执风挽^32 分钟前
Python基础编程题2
开发语言·python·算法·visual studio code
我在人间贩卖青春36 分钟前
C++之new和delete
c++·delete·new
Z9fish42 分钟前
sse哈工大C语言编程练习20
c语言·开发语言·算法
Trouvaille ~1 小时前
TCP Socket编程实战(三):线程池优化与TCP编程最佳实践
linux·运维·服务器·网络·c++·网络协议·tcp/ip
June`1 小时前
高并发网络框架:Reactor模式深度解析
linux·服务器·c++
小镇敲码人1 小时前
剖析CANN框架中Samples仓库:从示例到实战的AI开发指南
c++·人工智能·python·华为·acl·cann
萧鼎1 小时前
Python 包管理的“超音速”革命:全面上手 uv 工具链
开发语言·python·uv