运算符重载

2019-10-09  本文已影响0人  1墨家巨子

mystring.cpp

#include<cstring>
#include<iostream>
using namespace std;
class MyString{
public:
    MyString();//返回空串
    MyString(const char*p);//若p不为空,正常拷贝,为空,则返回空串
    MyString(const MyString& obj);
    ~MyString();
    //赋值运算符重载
    MyString& operator=(const char*p);//s="a";
    MyString& operator=(const MyString& s);//s=s1;
    //[]
    char &operator[](int index);//s[index]
    //<<  cout<<s4
    friend ostream & operator<<(ostream&out,MyString& obj);
    friend istream & operator>>(istream&in,MyString& obj);
    //== if(s2=="s2")或者if(s3==s2)
    bool operator==(const char*p)const;//this指针不能变
    bool operator==(const MyString&s)const;

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

    //<  if(s<"bbb")或者if(s3<s2)
    int operator<(const char* p);
    int operator<(const MyString& s);
    int operator>(const char* p);
    int operator>(const MyString& s);
private:
    int m_length;
    char *m_Space;

};
MyString::MyString(){
    m_length=10;
    m_Space = new char[m_length+1];
    strcpy(m_Space,"\0");
}
MyString::MyString(const char*p){
    m_length=strlen(p);
    m_Space = new char[m_length+1];
    if(p==NULL)
       strcpy(m_Space,"\0");
    else
       strcpy(m_Space,p);
}
MyString::MyString(const MyString& obj){
    m_length=strlen(obj.m_Space);
    m_Space = new char[m_length];
    strcpy(m_Space,obj.m_Space);
}
MyString::~MyString(){
    delete [] m_Space;
    m_Space=NULL;
}
MyString & MyString::operator=(const char*p){
    if(p==NULL)
       strcpy(m_Space,"\0");
    else
       strcpy(m_Space,p);
    return *this;
}
MyString & MyString::operator=(const MyString& s){
    strcpy(this->m_Space,s.m_Space);
    return *this;
}
char & MyString::operator[](int index){
    char ch;
    ch=this->m_Space[index];
    return ch;
}

ostream & operator<<(ostream&out,MyString& obj){
    out<<obj.m_Space;
}
istream & operator>>(istream&in,MyString& obj){
    in>>obj.m_Space;
}

bool MyString::operator==(const char*p)const{
     if(strcmp(m_Space,p)==0){
         return true;
     }
     else {
         return false;
     }
}
bool MyString::operator==(const MyString& s)const{
    if(0==strcmp(m_Space,s.m_Space)){
        return true;
    }
    else {
        return false;
    }
}
bool MyString::operator!=(const char*p)const{
    if(strcmp(m_Space,p)!=0){
        return true;
    }
    else {
        return false;
    }
}
bool MyString::operator!=(const MyString&s)const{
    if(0!=strcmp(m_Space,s.m_Space)){
        return true;
    }
    else {
        return false;
    }
}
int  MyString::operator<(const char* p){

    if(strcmp(m_Space,p)<0){
        return 1;
    }
    else {
        return 0;
    }

}
int  MyString::operator<(const MyString& s){

    if(strcmp(m_Space,s.m_Space)<0){
        return 1;
    }
    else {
        return 0;
    }
}
int  MyString::operator>(const char* p){

    if(strcmp(m_Space,p)>0){
        return 1;
    }
    else {
        return 0;
    }
}
int  MyString::operator>(const MyString& s){

    if(strcmp(m_Space,s.m_Space)>0){
        return 1;
    }
    else {
        return 0;
    }
}

main.cpp

#include <iostream>
#include <mystring.cpp>
using namespace std;
int main()
{
    MyString str;
    MyString str1("apple");
    str=str1;             // 赋值重载
    cout<<str<<endl;      // <<重载
   cin>>str;             // >>重载
    char ch=str[0];       // []重载
    cout<<ch<<endl;
    bool c1=(str==str1);
    bool c2=(str=="APPLE");
    bool c3=(str!=str1);
    bool c4=(str!="APPLE");
    bool c5=(str<str1);
    bool c6=(str<"bill");
    bool c7=(str>str1);
    bool c8=(str>"APPLE");
    cout<<c1<<endl;
    cout<<c2<<endl;
    cout<<c3<<endl;
    cout<<c4<<endl;
    cout<<c5<<endl;
    cout<<c6<<endl;
    cout<<c7<<endl;
    cout<<c8<<endl;
    cout << "Hello World!" << endl;
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读