string类中运算符重载的实现

#include<iostream>

using namespace std;

class MyString
{
public:
    MyString();
    MyString(const int number);
    MyString(const char *ptr);
    MyString(const MyString &str);
    ~MyString();
    MyString& operator =(const MyString &str);
    char& operator [](const int index);
    bool operator ==(const MyString& str);
    bool operator !=(const MyString& str);
    bool operator >(const MyString& str);
    bool operator <(const MyString& str);
    MyString operator +(const MyString &str);
    friend ostream& operator <<(ostream &out, const MyString &str);
    friend istream& operator >>(istream &in, MyString &str);
private:
    int len;
    char *pstr;
};

MyString::MyString()
{
    len = 0;
    pstr = new char[len + 1];
    strcpy(pstr, "");
    cout << "默认构造函数" << endl;
}
MyString::MyString(const int length)
{
    if (length == 0)
    {
        len = 0; 
        pstr = new char[len + 1];
        strcpy(pstr, "");
    }
    else
    {
        len = length;
        pstr = new char[len + 1];
        memset(pstr, 0, len);
    }
}
MyString::MyString(const char *ptr) 
{
    if (ptr == nullptr) 
    {
        len = 0;
        pstr = new char[len + 1];
        strcpy(pstr, "");
    }
    else 
    {
        len = strlen(ptr);
        pstr = new char[len + 1];
        strcpy(pstr, ptr);
    }
}
//拷贝构造函数
MyString::MyString(const MyString &str)
{
    len = strlen(str.pstr);
    pstr = new char[len + 1];
    strcpy(pstr, str.pstr);
    cout << "拷贝构造函数" << endl;
}
MyString::~MyString()
{
    delete [] pstr;
    pstr = nullptr;
    len = 0;
    cout << "destructor" << endl;
}

MyString& MyString::operator=(const MyString &str)
{
    if (this == &str)
        return *this;
    if (pstr != nullptr)
        delete[] pstr;
    len = strlen(str.pstr);
    pstr = new char[len + 1];
    strcpy(pstr, str.pstr);
    return *this;
}

char& MyString::operator[](const int index)
{
    return *(pstr + index);
}
bool MyString::operator==(const MyString &str)
{
    if (strcmp(pstr, str.pstr) == 0)
        return true;
    else
        return false;
}
bool MyString::operator!=(const MyString &str)
{
    if (strcmp(pstr, str.pstr) != 0)
        return true;
    else
        false;
}
bool MyString::operator<(const MyString &str)
{
    if (strcmp(pstr, str.pstr) < 0)
        return true;
    else
        return false;
}
bool MyString::operator>(const MyString &str)
{
    if (strcmp(pstr, str.pstr) > 0)
        return true;
    else
        false;
}
MyString MyString::operator +(const MyString& str)
{
    MyString temp;
//  len = len + str.len;
    temp.pstr = new char[strlen(pstr) + strlen(str.pstr) + 1];
    sprintf(temp.pstr, "%s%s", pstr, str.pstr);
    temp.len = strlen(temp.pstr);
    return temp;
}
ostream& operator<<(ostream &out, const MyString &str) 
{
    cout << str.pstr;
    return out;
}
istream& operator>>(istream &in, MyString &str)
{
    str.pstr = new char[256];
    cin >> str.pstr;
    str.len = strlen(str.pstr);
    return in;
}
void main() 
{
    MyString str1("abc");
    cout << "str1经过初始化之后值为:" << str1 << endl;
    MyString str2;
    str2 = str1;
    cout << "str2经过str1初始化之后值为:" << str2 << endl;
    MyString str3;
    str3 = str1 + str2;
    cout << "str1与str2之和str3为:" << str3 << endl;
    cout << "请输入字符串str4:" << endl;
    MyString str4;
    cin >> str4;
    cout << "你输入的str4为:" << str4 << endl;
    if (str3 != str4)
    {
        cout << "str3不等于str4" << endl;
    }

    if (str3>str4)
    {
        cout << str3 << "大于" << str4 << endl;

    }
    if (str3<str4)
    {
        cout << str3 << "小于" << str4 << endl;
    }

    system("pause");
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/u014253011/article/details/80495913