c++/qt作业

文章详细介绍了如何实现一个名为Mystring的C++类,该类模拟了C++标准库中的std::string。类中包含了构造函数(无参和有参)、拷贝构造函数、拷贝赋值运算符、析构函数以及一些基本的成员函数,如empty、size、c_str、at和自定义的比较运算符。此外,还定义了友元函数以支持与iostream的输入输出操作。
摘要由CSDN通过智能技术生成

实现Mystring类

#include <iostream>
#include <cstring>

using namespace std;

class Mystring
{
private:
    int len;
    char *str;       //记录c风格的字符串
public:
    //无参构造
    Mystring() :len(10)
    {
        str = new  char[len];
        strcpy(str, "");

    }
    //有参构造
    Mystring(const char *s)
    {
        len = strlen(s);
        str = new char[len+1];
        strcpy(str,s);

    }
    //定义拷贝构造函数
    Mystring(const Mystring &other)            //深拷贝
    {
        len = other.len;
        str = new char[len+1];
        strcpy(str,other.str);

    }
    //定义拷贝赋值函数
    Mystring & operator=(const Mystring &other)
    {
        if(this != &other)
        {
            this->len = other.len;
            strcpy(this->str,other.str);
        }
        return *this;
    }
    //定义析构函数
    ~Mystring()
    {
        delete[] str;

    }
    //定义判空函数
    bool empty()
    {
        return  strlen(str) == 0;
    }
    //定义size函数
    int size()
    {
        return strlen(str);
    }
    //定义c_str函数
    char *c_str()
    {
        return str;
    }
    //定义at函数
    char &at(int pos)
    {
        if(pos < 0 || pos > len)
        {
            cout<<"越界"<<endl;

        }
        return str[pos];
    }

    //operator[]
    char &operator[](int pos)
    {
        return str[pos];
    }

    //operator+
    const Mystring operator+(const Mystring &R)const
    {
        //定义临时变量
        Mystring temp;
        temp.len = this->len + R.len;
        temp.str = new char[temp.len + 1];
        strcpy(temp.str,this->str);
        strcat(temp.str,R.str);
        return temp;
    }

    //operator==
    bool operator==(const Mystring &R)const
    {
        return  strcmp(this->str,R.str) == 0;

    }
    //operator!=
    bool operator!=(const Mystring &R)const
    {
        return strcmp(this->str,R.str);
    }
    //operator<
    bool operator<(const Mystring &R)const
    {
        return strcmp(this->str,R.str) < 0;
    }
    //operator>
    bool operator>(const Mystring &R)const
    {
        return strcmp(this->str,R.str) > 0;
    }
    //operator<=
    bool operator<=(const Mystring &R)const
    {
        return strcmp(this->str,R.str) <= 0;
    }
    //operator>=
    bool operator>=(const Mystring &R)const
    {
        return strcmp(this->str,R.str) >= 0;
    }

    friend ostream &operator<<(ostream &L, const Mystring &R);
    friend istream &operator>>(istream &L, const Mystring &R);
};
//全局函数实现字符串的输入输出
//operator<<
ostream &operator<<(ostream &L, const Mystring &R)
{
    L<<R.str<<endl;
    return L;
}
//operator>>
istream &operator>>(istream &L, const Mystring &R)
{
   L>>R.str;
   return L;
}

int main()
{
    Mystring s1;
    Mystring s2("hello");
    Mystring s3("world");
    s2[0] = 'H';

    Mystring s4;
    s4 = s2 + s3;
    cout<<s4<<endl;
    Mystring s5;
    cin>>s5;
    cout<<s5<<endl;
    cout <<s5.size() << endl;
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值