C++ 面向对象程序设计 014:MyString ---- (北大Mooc)

文章目录


原题题目

在这里插入图片描述
在这里插入图片描述



代码实现

MyString(const MyString & a)
	{
    
    
        if(a.p == NULL)
            p = NULL;
        else
        {
    
    
            p = new char[strlen(a.p) +1];
            strcpy(p,a.p);
        }
	}
    void Copy(const MyString & w1)
    {
    
    
        p = new char[strlen(w1.p)+1];
        strcpy(p,w1.p);
    }
    friend ostream & operator << (ostream & os, const MyString & a)
    {
    
    
        os<<a.p;
        return os;
    }
    MyString & operator = (const MyString & a)
    {
    
    
        if(a.p == NULL)
            p = NULL;
        else
        {
    
    
            p = new char[strlen(a.p)+1];
            strcpy(p,a.p);
        }
        return *this;
    }
    MyString & operator = (const char* a)
    {
    
    
        if(!a)
            p = NULL;
        else
        {
    
    
            p = new char[strlen(a)+1];
            strcpy(p,a);
        }
        return *this;
    }

猜你喜欢

转载自blog.csdn.net/qq_37500516/article/details/114836666