重写string类

#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <stddef.h>
#include <string.h>


class MyString
{
public:
    MyString(const char* str);
    bool operator > (const MyString &mystring);
    MyString& operator = (const MyString &mystring);
    MyString operator + (const MyString &mystring);
    bool operator == (const MyString &mystring);
    ~MyString();
    void display();
private:
     char * m_mystring;

};

#endif // MYSTRING_H
#include "mystring.h"

MyString::MyString(const char* mystring)
{
    if(mystring==NULL)
    {
        m_mystring = new char[1];
        *m_mystring ='\0';
    }else
    {
        int len = strlen(mystring);
        m_mystring = new char[len + 1];
        strcpy(m_mystring,mystring);

    }

}
MyString& MyString::operator =(const MyString &mystring)
{
    if(this == &mystring)
    {
        return *this;
    }
    delete[] m_mystring;
    int len = strlen(mystring.m_mystring);
    this->m_mystring = new char[len + 1];
    strcpy(this->m_mystring,mystring.m_mystring);
    return *this;



}
MyString MyString::operator +(const MyString &mystring)
{
    int len =strlen(this->m_mystring) + strlen(mystring.m_mystring);
    MyString str("");
    str.m_mystring = new char [len + 1];
    memset(str.m_mystring,0,len + 1);
    strcpy(str.m_mystring,this->m_mystring);
    strcat(str.m_mystring,mystring.m_mystring);
    return str;


}

bool MyString::operator ==(const MyString &mystring)
{
    if(strcmp(this->m_mystring,mystring.m_mystring))
    {
        return true;
    }
    else
    {
        return false;

    }

}
bool MyString::operator >(const MyString &mystring)
{
    if(strlen(this->m_mystring) > strlen(mystring.m_mystring))
    {
        return true;
    }
    else
    {
        return false;

    }

}
MyString::~MyString()
{
   delete[]m_mystring;

}
void MyString::display()
{
    for ( int i =0; i < strlen(this->m_mystring);i++)
    {
        std::cout<<this->m_mystring[i];
    }

}
这里写代码片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值