作业2.1————仿照string类,封装一个My_string类,并实现相关功能

文章描述了一个简单的C++自定义字符串类`My_string`的实现,包括无参构造、有参构造(字符数组和字符填充)、析构、拷贝构造、拷贝赋值、`c_str`、`size`、`empty`和`at`等成员函数。示例展示了类的使用,如创建对象、赋值和访问字符串元素。
摘要由CSDN通过智能技术生成

//无参构造默认长度为15

My_string():size(15) { data = new char[size]; data[0] = '\0'; }

//有参构造

My_string(const char *str){}

//有参构造

My_string(int n, char ch){}

//析构函数

~My_string(){}

//拷贝构造函数

//拷贝赋值函数

//c_str函数

//size函数

//empty函数

//at函数

代码

#include <iostream>
#include<string.h>

using namespace std;
class My_string
{
    private:
        char *data;
        int size;
    public:
        //无参构造默认长度为15
        My_string():size(15)
        {
            data = new char[size];
            data[0] = '\0';
            cout<<"无参构造"<<endl;
        }

        //有参构造
        My_string(const char *str)
        {
            size=sizeof (str);
            data = new char[size];
            strcpy(data,str);
            cout<<"有参构造"<<endl;
        }
        My_string(int n, char ch)
        {
            data = new char[n];
            size=n;

            for(int i=0;i<n;i++)
            {
                data[i]=ch;
            }
            cout<<"有参构造1"<<endl;
        }
        //析构函数
        ~My_string()
        {
            delete []data;
            data=nullptr;
            cout<<"析构函数"<<endl;
        }
        //拷贝构造函数
        My_string(const My_string &other):data(new char[other.size]),size(other.size)
        {

            strcpy(data,other.data);
            cout<<"拷贝构造函数"<<endl;
        }

         //拷贝赋值函数
        My_string &operator=(const My_string &other)
        {
            if(other.size >size)
            {
                delete []data;
                data = new char[other.size];
            }
            strcpy(data,other.data);
            size = other.size;
            cout<<"拷贝赋值函数"<<endl;
            return *this;
        }

        //c_str函数
        const char *c_str()
        {
            return data;
        }
        //size函数
        int s_size()
        {
            return strlen(this->data);
        }
        //empty函数
        bool empty()
        {
            return data[0] == 0;
        }
        //at函数
        char &at(int n)
        {
            return data[n];
        }
};



int main()
{   
    My_string s1("hello");
    cout<<endl;
    My_string s2(10,'A');
    cout<<endl;
    My_string s3 = s2;
    My_string s4;
    s4 = s1;
    cout<<endl;
    printf("%s\n",s1.c_str());
    printf("%s\n",s2.c_str());
    cout<<endl;
    printf("s1 size = %d\n",s1.s_size());
    printf("s2 size = %d\n",s2.s_size());
    cout<<endl;
    My_string s5;
    if(s5.empty())
    {
        cout<<"空"<<endl;
    }
    else
    {
        cout<<"不空"<<endl;
    }
    s1.at(2) = 'L';
    cout<<s1.at(2)<<endl;
    cout<<s1.at(1)<<endl;
    cout<<endl;
    return 0;
}

执行实现结果

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值