C++(day4)

思维导图

封装Mystring

#include 
#include

using namespace std;

class Mystring{
public:
    //无参构造函数
    Mystring():size(10){
        str=new char[size];
        strcpy(str,"");
        cout<<"无参构造函数"<size=other.size;
        this->str=new char[this->size];
        strcpy(this->str,other.str);
        cout<<"拷贝构造函数"<size=other.size;
            strcpy(this->str,other.str);
        }
        cout<<"拷贝赋值函数"<str);
    }
    //size函数
    int strsize()const{
        return strlen(this->str);
    }
    //c_str函数
    char *c_str(){
        return this->str;
    }
    //at函数
    char &at(int pos){
        return *(this->str+pos-1);
    }
    //加号运算符重载
    Mystring operator+(const Mystring &R)const{
        Mystring temp;
        strcat(temp.str,this->str);
        strcat(temp.str,R.str);
        return temp;
    }
    //加等于运算符重载
    Mystring &operator+=(const Mystring &R){
        strcat(this->str,R.str);
        return *this;
    }
    //关系运算符重载(>)
    bool operator>(const Mystring &R)const{
        if(strcmp(this->str,R.str)>0){
            return true;
        }
        else{
            return false;
        }
    }
    //中括号运算符重载
    char &operator[](int pos)const{
        return *(this->str+pos-1);
    }
    //展示函数
    void show(){
        cout<str2){
        cout<<"str3>str2"<

你可能感兴趣的:(c++,开发语言)