实现String类

#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class MyString
{
public:
    MyString(const char *str = "");//有参构造
    MyString(const MyString &other);//拷贝构造

    ~MyString();//析构

    MyString &operator=(const char *str);//重载=
    MyString &operator=(const MyString &otther);//重载=

    char &operator[](unsigned int index);//重载[]

    friend ostream &operator<<(ostream &os, MyString &str);
    friend istream &operator>>(istream &is, MyString &str);
private:
    char *m_str;
};

MyString::MyString(const char * str)//有参构造
{
    if (NULL == str)
    {
        this->m_str = new char[1]{ 0 };
    }
    else
    {
        int len = strlen(str);
        this->m_str = new char[len + 1]{ 0 };
        strcpy(this->m_str, str);
    }
}

MyString::MyString(const MyString &other)
{

    if (NULL == other.m_str)
    {
        this->m_str = new char[1]{ 0 };
    }
    else
    {
        int len = strlen(other.m_str);
        this->m_str = new char[len + 1]{ 0 };
        strcpy(this->m_str, other.m_str);
    }
}
MyString::~MyString()
{
    if (this->m_str != NULL)
    {
        cout << "析构" << endl;
        delete[]this->m_str;
        this->m_str = NULL;
    }
}

MyString &MyString::operator=(const char *str)
{
    if (NULL == this->m_str)
    {
        delete[]this->m_str;
    }
    if (NULL == str)
    {
        this->m_str = new char[1]{ 0 };
    }
    else
    {
        int len = strlen(str);
        this->m_str = new char[len + 1];
        strcpy(this->m_str, str);
    }
    return *this;
}

MyString &MyString::operator=(const MyString &other)
{
    if (this == &other)//自身赋值
    {
        return *this;
    }
    if (this->m_str != NULL)
    {
        delete[]this->m_str;
    }
    if (NULL == other.m_str)
    {
        this->m_str = new char[1]{ 0 };
    }
    else
    {
        int len = strlen(other.m_str);
        this->m_str = new char[len + 1]{ 0 };
        strcpy(this->m_str, other.m_str);
    }
    return *this;
}

#if 0
operator[]//不抛异常
at()//会抛异常
#endif

char &MyString::operator[](unsigned int index)
{
    return this->m_str[index];
}

ostream &operator<<(ostream &os, MyString &str)
{
    os << str.m_str;
    return os;
}

istream &operator >>(istream &is, MyString &str)
{
    char buf[100] = { 0 };
    if (str.m_str != NULL)
    {
        delete[]str.m_str;
    }
    is >> buf;//接受用户的信息
    int len = strlen(buf);
    str.m_str = new char[len + 1]{ 0 };
    strcpy(str.m_str, buf);
    return is;
}

void main()
{
    MyString s1("hi");//有参构造

    MyString s2(s1);//拷贝构造 旧值初始化新值

    MyString s3("ABin");
    cout <<"有参构造:"<< s1 << endl;
    cout <<"拷贝构造:" <<s2 << endl;
    cout << "....:" << s3 << endl;

    //s3.~MyString();//析构

    s2 = s3;
    cout << "operator= : " << s2 << endl;

    s1 = "abin";
    cout << "字符串operator= : " << s1 << endl;

    MyString s4;
    cin >> s4;
    cout << s4 << " 分割 :" << s4[2] << endl;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值