myString

该篇文章展示了如何在C++中自定义一个名为myString的字符串类,包括无参构造、有参构造、拷贝构造、析构函数以及拷贝赋值操作。此外,还实现了加号运算符的成员和友元重载,以及大于运算符重载,用于比较字符串。代码示例演示了类的使用,如创建对象、拷贝和赋值等。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <cstring>

using namespace std;

class myString
{
private:
    char *str;
    int size;

public:
    myString():size(10)
    {
        str = new char[size];
        strcpy(str, "");
    }

    //有参构造
    myString(const char *s)
    {
        if (s == nullptr)       //如果是空,则初始化空
        {
            size = 0;
            str = new char[size];
            strcpy(str, "");
        }
        else
        {
        size = strlen(s);
        str = new char[size + 1];
        strcpy(str, s);

        }
    }

    //拷贝构造
    myString (const myString &other)
    {
        this->size = other.size;
        this->str = new char[size + 1];
        strcpy(this->str, other.str);
    }

    //析构函数
    ~myString()
    {
        delete [] str;
    }

    //拷贝赋值函数
    myString &operator= (const myString &other)
    {
        if (this != &other)
        {
            delete [] str;      //由于需要重新申请空间,所以要释放之前使用的空间
            this->size = other.size;
            this->str = new char[size + 1];
            strcpy(this->str, other.str);
        }

        return *this;
    }

    //判空函数
    bool empty()
    {
        return size == 0;
    }

    //size函数
    int str_len()
    {
        return size;
    }

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

    //at函数
    char &at(int pos)
    {
        if (pos >= 0 || pos < this->size)
        {
            return str[pos];
        }

    }

    //成员函数实现加号运算符重载:
    const  myString operator+ (const myString &R) const
    {
        myString temp;

        temp.size = this->size + R.size;
        temp.str = new char[temp.size + 1];
        *temp.str = '\0';
        strcat(temp.str, this->str);
        strcat(temp.str, R.str);

        return temp;
    }

    //成员函数实现加号运算符重载:
    myString &operator+= (const myString &R)
    {
        char *temp = this->str;
        delete [] str;      //由于需要重新申请空间,所以要释放之前使用的空间
        this->size = R.size + this->size;
        this->str = new char[this->size + 1];
        strcpy(this->str, temp);
        strcpy(this->str, R.str);
        return *this;
    }

    //关系运算符重载(>)
    bool operator> (const myString &R) const
    {

        if (strcmp(this->str, R.str) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

};

int main()
{
    //有参构造
    myString s1("hello world");
    cout << s1.c_str() << endl;

    //拷贝构造
    myString s2(s1);
    cout << s2.c_str() << endl;

    //拷贝赋值
    myString s3;
    s3 = s1;
    cout << s3.c_str() << endl;

    //+重写
    myString s4 = s1 + s3;
    cout << s4.c_str() << endl;

    myString s5("He");
    if (s1 > s5)
    {
        cout << "yes" << endl;
    }
    else
    {
        cout << "no" << endl;
    }


    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值