C++mystring 2

#include <iostream>
#include <cstring>

using namespace std;

class myString
{
private:
    char *str;
    int size;
public:
    //无参构造
    myString():size(10)
    {
        str = new char [size];
        strcpy(str,"");
        cout<<"无参构造"<<endl;
    }
    //有参构造
    myString(const char *s)
    {
        size = strlen(s);
        str=new char[size+1];
        strcpy(str,s);
        cout<<"有参构造"<<endl;
    }

    //拷贝构造
    myString(const myString &other):str(new char(*(other.str))),size(other.size)
    {
        strcpy(str,other.str);
    }

    //析构函数
    ~myString()
    {
        delete str;
        cout<<"析构函数"<<endl;
    }

    //判空函数
    int empty()
    {
        if(0 ==size)
            return true;
        else
            return false;
    }
    //size函数
    int length()
    {

        return size;
    }
    //c_str 函数
    char *c_str()
    {
        return str;
    }

    //at函数
    char &at(int a)
    {
        if(a<=size)
        {
             return str[a];
        }
        else
        {
            cout<<"error"<<endl;
            return str[a];
        }

    }

    //字符串赋值 =
    myString operator=(const myString &other)
    {
        this->size=other.size;
        this->str=new char[other.size+1];
        strcat(this->str,other.str);
        return *this;
    }
    //[]函数
    char &operator[](int a)
    {
        if(a<=size)
        {
             return str[a];
        }
        else
        {
            cout<<"error"<<endl;
            return str[a];
        }

    }
    // 类内+连接两个字符串
    myString operator+(const myString &other)const
    {
        myString temp;
        temp.size=this->size+other.size;
        temp.str=new char[temp.size+1];
        strcpy(temp.str,"");
        strcpy(temp.str,this->str);
        strcat(temp.str,other.str);
        return temp;
    }

    //条件判断==是否相等
    bool operator ==(const myString &other)
    {
         if(0==strcmp(this->str,other.str))
                return true;
         else
             return false;
    }
    //比较两个字符的大小 >
    bool operator>(const myString &other)
    {
        if(strcmp(this->str,other.str)>0)
        {
            return true;
        }
        else
            return false;
    }
    //比较两个字符的大小 <
    bool operator<(const myString &other)
    {
        if(strcmp(this->str,other.str)<0)
        {
            return true;
        }
        else
            return false;
    }
    //比较两个字符的大小 >=
    bool operator>=(const myString &other)
    {
        if(strcmp(this->str,other.str)>0&&strcmp(this->str,other.str)==0)
        {
            return true;
        }
        else
            return false;
    }
    //+=的赋值
    myString &operator+=(const myString &other)
    {
        myString temp;
        temp.size=this->size+other.size;
        temp.str=new char[temp.size+1];
        strcpy(temp.str,this->str);
        strcat(temp.str,other.str);
        delete this->str;
        this->size=temp.size;
        this->str=new char[this->size+1];
        strcpy(this->str,temp.str);
        return *this;
    }

   //<<的重载
    friend ostream &operator<<(ostream &L,const myString &O);
    friend istream &operator>>(istream &L,const myString &O);
};

//全局函数实现<<运算符的重载:输出所有内容
ostream &operator<<(ostream &L,const myString &O) //L--cout
{
    L<<O.str;
    return L;
}
//全局函数实现>>运算符的重载:输入所有内容
/*
istream &operator>>(istream &L,const myString &O) //L--cin
{
    myString temp;
    temp.size=O.size;
    temp.str=new char[O.size+1];
    strcpy(temp.str,"");
    L>>temp.str;
    return L;
}
*/
using namespace std;

int main()
{
    //有参构造
    myString s1("Hello world");
    //at函数
    cout<<"s1.at(2)= "<<s1[2]<<endl;
    cout<<"--------------------------"<<endl;
    s1.at(2)='E';
    cout<<"s1="<<s1<<endl;
    cout<<"--------------------------"<<endl;
    //拷贝函数
    myString s2(s1);
    cout << "s2="<<s2<<endl;
    cout<<"--------------------------"<<endl;
    //size函数
    s2.length();
    cout<< "s2 size ="<<s2.length()<<endl;
    cout<<"--------------------------"<<endl;
       // =运算符
    myString s3="good boy";
    cout<<"s3="<<s3<<endl;
    cout<<"--------------------------"<<endl;
       //[]
    s3[2]='O';
    cout<<"s3="<<s3<<endl;
    cout<<"--------------------------"<<endl;
    //+连接两个字符串重载
    cout<<"s3+s1= "<<s3+s1<<endl;
    cout<<"--------------------------"<<endl;

    // == 判断是否相等
    if(s3 == s1)
    {
        cout<<"相等"<<endl;
    }
    else
    {
        cout<<"不相等"<<endl;
    }
    cout<<"--------------------------"<<endl;
    //+= 重载
    cout<<"s1+=s2= "<<(s1+=s2)<<endl;
    cout<<"s3+=s3= "<<(s3+=s3)<<endl;
    cout<<"--------------------------"<<endl;
    //cin >>的重载
   // cin>>s1;
    //cout<<s1<<endl;
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值