【在myString上进行运算符的重载】

//仿照string完成mystring类

#include <iostream>
#include <cstring>
using namespace std;

class myString
{
private:
    char *str;//记录c风格的字符串
    int size;//记录字符串的实际长度
public:
    //无参构造
    myString():size(10)//定义字符串大小
    {
        str = new char[size];//申请一个字符串的空间
        cout<<"无参函数"<<endl;
    }
    //有参构造
    myString(const char *s)//传字符串参数
    {
        size = strlen(s);
        str = new char[size+1];//+1是字符串后带有\0
        strcpy(str,s);
        cout<<"有参函数"<<endl;
    }
    //定义展示函数
    void show()
    {
        cout<<"size= "<<size<<endl;//大小
        cout<<"str= "<<str<<endl;//内容
    }
    //拷贝构造
    myString(const myString & other)
    {
        size = other.size;
        str = new char[size+1];
        strcpy(str,other.str);

        cout<<"拷贝构造函数"<<endl;
    }
    //赋值构造
    myString & operator=(const myString &other)
    {

        if(this != &other)//排除自己给自己赋值的情况
        {
           strcpy(this->str,other.str);
            this->size = other.size;
        }
            cout<<"赋值构造"<<endl;
             return *this;

    }
    //析构函数
    ~myString()
    {
        cout<<"析构函数"<<endl;
        delete []str;//对指针空间进行释放
    }
    //判空函数
    bool empty()
    {
        if(size==0)
        {
            return  true;
        }
        else
            return false;
    }

    //size函数
    int len()
    {
        return  size;
    }
    //c_str函数
    char *c_str()
    {
        return str;
    }

    //at函数
    char &at(int buf)
    {
        if(buf < 0 || buf>= size)
        {
            cout<<"输入错误"<<endl;
        }
        else
        return  str[buf];
    }



    //重构+
    const myString operator+(const myString &R)const
    {
        myString temp;
        temp.size = this->size+R.size;
        temp.str = strcat(this->str,R.str);

        return  temp;
    }
    //成员函数版实现关系运算符 >
      bool operator>(const myString &R)const
    {
        if(this->size>R.size && this->str > R.str)
        {
            return 1;
        }
        return 0;
    }
      //[]运算符
      
};




int main()
{
//    myString s1;//无参构造
    myString s2("abcde");//有参函数
//    s2.show();

 //  myString s3(s2);//拷贝构造函数
 //   s3.show();

    myString s4("hqyjsh");//有参函数
    //s2.show();

 //   myString s5=s2; //调用拷贝构造
 //   s5=s4; //调用赋值构造
 //   cout<<"s2.empty()="<<s2.empty()<<endl;
 //   s5.show();

 //   cout<<"s5.len="<< s5.len()<<endl;
 //   cout<<"s5.at(3)="<< s5.at(3) <<endl;
    
    //+
    myString s6;
    s6=s2+s4;
    s6.show();

    if(s2>s6)
    {
        cout<<"yes"<<endl;
    }
    else
    {
        cout<<"no"<<endl;
    }

    
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,可以参考下面的示例代码: ```c++ #include <iostream> #include <cstring> using namespace std; class mystring { private: char* str; public: mystring() { str = NULL; } mystring(const char* s) { int len = strlen(s); str = new char[len + 1]; strcpy(str, s); } mystring(const mystring& other) { int len = strlen(other.str); str = new char[len + 1]; strcpy(str, other.str); } ~mystring() { if (str != NULL) { delete[] str; str = NULL; } } mystring& operator=(const mystring& other) { if (this != &other) { if (str != NULL) { delete[] str; str = NULL; } int len = strlen(other.str); str = new char[len + 1]; strcpy(str, other.str); } return *this; } friend ostream& operator<<(ostream& os, const mystring& s) { os << s.str; return os; } }; int main() { mystring s1("Hello"); mystring s2 = "World"; mystring s3(s1); cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; cout << "s3: " << s3 << endl; s2 = s1; cout << "s2: " << s2 << endl; return 0; } ``` 在上面的代码中,我们自定义了一个字符串 `mystring`,并实现运算符 `=` 的重载。在 `operator=` 函数中,我们首先判断是否为自赋值,如果不是,就先释放原有的内存空间,再申请新的内存空间,并将字符串拷贝到新的内存中。 在 `main` 函数中,我们创建了三个 `mystring` 型的对象 `s1`、`s2` 和 `s3`,并通过重载的 `<<` 运算符输出它们的值。然后将 `s1` 赋值给 `s2`,并再次输出 `s2` 的值,可以看到 `s2` 的值已经变成了 `Hello`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值