c++中mystring运算符重载

cpp 复制代码
#include <iostream>
#include <cstring>

using namespace std;

class mystring
{
    char* buf;
public:
    mystring(); //构造函数
    mystring(const char * str); //构造函数
    mystring(const mystring& str); //深拷贝函数
    void show(); //输出函数
    void setmystr(const mystring& str); //设置函数
    const char* getmystr() const; //获取函数
    void append(const mystring& str); //追加函数
    int isEqual(const mystring& str); // 比较函数
    void swap(mystring& str);//交换函数
    ~mystring(); //析构函数
/****************************************************************************/
    mystring& operator+(const mystring& str);//+
    mystring& operator=(const mystring& str);//=
    mystring& operator+=(const mystring& str);//+=
    bool operator==(const mystring& str);//==
    bool operator!=(const mystring& str);// !=
    mystring& operator++();//前++
    mystring operator++(int);//后++
    char operator[](const int index);//[]
    friend ostream& operator<<(ostream& out , const mystring& str);//cout << mystring
    friend istream& operator>>(istream& in , const mystring& str);//cin >> mystring
};

/****************************************************************************/
//+
mystring& mystring::operator+(const mystring& str)
{
    this->append(str);
    return *this;
}
//=
mystring& mystring::operator=(const mystring& str)
{
    int len = strlen(str.buf);
    buf = new char[len+1];
    strcpy(buf , str.buf);
    return *this;
}
//+=
mystring& mystring::operator+=(const mystring& str)
{
    this->append(str);
    return *this;
}
//==
bool mystring::operator==(const mystring& str)
{
    if (strcmp(this->buf , str.buf) == 0 )
        return true;
    else
        return false;
}
// !=
bool mystring::operator!=(const mystring &str)
{
    if(strcmp(buf , str.buf) == 0)
        return false;
    else
        return true;
}
//++
mystring& mystring::operator++()
{
    char * p = buf;
    while (*p != '\0') {
        ++(*p);
        p++;
    }
    return *this;
}
//后++
mystring mystring::operator++(int)
{
    mystring temp = *this;
    char * p = buf;
    while (*p != '\0') {
        ++(*p);
        p++;
    }
    return temp;
}
//[]
char mystring::operator[](const int index)
{
    int len = strlen(buf);
    if(index < len)
    {
        char * p = buf;
        return *(p+index);
    }
    return '\0';
}
//cout << mystring
ostream& operator<<(ostream& out , const mystring& str)
{
    out << str.buf;
    return out;
}
//cin >> mystring
istream& operator>>(istream& in , const mystring& str)
{
    in >> str.buf;
    return in;
}


/****************************************************************************/
//构造函数
mystring::mystring()
{
    buf = new char[1];
}
//构造函数
mystring::mystring(const char * str)
{
    int len = strlen(str);
    buf = new char[len+1];
    strcpy(buf , str);
}
//深拷贝函数
mystring::mystring(const mystring& str)
{
    int len = strlen(str.buf);
    buf = new char[len+1];
    strcpy(buf , str.buf);
}
//打印函数
void mystring::show()
{
    cout << buf << endl;
}
//设置函数
void mystring::setmystr(const mystring& str)
{
    free(this->buf);
    int len = strlen(str.buf);
    this->buf = new char[len+1];
    strcpy(this->buf , str.buf);
}
//获取函数
const char* mystring::getmystr() const
{
    return buf;
}
//追加函数
void mystring::append(const mystring& str)
{
    int len1 = strlen(this->buf);
    char* temp = new char[len1+1];
    memset(temp , 0 , len1+1);
    strcpy(temp , this->buf);
    delete[] buf;

    int len2 = strlen(str.buf);
    this->buf = new char[len1+len2+1];
    memset(buf , 0 , len1+len2+1);
    strcat(this->buf , temp);
    strcat(this->buf , str.buf);
}
//比较函数
int mystring::isEqual(const mystring& str)
{
    if(strcmp(buf , str.buf) > 0)
    {
        return 1;
    }
    else if(strcmp(buf , str.buf) < 0)
    {
        return -1;
    }
    else
        return 0;
}
//交换函数
void mystring::swap(mystring& str)
{
    char * temp = this->buf;
    this->buf = str.buf;
    str.buf = temp;
}
//析构函数
mystring::~mystring()
{
    delete[] buf;
}

int main()
{
    mystring ptr;
    mystring str = "hello world";
    str.show();
    ptr.setmystr("hello kitty");
    cout << ptr.getmystr() << endl;

    mystring qtr;
    qtr = ptr + str;
    qtr.show();

/*	ptr += str;
    ptr.show();  	*/

/*	if(ptr == str){}
    else{
        cout << "== success" << endl;
    } 				*/

/*	if(ptr != str)
    {
        cout << "!= success" << endl;
    }  				*/

/*	mystring temp = str++;
    temp.show();
    str.show(); 	*/

//	cout << str[0] <<  str[3] << str[8] << endl;

    mystring ttr;
    cout << "请输入:";
    cin >> ttr;
    cout << ttr;
    cout << endl;
    return 0;
}
相关推荐
zh_xuan5 分钟前
c++ 单例模式
开发语言·c++·单例模式
老胖闲聊31 分钟前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.11835 分钟前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之1 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?1 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头2 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
liuyang-neu2 小时前
java内存模型JMM
java·开发语言
int型码农2 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
利刃大大2 小时前
【在线五子棋对战】二、websocket && 服务器搭建
服务器·c++·websocket·网络协议·项目
UFIT2 小时前
NoSQL之redis哨兵
java·前端·算法