2023/9/11 qt&c++

cpp 复制代码
#include <iostream>
#include <cstring>
using namespace std;
class myString
{
private:
    char *str;
    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):size(other.size)
    {
        str = new char [size];
        strcpy(this->str,other.str);
        cout<<"拷贝构造函数"<<endl;
    }

    //析构函数
    ~myString()
    {
        delete[] this->str;
        cout<<"析构函数"<<endl;
    }
    //拷贝赋值函数
    myString &operator = (const myString &other)
    {
       size = other.size;
       strcpy(str,other.str);
       cout<<"拷贝赋值函数"<<endl;
       return *this;

     }

    //判空函数
    bool str_empty(const char *str) const
    {
        if(str ==NULL||*str=='\0')
        {

           return true;
        }
        else
            return false;
    }
    //size函数
    int str_size(const char *str)const
    {
        return sizeof(str);
    }
    //c_str函数
    const char *c_str() const
    {
        return str;
    }
    //at函数
    char  &at(int pos)
    {


        return str[pos];

    }
    //成员函数版实现加号运算符重载
    myString operator+(const myString &R)const
    {
        myString new_string = *this;
        delete[] new_string.str;
        int len =strlen(this->str)+strlen(R.str)+1;
        new_string.str = new char[len];
        strcpy(new_string.str,this->str);
        strcat(new_string.str,R.str);
        return new_string;
    }
    //成员函数版实现加等于运算符重载
    myString &operator+=(const myString &R)
    {
       int len = strlen(str)+strlen(R.str)+1;
       char *s =this->str;
       str = nullptr;
       delete [] str;
       str = new char [len];
       strcpy(this->str,s);
       strcat(this->str,R.str);
       return *this;
    }

    //关系运算符重载
    bool operator>(const myString &R)const
    {
        //先求出长度
        int len1 = strlen(this->str);
        int len2 = strlen(R.str);
        int minlen =(len1<len2?len1:len2);
        for(int i=0;i<minlen;i++)
        {
            if(this->str[i]>R.str[i])
            {
                return true;
            }
            else if(this->str[i]<R.str[i])
            {
                return false;
            }
        }
        return  len1>len2;
    }

    //成员函数版实现中括号运算符重载
    char & operator[](int index)
    {
        if(index>=0&&index<size)
        {
            return str[index];
        }
    }
    //展示函数
    void show()
    {
        cout<<"str = "<<str<<"  size = "<<size<<endl;
    }
};
int main()
{
    myString s1("hello");
    s1[0]='H';
    s1.show();
    myString s2("world");
    s2.show();
    myString s6 =s1;
    s6.show();
    myString s3;
    s3 = s1 + s2;
    cout<<s3.c_str()<<endl;
    myString s4("hahaha");
    s4+=s1;
    cout<<s4.c_str()<<endl;
    if(s3>s2)
    {
        cout<<"yes"<<endl;
    }
    else
        cout<<"no"<<endl;
    myString s5("daydayup");
    s5.show();
    return 0;
}
相关推荐
喜欢吃燃面3 小时前
深入 C++ STL:从 unordered_set与unordered_map到底层哈希表的原理与实现
数据结构·c++·散列表
Quz3 小时前
QML Loader:从文件加载组件、加载内联组件
qt
Quz3 小时前
QML Loader: 状态监听与动态标签页
qt
似水এ᭄往昔3 小时前
【Qt】--常用控件(显示类控件)
开发语言·qt
FlightYe3 小时前
音视频修炼之基础理论(五):AAC格式与解析
android·linux·c++·音视频·aac
ShineWinsu4 小时前
对于MySQL:数据库的操作的解析
linux·数据库·c++·mysql·面试·笔试·库的操作
云小逸4 小时前
C++ 第一阶段:对象、内存与生命周期
开发语言·c++
夜航海图4 小时前
S-57 数据解剖:把一个 ENC 文件拆给你看
c++
(Charon)4 小时前
【C++】网络缓冲区设计(三):Chain Buffer链式缓冲区、动态扩容与跨节点读写
服务器·c++
软行6 小时前
LeetCode 每日一题 3876. 构造奇偶一致的数组 II
c++·算法·leetcode