C++ 对象的拷贝、赋值、清理和移动(MyString类)

MyString类

MyString.h

#ifndef MYSTRING_H
#define MYSTRING_H

#include 
using namespace std;

class MyString
{
    private:
        char*  str = nullptr;
        unsigned int MaxSize = 0;
    public:
        MyString();                /*默认构造函数*/
        MyString(unsigned int n);  /*有参构造函数*/
        MyString(const char* S);   /*有参构造函数*/
        MyString(const MyString& S);                  /*拷贝构造函数*/
        MyString& operator= (const MyString& S);      /*拷贝赋值运算符重载*/
        ~MyString();  /*析构函数*/
        MyString( MyString && S) noexcept;            /*移动构造函数*/
        MyString& operator= (MyString&& S) noexcept;  /*移动赋值运算符重载*/
        friend ostream& operator<< (ostream& Output, MyString& S);
        friend istream& operator>> (istream& Input, MyString& S);
        char& operator[] (unsigned int i);
  //   const char& operrator[] (unsigned int i)  const ;
};
#endif

MyString.c

#include "MyString.h"

/*获取字符串长度*/
unsigned int Length(const char* S)
{
    const char* p = S;
    unsigned int L = 0;
    while (*p != '\0')
    {
        p++;
        L++;
    }
    return L;
}
/*默认构造函数*/
MyString::MyString()
{
    cout << "无参构造函数:";
}
/*创建可包含n个字符的空字符串*/
MyString::MyString(unsigned int n)
{
    MaxSize = n + 1;
    str = new char[MaxSize] {};
}
/*构造函数,写入字符串*/
MyString::MyString(const char* S)
{
    MaxSize = Length(S) + 1;
    str = new char[MaxSize] {};
    for (unsigned int i = 0; i < (MaxSize - 1); i++)
    {
        str[i] = S[i];
    }
    str[MaxSize - 1] = '\0';
    cout << "有参构造函数:" ;
}
/*拷贝构造函数*/
MyString::MyString(const MyString& S)
{
    MaxSize = S.MaxSize;
    str = new char[MaxSize] {};
    for (unsigned int i = 0; i < MaxSize; i++)
    {
       str[i] = S.str[i];
    }
    cout << "拷贝构造函数:";
}
/*拷贝赋值运算符重载*/
MyString& MyString::operator= (const MyString& S)
{
    delete[] str;
    MaxSize = S.MaxSize;
    str = new char[MaxSize] {};
    for (unsigned int i = 0; i < MaxSize; i++)
    {
        str[i] = S.str[i];
    }
    cout << "拷贝赋值运算符重载:";
    return *this;
}
/*析构函数*/
MyString::~MyString()
{
    if (str != nullptr)
        delete[] str;
    cout << "析构函数" << endl;
}
/*移动构造函数*/
MyString::MyString(MyString&& S) noexcept
{
    delete[] str;
    MaxSize = S.MaxSize;
    str =S.str;
    S.str = nullptr;
    cout << "移动构造函数:";
}
/*移动赋值运算符重载*/
MyString& MyString::operator= (MyString&& S) noexcept
{
    delete[] str;
    MaxSize = S.MaxSize;
    str = S.str;
    S.str = nullptr;
    cout << "移动赋值运算符重载:";
    return *this;
}

ostream& operator<< (ostream& Output, MyString& S)
{
    Output << S.str;
    return Output;
}
istream& operator>> (istream& Input, MyString& S)
{
    Input >> S.str;
    return Input;
}

char& MyString::operator [] (unsigned int i) 
{
    return str[i];
}

main.c

#include 
using namespace std;

#include "MyString.h"

MyString GetMyString(void)
{
    MyString temp = "C++";
    return temp;
}

void MyString_Test(void)
{
    MyString S1;         //无参构造函数
    cout << "S1 " << endl;
    char str[] = { "Hello World!" };
    MyString S2{ str };   //有参构造函数
    cout << "S2 " << S2 << endl;
    MyString S3{ S2 };    //拷贝构造函数
    cout << "S3 " << S3 << endl;
    S1 = S3;             //拷贝赋值运算符重载
    cout << "S1 " << S1 << endl;
    MyString S4{ move(S1) };  //移动构造函数
    // MyString S4{ GetMyString() };    
    cout << "S4 " << S4 << endl;
    S4 = GetMyString();  //移动赋值运算符重载
    cout << "S4 " << S4 << endl;
    MyString S5{ 3 };
    S5[0] = 'A';
    S5[1] = 'B';
    S5[2] = 'C';
    cout << S5 << endl;
}

int main()
{
   MyString_Test();
}

结果:

C++ 对象的拷贝、赋值、清理和移动(MyString类)_第1张图片

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