C++-day4作业

#include 
#include 

using namespace std;

class MyString
{
private:
    char *str;       //记录C风格的字符串
    int size;        //记录字符串的实际长度
public:
    //无参构造
    MyString():size(10)
    {
        str=new char[size];  //构造出一个长度为10的字符串
        strcpy(str,"");      //赋值为空串
    }

    //有参构造
    MyString(const char *s)
    {
        size=strlen(s);
        str=new char[size+1];
        strcpy(str,s);
    }

    //拷贝构造
    MyString(const MyString &other):str(new char(*other.str)),size(other.size)
    {
        strcpy(this->str,other.str);
        cout<<"拷贝构造函数"<size=other.size;

            //判断原来指针空间释放被清空
            if(this->str!=NULL)
            {
                delete this->str;
            }
            this->str=new char(*other.str);
        }
        strcpy(this->str,other.str);
        cout<<"拷贝赋值函数"<s2)
    {
        cout<<"yes"<

你可能感兴趣的:(c++,开发语言)