8月23日C++作业

#include <iostream>

using namespace std;
class my_string
{
private:
    char *str;
    int len;
public:
    my_string():str(NULL)
    {
        cout<<"这是一个无参构造函数"<<endl;
    }
    my_string(char *str, int len)
    {
        this->str = str;
        this->len = len;
        cout<<"这是一个有参构造函数"<<endl;
    }
    ~my_string()
    {
        delete str;
        str = NULL;
        cout<<"这是一个析构函数"<<endl;
    }
    my_string(const my_string& other)
    {
        this->len = other.len;
        this->str = new char[other.len+1];
        char *p = this->str;
        char *q = other.str;
        int i = 0;
        for(i=0; i<len; i++)
        {
           *(p++) = *(q++);
        }
         cout<<"这是一个深拷贝构造函数"<<endl;
    }
    my_string& operator=(const my_string& other)
    {
        if(&other != this)
        {
            this->len = other.len;

            if(this->str != NULL)
            {
                delete []this->str;
                this->str = NULL;
            }
            this->str = new char[len+1];
            int i = 0;
            char *p = this->str;
            char *q = other.str;
            for(i=0; i<len; i++)
            {
               *(p++) = *(q++);
            }
        }
        cout<<"这是一个深拷贝赋值函数"<<endl;
        return *this;
    }
    char* show()
    {
       return str;
    }
    bool my_empty()
    {
        if(str == NULL)
            return false;
        else
            return true;
    }
    int my_size()
    {
        return len;
    }
};
int main()
{
    my_string stu1("cdq",3);
    cout<<stu1.show()<<endl;
    my_string stu2(stu1);
    cout<<stu2.show()<<endl;
    my_string stu3;
    stu3 = stu2;
    cout<<stu3.show()<<endl;
    cout<<stu3.my_empty()<<endl;
    cout<<stu3.my_size()<<endl;




}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值