2025.1.10(面对对象特性封装)

作业

作业:

仿照string类,实现myString

作业:
仿照string类,实现myString
仿照string完成myString类
class myString
{
    private:
        char *str;          //记录c风格的字符串
        int size;            //记录字符串的实际长度
    public:
        //无参构造
        myString():size(10)
        {
            str = new char[size];         //构造出一个长度为10的字符串                    
        }
        //有参构造
        myString(const char *s);              //有参构造     string  s("hello wirld");
        //有参构造
        mystring(int n, char ch);                //string   s(5, 'A');
        //析构函数
        //拷贝构造函数
        //拷贝赋值函数
        //判空函数
        //size函数
        //c_str函数
        //at函数
        char &at(int index)            
        //二倍扩容    
        //实现+=运算符重载            s1 += s2
        //取地址运算符重载 
};

#include <iostream>
#include <cstring>
//#include <stdexcept>
using namespace std;
class myString
{
private:
    char *str;          // 记录C风格的字符串
    int size;           // 记录字符串的实际长度

public:
    // 无参构造
    myString() : size(10)
    {
        str = new char[size];
        str[0] = '\0';  // 初始化为空字符串
    }

    // 有参构造
    myString(const char *s)
    {
        size = strlen(s) + 1;  // 包括终止字符'\0'
        str = new char[size];
        strcpy(str, s);
    }

    // 有参构造
    myString(int n, char ch)
    {
        size = n + 1;  // 包括终止字符'\0'
        str = new char[size];
        for (int i = 0; i < n; ++i)
        {
            str[i] = ch;
        }
        str[n] = '\0';
    }

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

    // 拷贝构造函数
    myString(const myString &other)
    {
        size = other.size;
        str = new char[size];
        strcpy(str, other.str);
    }

    // 拷贝赋值函数
    myString& operator=(const myString &other)
    {
        if (this != &other)
        {
            delete[] str;
            size = other.size;
            str = new char[size];
            strcpy(str, other.str);
        }
        return *this;
    }

    // 判空函数
    bool empty() const
    {
        return str[0] == '\0';
    }

    // size函数
    int getSize() const
    {
        return size - 1;  // 不包括终止字符'\0'
    }

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

    // at函数
    char& at(int index)
    {
        if (index < 0 || index >= size - 1)
        {
            cout<<"输入有误"<<endl;
            //throw std::out_of_range("参数有误");
        }
        return str[index];
    }

    // 二倍扩容
    void resize(int new_size)
    {
        if (new_size > size)
        {
            char *new_str = new char[new_size];
            strcpy(new_str, str);
            delete[] str;
            str = new_str;
            size = new_size;
        }
    }

    // 实现+=运算符重载
    myString& operator+=(const myString &other)
    {
        int new_size = size + other.size - 1;  // 新的大小
        char *new_str = new char[new_size];
        strcpy(new_str, str);
        strcat(new_str, other.str);
        delete[] str;
        str = new_str;
        size = new_size;
        return *this;
    }

    // 取地址运算符重载
    char* data()
    {
        return str;
    }
};

int main()
{
    myString s1("Hello");
    myString s2(" World");
    s1 += s2;
    std::cout << s1.c_str() << std::endl;  // 输出: Hello World
    return 0;
}

思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值