2023/9/11 qt&c++

#include 
#include 
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<<"拷贝构造函数"<str;
        cout<<"析构函数"<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 =(len1str[i]>R.str[i])
            {
                return true;
            }
            else if(this->str[i]len2;
    }

    //成员函数版实现中括号运算符重载
    char & operator[](int index)
    {
        if(index>=0&&indexs2)
    {
        cout<<"yes"<

2023/9/11 qt&c++_第1张图片

你可能感兴趣的:(qt,c++)