使用C++封装String,并为其重载运算符

        本次重载的运算符有 +、[]、==、>=、>>、<<。需要注意>>和<<不需要去ostream和istream中去添加友元,也不能在类中直接定义重载,需要在类外定义函数,类内声明友元才行。

代码:

#include <iostream>
#include <cstring>

using namespace std;

class MyString{
    char *str; //记录C风格字符串
    int size; //记录字符串实际长度
public:
    //无参构造
    MyString():size(10){
        str = new char[size];
    }
    //有参构造
    MyString(const char *s){
        size = strlen(s) + 1;
        str = new char[size];
        strcpy(str, s);
    }
    //拷贝构造
    MyString(char *s, int res):str(new char(*s)), size(res){
        strcpy(this->str, s);
        this->size = res;
        cout << "拷贝构造" << endl;
    }
    //拷贝赋值
    MyString & operator=(const MyString &other ){
        if(this != &other){
            strcpy(this->str, other.str);
            this->size = other.size;
        }
        cout << "拷贝赋值" << endl;
        return *this;
    }
    //析构函数
    ~MyString(){
        cout << "析构" << endl;
    }
    //判空函数
    bool ifNone(){
        if(strlen(this->str) == 0){
            return 0;
        }
        else{
            return 1;
        }
    }
    //size函数
    int sizeofstr(){
        return this->size;
    }
    //c_str函数
    const char *c_str(){
        return str;
    }
    //at函数
    int at(const char *p){
        int n = strlen(p);

        for(int i = 0; i < size-1; i ++){
            int type = 0;
            for(int j = 0; j < n; j ++){
                if(*((this->str)+j+i) == *(p+j)){
                    type ++;
                }
            }
            if(type == n){
                return i + 1;
            }
        }
        return -1;
    }

    //重载+
    const MyString operator+(const MyString &R)const{
        MyString temp;
        strcpy(temp.str,this->str);
        strcat(temp.str,R.str);
        temp.size = strlen(temp.str);
        return temp;
    }
    //重载[]
    char & operator[](const int n)const{
        for(int i = 0; i < this->size - 2; i ++){
            if(i == n){
                return *(this->str+i);
            }
        }
        char *p = NULL;
        return *p;
    }
    //重载+=
    const MyString & operator+=(const MyString &R){
        strcat(this->str,R.str);
        this->size = strlen(this->str);
        return *this;
    }
    //重载==
    bool operator==(const MyString &R)const{
        if(this->size != R.size){
            return false;
        }
        else{
            for(int i = 0; i < this->size - 2; i ++){
                if(*(this->str+i) != *(R.str+i)){
                    return false;
                }
            }
        }
        return true;
    }
    //重载>=
    bool operator>=(const MyString &R)const{
        int max = 0;
        if(this->size < R.size){
            max = this->size;
        }
        else{
            max = R.size;
        }
        for(int i = 0; i < max-2; i ++){
            if(*(this->str+i) - *(R.str+i) < 0){
                return false;
            }
        }

        return true;
    }
    //以下省略
        //重载>
        //重载<
        //重载<=
        //重载>=

    friend ostream & operator<<(ostream &L, MyString &R);
    friend istream & operator>>(istream &L, MyString &R);
};

//重载>>
ostream & operator<<(ostream &L, MyString &R){
    cout << R.c_str() << endl;
    return L;
}


//重载<<
istream & operator>>(istream &L, MyString &R){
    char mid[R.size];
    cin >> mid;
    strcpy(R.str,mid);
    return L;
}

int main()
{
    MyString s1("xzg");
    cout << "s1 = " << s1.c_str() << endl;

    MyString s2 = s1;
    cout << "s2 = " << s2.c_str() << endl;

    MyString s3;
    s3 = s2;
    cout << "s3 = " << s3.c_str() << endl;

    MyString s4 = ("");

    if(!s4.ifNone()){
        cout << "字符串s4为空" << endl;
    }
    else{
        cout << "字符串s4不为空" << endl;
    }

    if(!s2.ifNone()){
        cout << "字符串s2为空" << endl;
    }
    else{
        cout << "字符串s2不为空" << endl;
    }

    MyString s5 = ("adhbadfhxxc");
    cout << "s5 = " << s5.c_str() << endl;
    cout << "s5长度为: " << s5.sizeofstr() - 1 << endl;

    cout << "fhxx 在字符串中首次出现的位置是: " << s5.at("fhxx") << endl;

    MyString s6;
    s6 = s5 + s2;
    cout << "s6 = " << s6.c_str() << endl;

    s6[3] = 'c';
    cout << "s6 = " << s6.c_str() << endl;

    MyString s7 = "54844";
    s7 += s6;
    cout << "s7 = " << s7.c_str() << endl;

    if(s2 == s1){
        cout << "true" << endl;
    }
    else{
        cout << "false" << endl;
    }

    if(s2 >= s1){
        cout << "true" << endl;
    }
    else{
        cout << "false" << endl;
    }

    cout << s7 << s6 << endl;
    MyString s8;
    cin >> s8;
    cout << "s8 = " << s8 << endl;
    return 0;
}

结果:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值