自定义类,运算符重载

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

class My_string
{
private:
char *cstr;
int len;

public:
My_string():cstr(NULL),len(0)//无参构造
{
}
My_string(const char *str)//有参构造
{
    this->len=strlen(str);
    this->cstr=new char[this->len+1];
    strcpy(this->cstr,str);
}
My_string(const My_string &other)//拷贝构造
{
    this->len=other.len;
    this->cstr=new char[this->len+1];
   strcpy(cstr,other.cstr);
}
~My_string() //析构函数
{
    if(this->cstr!=NULL)
    {
        delete []this->cstr;
        cstr=NULL;
        cout<<"释放"<<endl;
    }
}
bool empty()//判断是否为空
{
    return len==0?1:0;
}
int size()//返回字符串的长度
{
    return len;
}
char &at(int index)//定位函数
{
    return this->cstr[index];
}
char* c_str()//转化为C风格字符串
{
    return this->cstr;
}
const My_string operator+(const My_string & R)//+运算符重载
{
    My_string temp;
    temp.len=R.len+this->len;
    temp.cstr=new char[temp.len+1];
    if(this->cstr!=NULL)
    {
        strcpy(temp.cstr,this->cstr);
        strcat(temp.cstr,R.cstr);
    }
    else
    {
        strcpy(temp.cstr,R.cstr);
    }
    return temp;
}
bool operator==(const My_string &other)const//==运算符的重载
{
    return strcmp(this->cstr,other.cstr)==0?1:0;
}
bool operator>(const My_string &other)const//>运算符的重载
{
    return strcmp(this->cstr,other.cstr);
}
bool operator<(const My_string &other)const//<运算符的重载
{
    return strcmp(this->cstr,other.cstr);
}
My_string &operator=(const My_string &other)//=运算符重载(字符串对象)
{
    if(this!=&other)
    {
        this->len=other.len;
        if(this->cstr==NULL)
        {
            this->cstr=new char[other.len+1];
        }
        delete []cstr;
        this->cstr=new char[other.len+1];
        strcpy(this->cstr,other.cstr);
    }
    return *this;
}
My_string &operator=(const char *str)//拷贝赋值函数(字符串)
{
    this->len=strlen(str);
    if(this->cstr==NULL)
    {
        this->cstr=new char[len+1];
    }
    delete []cstr;
    this->cstr=new char[len+1];
    strcpy(this->cstr,str);
    return *this;
}
friend ostream &operator<<(ostream & L,const My_string & R);//将插入运算符重载的全局函数设为友元
friend istream &operator>>(istream & L,My_string & R);//将提取运算符重载的全局函数设为友元
};
ostream & operator<<(ostream & L,const My_string & R)//<<运算符重载
{
    L<<R.cstr;
    return L;
}
istream & operator>>(istream & L,My_string &R)//>>提取符重载
{
    if(R.cstr==NULL)
    {
        R.cstr=new char[18];
    }
    L>>R.cstr;
    R.len=strlen(R.cstr);
    return L;
}
int main()
{
    My_string s1="hello";
    My_string s2("world");
    My_string s3;
    cin>>s3;
    s1=s3+s2;
    cout<<"s1= "<<s1<<endl;
   /* My_string s4("world");
    s3=s1+s2;
    cout<<s3<<" "<<s3.size()<<endl;//helloworld 10
    if(s2==s4)
    {
        cout<<"equal"<<endl;//equal
    }
    else
    {
        cout<<"differ"<<endl;
    }
    if(s1>s2)
    {
        cout<<"big"<<endl;
    }
    else
    {
        cout<<"small"<<endl;//small
    }
    if(s1<s2)
    {
        cout<<"big"<<endl;//big
    }
    else
    {
        cout<<"small"<<endl;
    }*/
   // cin>>s3;
    //cout<<s3<<" "<<s3.size()<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值