在My_string类的基础上,完成运算符重载

作业:

        在My_string类的基础上,完成运算符重载

        算术运算符:+

        赋值运算符:+=

        下标运算符:[]

        关系运算符:>、=、

        插入提取运算符:>

        要求:注意数据的保护(const)

头文件:

#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class My_string
{
private:
    char *data;
    int size;
public:

    //无参构造函数,默认值15
    My_string();

    //有参构造函数
    My_string(const char *str);

    //有参构造函数
    My_string(int n,char ch);

    //析构函数
    ~My_string();

    //拷贝构造函数
    My_string(const My_string &other);

    //拷贝赋值函数
    My_string & operator = (const My_string &other);

    //输出字符串地址函数
    char *c_str();

    //字符串个数输出函数
    bool size_t();

    //empty函数
    int empty();

    //at函数
    char & at(int num);

    //遍历
    void show();

    //实现+运算符重载
    const My_string operator+(const My_string &R);

    //实现+=运算符重载
    My_string & operator+=(const My_string &R);

    //实现[]运算符重载
    char &operator[](int index);

    //实现>运算符重载
    int operator>(const My_string &R)const;

    //实现<运算符重载
    int operator<(const My_string &R)const;

    //实现==运算符重载
    bool operator==(const My_string &R)const;

    //实现!=运算符重载

    bool operator!=(const My_string &R)const;

    //实现>=运算符重载
    bool operator>=(const My_string &R)const;

    //实现<=运算符重载
    bool operator<=(const My_string &R)const;

    friend ostream &operator<<(ostream &L,const My_string &s);
    friend istream &operator>>(istream &I,const My_string &s);

};



#endif // HEADER_H

源文件:

#include <iostream>
#include "header.h"
#include <string>
#include <cstring>



//无参构造函数,默认值15
My_string::My_string():size(15)
{
    data = new char[size+1];
    data[0] = '\0';
    cout<<"无参构造"<<endl;
}

//有参构造函数
My_string::My_string(const char *str):size(strlen(str))
{
    data = new char[size+1];
    strcpy(data,str);
    cout<<"有参构造"<<endl;
}

//有参构造函数
My_string::My_string(int n,char ch):size(n)
{
    data = new char[size+1];
    for(int i=0;i<size;i++)
    {
        data[i] = ch;
    }
    cout<<"有参构造"<<endl;

}

//析构函数
My_string::~My_string()
{
    delete []data;
    data = nullptr;
    size = 0;
    cout<<"析构函数"<<endl;
}

//拷贝构造函数
My_string::My_string(const My_string &other):size(other.size)
{
    data = new char[size+1];
    strcpy(data,other.data);
    cout<<"拷贝构造函数"<<endl;
}
//拷贝赋值函数
My_string & My_string::operator = (const My_string &other)
{
    if(this->size<other.size)
    {
        delete []data;
        data = new char[other.size];
    }
    this->size = other.size;
    strcpy(this->data,other.data);
    cout<<"拷贝赋值"<<endl;
    return *this;
}

//输出字符串地址函数
char *My_string::c_str()
{
    return data;
}

//字符串个数输出函数
bool My_string::size_t()
{
    return size;
}

//empty函数
int My_string::empty()
{
    return 0 == size;
}

//at函数
char & My_string::at(int num)
{
    if(num<0 || num>size-2)
    {
        cout<<"所给数据不合理"<<endl;
        return data[-1];
    }
    return data[num];
}

//遍历
void My_string::show()
{
    cout<<"data = "<<this->data<<endl;
    cout<<"size = "<<this->size<<endl;
}

//实现+运算符重载
const My_string My_string::operator+(const My_string &R)
{
    My_string temp;
    temp.size = this->size+R.size;
    temp.data = new char[temp.size+1];
    strcpy(temp.data,this->data);
    strcat(temp.data,R.data);
    return temp;
}


//实现+=运算符重载
My_string & My_string::operator+=(const My_string &R)
{
    My_string temp;
    temp.size = (this->size+R.size);
    temp.data = new char[temp.size+1];
    strcpy(temp.data,this->data);
    strcat(temp.data,R.data);
    delete []this->data;
    this->data = nullptr;
    this->data = new char[temp.size+1];
    strcpy(this->data,temp.data);
    delete []temp.data;
    temp.data = nullptr;
    this->size = temp.size;
    cout<<this->data<<endl;
    cout<<size<<endl;
    return *this;
}

//实现[]运算符重载
char &My_string::operator[](int index)
{
    if(index<0 || index>=this->size)
    {
        cout<<"下标访问越界"<<endl;
    }
    //cout<<data[index]<<endl;
    return this->data[index];
}

//实现>运算符重载
int My_string::operator>(const My_string &R)const
{
    int i = 0;
    int temp;
    for(i=0;i<(this->size>=R.size?R.size:this->size);i++)
    {
        temp = this->data[i]-R.data[i];
        if(temp)
        {

        }
        if(this->data[i]>R.data[i])
        {
            return 1;
        }
        else if(this->data[i]<R.data[i])
        {
            return -1;
        }
    }

    return this->data[i]-R.data[i];
}

//实现<运算符重载
int My_string::operator<(const My_string &R)const
{
    int i = 0;
    for(i=0;i<(this->size<=R.size?R.size:this->size);i++)
    {
        if(this->data[i]<R.data[i])
        {
            return 1;
        }
        else if(this->data[i]>R.data[i])
        {
            return -1;
        }
    }

    return this->data[i]-R.data[i];
}

//实现==运算符重载
bool My_string::operator==(const My_string &R)const
{
    if(this->size == R.size)
    {
        for(int i=0;i<size;i++)
        {
            if(this->data[i] != R.data[i])
            {
                return 0;
            }
        }
    }
    else
    {
        return 0;
    }
    return 1;
}

//实现!=运算符重载

bool My_string::operator!=(const My_string &R)const
{
    if(this->size != R.size)
    {
        return 1;
    }
    else if(this->size == R.size)
    {
        for(int i=0;i<this->size;i++)
        {
            if(this->data[i] != R.data[i])
            {
                return 1;
            }
        }
    }
    return 0;
}

//实现>=运算符重载

bool My_string::operator>=(const My_string &R)const
{
    int i=0;
    if(this->size>=R.size)
    {
        for(i=0;i<this->size;i++)
        {
            if(this->data[i] > R.data[i])
            {
                return 1;
            }
            else if(this->data[i] < R.data[i])
            {
                return 0;
            }
        }
    }
    else
    {
        return 0;
    }
    return 1;
}

//实现<=运算符重载
bool My_string::operator<=(const My_string &R)const
{
    int i=0;
    if(this->size<=R.size)
    {
        for(i=0;i<R.size;i++)
        {
            if(this->data[i] < R.data[i])
            {
                return 1;
            }
            else if(this->data[i] > R.data[i])
            {
                return 0;
            }
        }
    }
    else
    {
        return 0;
    }
    return 0;


}



//实现>>运算符重载
ostream &operator<<(ostream &L,const My_string &s)
{
    L<<s.data<<endl;
    return L;
}

istream &operator>>(istream &I,const My_string &s)
{
    I>>s.data;
    return I;
}

主函数调用:

#include <iostream>
#include <string>
#include <cstring>
#include "header.h"

using namespace std;


int main()
{
    My_string s1("abc");
    My_string s2("bcd");
    My_string s3("cde");
    s3 = s1+s2;
    My_string s4("ccc");
    //s4+=s3;
    if(s2>s1)
    {
        cout<<"s2大"<<endl;
    }


    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鱼YY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值