c++操作符重载

/*-----MyString.h-----*/
class MyString {

    friend ostream& operator<<(ostream &out, MyString &s);
private:
    int m_len;
    char *m_p;
public:
    MyString();
    MyString(const char *p);
    MyString(const MyString& s);
    ~MyString();

    //重载 =
    MyString& operator=(const char *p);
    MyString& operator=(const MyString& s);

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

    //重载 ==  !=
    bool operator==(const char *p);
    bool operator==(const MyString& s);
    bool operator!=(const char *p);
    bool operator!=(const MyString& s);

    int operator<(const char *p);
    int operator>(const char *p);
    int operator<(const MyString& s);
    int operator>(const MyString& s);

    char *c_str()
    {
        return m_p;
    }
    const char *c_str2()
    {
        return m_p;
    }
    int length()
    {
        return m_len;
    }

};
/*-----MyString.cpp-----*/
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
using namespace std;
#include "MyString.h"


MyString::MyString() {
    m_len = 0;
    m_p = new char[m_len + 1];
    strcpy(m_p, "");
}

MyString::MyString(const char *p) {
    if (p == NULL) {
        m_len = 0;
        m_p = new char[m_len + 1];
        strcpy(m_p, "");
    }
    else {
        m_len = strlen(p);
        m_p = new char[m_len + 1];
        strcpy(m_p, p);
    }
}

//拷贝构造函数
//当用一个已初始化过了的自定义类类型对象去初始化另一个新构造的对象的时候,拷贝构造函数就会被自动调用
MyString::MyString(const MyString& s) {
    m_len = s.m_len;
    m_p = new char[m_len + 1];
    strcpy(m_p, s.m_p);
}

MyString::~MyString() {
    if (m_p != NULL) {
        delete[] m_p;
        m_p = NULL;
        m_len = 0;
    }
}

//s4="asd";
MyString& MyString::operator=(const char *p) {
    if (m_p != NULL) { //释放旧内存
        delete[] m_p;
        m_p = NULL;
        m_len = 0;
    }
    if (p = NULL) { //分配新内存
        m_len = 0;
        m_p = new char[m_len + 1];
        strcpy(m_p, "");
    }
    else {
        m_len = strlen(p);
        m_p = new char[m_len + 1];
        strcpy(m_p, p);
    }
    return *this;
}

//s4=s2;
MyString& MyString::operator=(const MyString& s) {
    if (m_p != NULL) { //释放旧内存
        delete[] m_p;
        m_p = NULL;
        m_len = 0;
    }
    //分配新内存
    m_len = s.m_len;
    m_p = new char[m_len + 1];
    strcpy(m_p, s.m_p);
    return *this;
}

//s4[0];
char& MyString::operator[](int index) {
    return m_p[index];
}

//cout << s4 << endl;
ostream& operator<<(ostream &out, MyString &s) {
    cout << s.m_p;
    return out;
}

//if (s2 == "s222");
bool MyString::operator==(const char *p) {
    if (p == NULL) {
        if (m_len == 0)
            return true;
        else
            return false;
    }
    else {
        if (m_len == strlen(p))
            return !strcmp(m_p, p);
        else
            return false;
    }
}

//if (s3 == s2);
bool MyString::operator==(const MyString& s) {
    if (m_len != s.m_len)
        return false;
    else
        return !strcmp(m_p, s.m_p);
}
bool MyString::operator!=(const char *p) {

    return !(*this == p);
}
bool MyString::operator!=(const MyString& s) {

    return !(*this == s);
}
//if (s3 < "bbbb")
int MyString::operator<(const char *p)
{
    return strcmp(this->m_p , p);
}

int MyString::operator>(const char *p)
{
    return strcmp(p, this->m_p);
}

int MyString::operator<(const MyString& s)
{
    return strcmp(this->m_p , s.m_p);
}

int MyString::operator>(const MyString& s)
{
    return  strcmp(s.m_p, m_p);
}
/*-----test.cpp-----*/
#include <iostream>
using namespace std;
#include "MyString.h"

void func() {
    MyString s1;
    MyString s2("s2");
    MyString s3 = s2;
    if (s2 == "s222")
        cout << "equal" << endl;
    else
        cout << "unequal" << endl;

    if (s3 == s2) 
        cout << "equal" << endl;
    else
        cout << "unequal" << endl;
}
void func1()
{
    MyString s1;
    MyString s2("s2");

    MyString s3 = s2;
    s3 = "aaa";

    int tag = (s3 < "bbbb");
    if (tag < 0 )
    {
        printf("s3 小于 bbbb");
    }
    else
    {
        printf("s3 大于 bbbb");
    }

    MyString s4 = "aaaaffff";
    strcpy(s4.c_str(), "aa111"); //MFC
    cout<<s4<<endl;
}
void func2()
{
    MyString s1(128);
    cout<<"\n请输入字符串(回车结束)";
    cin>>s1;

    cout<<s1;

}
void func3()
{
    MyString s1(128);
    cout<<"\n请输入字符串(回车结束)";
    cin>>s1;

    cout<<s1<<endl;

}
int main() {

    MyString s1;
    MyString s2("hello");
    MyString s2_2 = NULL;
    MyString s3 = s2; //拷贝构造函数
    MyString s4 = "444";

    //测试运算符重载
    s4 = "acd";
    s4 = s2;
    s4[0] = '4';

    cout << s4 << endl;

    func();
    func1();
    func2();
    func3();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值