• C++ day4


    1、仿照string类,完成myString 类
    
    #include 
    #include 
     
    
    using namespace std;
     
    
    class myString
    {
    private:
        char *str;  //记录c风格的字符串
        int size;   //记录字符串的实际长度
    public:
        //无参构造
        myString():size(10)
        {
            str = new char[size];   //构造出一个长度为10的字符串
            strcpy(str, "");    //赋值为空
        }
        //有参构造
        myString(const char *s)
        {
            size = strlen(s);
            str = new char[size+1];
            strcpy(str, s);
        }
     
    
        //拷贝构造函数
        myString(const myString &other):str(new char(*(other.str))),size(other.size)
        {
            strcpy(this->str,other.str);    //拷贝字符串
            this->size = other.size;    //拷贝字符串长度
            cout<<"myString::拷贝构造函数 str = "<    }
     
    
        //析构函数
        ~myString()
        {
            delete str;
            cout<<"myString::析构函数 this = "< 
    
        }
     
    
     
    
        //拷贝赋值函数
        myString & operator=(const myString &other)
        {
            if(this != &other)
            {
                strcpy(this->str, other.str);
                this->size = other.size;
            }
            cout<<"myString::拷贝赋值函数 str = "<        return *this;
        }
     
    
        //判空函数
        bool empty()
        {
            return 0==size;
        }
     
    
        //size函数
        int myString_size()
        {
            return strlen(str);
        }
     
    
        //c_str函数
        const char *c_str()
        {
            return this->str;
        }
     
    
        //at函数
        char &at(int pos)
        {
            return str[pos-1];
        }
     
    
        //加号运算符重载
       const myString operator+(const myString &str)const
        {
            myString temp;
            temp.size = this->size+str.size;
            temp.str = strcat(temp.str, this->str);
            temp.str = strcat(temp.str, str.str);
            return temp;
        }
     
    
        //加等于运算符重载
        const myString operator+=(const myString &str)
        {
            strcat(this->str, str.str);
            this->size = strlen(this->str);
            return *this;
        }
     
    
     
    
        //关系运算符重载
     
    
        bool operator >(const myString &s)const{
            if(this->size < s.size){
                return false;
            }
            else{
                for(int i=0;isize;i++){
                    if(*(this->str+i)<*(s.str+i)){
                        return false;
                    }
                }
            }
            return true;
    }
     
    
     
    
        //中括号运算符重载
        char & operator[](const int pos)const{
            if(pos>=size || pos<0){
                cout<<"访问越界"< 
    
            }
            return *(str+pos);
        }
    };
     
    
    int main()
    {
        myString s1("hello world");
     
    
        myString s2(s1);
     
    
        myString s3;
        s3 = s2;
        cout << "字符串长度为:" < 
    
     
    
        cout<<"at(1) = "< 
    
     
    
        cout<<"c_str: "< 
    
     
    
     
    
        return 0;
    }

    2、思维导图

  • 相关阅读:
    哪款蓝牙运动耳机性价比最高、性价比最高的蓝牙运动耳机
    【Docker】Linux各系统安装Docker,Docker-compose方式汇总
    智慧屏内核崩溃问题分析
    基于Flume+Kafka+Hbase+Flink+FineBI的实时综合案例(一)案例需求
    超详细Redis入门教程二
    【代码随想录】二刷-链表
    uboot启动学习笔记三之启动镜像文件分析
    音视频 - 视频编码原理
    NetDevOps — YANG 协议 — 模型文件
    【Android】Android apk 逆向编译
  • 原文地址:https://blog.csdn.net/2302_77738263/article/details/132818067