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;
}
相关推荐
上去我就QWER1 小时前
Qt中如何获取系统版本信息
开发语言·qt
我是苏苏2 小时前
C#高级:程序查询写法性能优化提升策略(附带Gzip算法示例)
开发语言·算法·c#
木木子99992 小时前
业务架构、应用架构、数据架构、技术架构
java·开发语言·架构
sali-tec3 小时前
C# 基于halcon的视觉工作流-章56-彩图转云图
人工智能·算法·计算机视觉·c#
学涯乐码堂主7 小时前
GESP C++ 四级第一章:再谈函数(上)
c++·青少年编程·gesp·四级·学漄乐码青少年编程培训
黑岚樱梦7 小时前
代码随想录打卡day23:435.无重叠区间
算法
微露清风7 小时前
系统性学习C++-第九讲-list类
c++·学习·list
大佬,救命!!!7 小时前
C++多线程同步与互斥
开发语言·c++·学习笔记·多线程·互斥锁·同步与互斥·死锁和避免策略
Kuo-Teng7 小时前
Leetcode438. 找到字符串中所有字母异位词
java·算法·leetcode
赵文宇(温玉)8 小时前
构建内网离线的“github.com“,完美解决内网Go开发依赖
开发语言·golang·github