c语言入门:c++运算符重载

MyString 类:

#ifndef MYSTRING_H
#define MYSTRING_H

#include <iostream>
using namespace std;

class MyString
{
public:
    friend ostream& operator<<(ostream& o,MyString& str);
    friend istream& operator>>(istream& o,MyString& str);

    MyString(char* str);
    MyString(const MyString& str);
    ~MyString();

    MyString& operator=(const MyString& str);
    char& operator[](int i);
    bool operator==( MyString& str);
    bool operator==(const char* str);
    bool operator!=( MyString& str);
    bool operator!=(const char* str);

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



private:
    char* m_str;
    int m_len;

};

#endif // MYSTRING_H
#include "mystring.h"
#include <string.h>


MyString::MyString(char* str)
{

    m_len=strlen(str)+1;
    m_str=(char*)malloc(m_len);
    strcpy(m_str,str);
}
MyString::MyString(const MyString& str)
{
    m_len=strlen(str.m_str)+1;
    m_str=(char*)malloc(m_len);
    strcpy(m_str,str.m_str);

}
MyString::~MyString()
{

    m_len=0;
    if(m_str!=NULL){
        free(m_str);
        m_str=NULL;
        cout<<"free"<<endl;
    }


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

istream& operator>>(istream& i,MyString& str){
    i >> str.m_str;
    return i;
}

MyString& MyString::operator=(const MyString& str){

    m_len=strlen(str.m_str)+1;
    m_str=(char*)malloc(m_len);
    strcpy(m_str,str.m_str);
    return *this;
}

char& MyString::operator[](int i){

    return m_str[i];
}


bool MyString::operator==( MyString& str){

    if(m_len!=str.m_len){
        return false;
    }else{

        for(int i=0;i<m_len;i++){
            if(m_str[i]!=str[i]){
                return false;
            }

        }


    }
    return true;

}


bool MyString::operator==(const char* str){

    if(m_len!=strlen(str)+1){
        return false;
    }else{

        for(int i=0;i<m_len;i++){
            if(m_str[i]!=str[i]){
                return false;
            }

        }


    }
    return true;

}
bool MyString::operator!=(MyString& str){


    return !(*this==str);

}
bool MyString::operator!=(const char* str){
    return !(*this==str);
}

int MyString::operator<( MyString& str){
    return strcmp(m_str,str.m_str);
}
int MyString::operator<(const char* str){
    return strcmp(m_str,str);
}
int MyString::operator>( MyString& str){
    return strcmp(m_str,str.m_str);
}
int MyString::operator>(const char* str){
    return strcmp(m_str,str);
}


测试用例:


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

int main()
{


    MyString str1("12333");
    MyString str2=str1;

    MyString str3("123111");
    //    str3=str1;

    //    str3[1]='2';

    //    cout << str3[1];

//    if(str3!=str1){
//        cout<<"不相等";

//    }else{
//        cout<<"相等";
//    }



    cout << "请输入str3:" << endl;
    cin >> str3;
    cout << str3 << endl;

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值