C++——仿照string类,实现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];         //构造出一个长度为10的字符串
        strcpy(str, "");
        cout << "MyString::无参构造,this = " << this << endl;
    }

    //定义有参构造
    MyString(const char *s)          //string  s("hello world")
    {
        size = strlen(s);
        str = new char[size + 1];
        strcpy(str, s);
        cout << "MyString::有参构造,this = " << this << endl;
    }

    //定义拷贝构造函数
    MyString(const MyString &other) : str(new char[other.size + 1]), size(other.size) {
        strcpy(this->str, other.str);
        cout << "MyString::拷贝构造,this = " << this << endl;
    }

    //定义拷贝赋值函数
    MyString &operator=(const MyString &other) {
        if (this != &other)      //防止自己给自己赋值
        {
            char *p = other.str;
            char *q = this->str;
            while (*p != '\0') {
                *q = *p;
                q++;
                p++;
            }
            this->size = other.size;
        }
        cout << "MyString::拷贝赋值,this = " << this << endl;
        return *this;
    }

    //定义析构函数
    ~MyString() {
        //手动释放堆区空间
        delete[]str;
        cout << "MyString::析构函数,this = " << this << endl;
    }

    //判空函数
    bool empty() {
        if (this->size == 0)
            return true;
        else
            return false;
    }

    //size函数
    const int my_size() const {
        return this->size;
    }

    //c_str函数
    const char *c_str() {
        return this->str;
    }

    //at函数
    char &at(int pos) {
        //法一:
//        char *p = this->str;
//        while (pos--) {
//            p++;
//        }
//        return *p;
        //法二:
//        return str[pos];
        //发三:
        return *(str + pos);
    }
};

int main() {
    //调用无参构造
    MyString s1;
    cout << "my_size = " << s1.my_size() << "  str1 = " << s1.c_str() << endl;

    //调用有参构造

    MyString s2("XHJ");
    cout << "my_size = " << s2.my_size() << "  str2 = " << s2.c_str() << endl;

    //调用拷贝构造
    MyString s3(s2);
    cout << "my_size = " << s3.my_size() << "  str3 = " << s3.c_str() << endl;

    //调用at函数
    cout << "第一个字符:" << s3.at(0) << endl;
    cout << "第二个字符:" << s3.at(1) << endl;
    cout << "第三个字符:" << s3.at(2) << endl;

    //调用有参构造
    MyString s4("");
    cout << "my_size = " << s4.my_size() << "  str4 = " << s4.c_str() << endl;

    //调用empty函数
    //判断字符串s3是否为空
    if (s3.empty())
        cout << "s3字符串为空!" << endl;
    else
        cout << "s3字符串不为空!" << endl;

    //判断字符串s4是否为空
    if (s4.empty())
        cout << "s4字符串为空!" << endl;
    else
        cout << "s4字符串不为空!" << endl;

    s4 = s2;
    cout << "my_size = " << s4.my_size() << "  str4 = " << s4.c_str() << endl;
}

 运行结果如下图:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值