C++作业(0824-林雪阵)

本文档展示了如何定义一个名为my_string的类,该类实现了包括构造函数、析构函数、拷贝构造函数、赋值运算符在内的功能。类中还包含了关系运算符(>、<、<=、>=、==、!=)的重载,以及加号运算符(+)的重载,用于字符串拼接。此外,还提供了取成员运算符([])以访问字符串中的单个字符,以及成员函数如my_empty()、my_size()和my_str()。最后,通过主函数展示了类的使用,包括创建对象、拷贝、赋值、比较和字符串操作等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义my_string 类重载的运算符全部重载掉

关系运算符:>、=、<=、!=

加号运算符:+

取成员运算符:[]

赋值运算符: =

分文件编程:头文件

#ifndef MY_STRING
#define MY_STRING

#include <iostream>
#include<cstring>
#include<my_string.h>
using namespace std;

class my_string
 {
 private:
    char *str;
    int len;
 public:
/**********构造函数,类内定义************/
    //无参构造
    my_string()
    {
        this->len=0;
        this->str=NULL;
    }
    //析构函数
    ~my_string()
    {
        delete str;
        str=NULL;
    }
    //有参构造
    my_string(char c,int l)
    {
        this->str = new char[l];
        for(int i=0;i<l;i++)
        {
            str[i]=c;
        }
        str[l]=0;
        this->len=l;
    }

    my_string(char *c)
    {
        this->len=strlen(c);
        this->str=new char[len+1];
        strcpy(str,c);
        cout<<"asd"<<endl;
    }
    //拷贝构造
    my_string(const my_string& other)
    {
        this->len=other.len;
        str = new char[other.len+1];
        for(int i=0;i<other.len+1;i++)
        {
            str[i]=*(other.str+i);
        }
    }

/**********类内声明,类外定义************/

    //拷贝赋值
    my_string & operator=(const my_string & );
    my_string & operator=(char *);
    //判空
    bool my_empty();
    //求长度
    int my_size();
    //转化为c风格字符串
    char *my_str();
    //取单个字符
    char operator[](int i);
/**********使用全局友元函数定义************/
    friend bool operator>(const my_string& ,const my_string & );
    friend bool operator<(const my_string& ,const my_string & );
    friend bool operator==(const my_string& ,const my_string & );
    friend bool operator!=(const my_string& ,const my_string & );
    friend bool operator>=(const my_string& ,const my_string & );
    friend bool operator<=(const my_string& ,const my_string & );
    friend my_string operator+(my_string& L,const my_string & R);

};

#endif // MY_STRING

功能文件

#include<my_string.h>

//拷贝赋值
my_string & my_string::operator=(const my_string & other)
{
    delete this->str;

    this->len=other.len;
    str = new char[other.len+1];
    for(int i=0;i<other.len+1;i++)
    {
        str[i]=*(other.str+i);
    }
    return *this;
}

my_string & my_string::operator=(char *c)
{
    delete this->str;

    this->len=strlen(c);
    this->str=new char[len+1];
    strcpy(str,c);
    return *this;
}

//bool my_empty() 判空
bool my_string::my_empty()
{
    if(len==0)
        return true;
    else
        return false;
}
//int my_size() 求长度
int my_string::my_size()
{
    return len;
}
//char *my_str() 转化为c风格字符串
char *my_string::my_str()
{
    return str;
}
//关系运算符的重载:>  < == >= <= !=
bool operator>(const my_string& L,const my_string & R)
{
    int len1=L.len;
    int len2=R.len;
    int size= len1>len2?len1:len2;
    for(int i=0;i<size;i++)
    {
        if(*(L.str+i)>*(R.str+i))
            return true;
        else if(*(L.str+i)<*(R.str+i))
            return false;
    }
    return false;
}

bool operator<(const my_string& L,const my_string & R)
{
    int len1=L.len;
    int len2=R.len;
    int size= len1>len2?len1:len2;
    for(int i=0;i<size;i++)
    {
        if(*(L.str+i)<*(R.str+i))
            return true;
        else if(*(L.str+i)>*(R.str+i))
            return false;
    }
    return false;
}

bool operator==(const my_string& L,const my_string & R)
{
    int len1=L.len;
    int len2=R.len;
    if(len1 != len2)
        return false;
    int size= len1;
    for(int i=0;i<size;i++)
    {
        if(*(L.str+i)!= *(R.str+i))
            return false;
    }
    return true;
}

bool operator!=(const my_string& L,const my_string & R)
{
    int len1=L.len;
    int len2=R.len;
    if(len1 != len2)
        return true;
    int size= len1;
    for(int i=0;i<size;i++)
    {
        if(*(L.str+i)!= *(R.str+i))
            return true;
    }
    return false;
}

bool operator>=(const my_string& L,const my_string & R)
{
    int len1=L.len;
    int len2=R.len;
    int size= len1>len2?len1:len2;
    for(int i=0;i<size;i++)
    {
        if(*(L.str+i)>*(R.str+i))
            return true;
        else if(*(L.str+i)<*(R.str+i))
            return false;
    }
    return true;
}

bool operator<=(const my_string& L,const my_string & R)
{
    int len1=L.len;
    int len2=R.len;
    int size= len1>len2?len1:len2;
    for(int i=0;i<size;i++)
    {
        if(*(L.str+i)<*(R.str+i))
            return true;
        else if(*(L.str+i)>*(R.str+i))
            return false;
    }
    return true;
}
//strcat   +
my_string operator+(my_string& L,const my_string & R)
{
    int size=L.len+R.len;
    char *p = new char[size+1];
    strcpy(p,L.str);
    strcat(p,R.str);
    delete L.str;
    L.str=p;
    L.len=size;
    return L;
}
//取字符
char my_string::operator[](int i)
{
    if(i>this->len || i<0)
        return 0;
    return*(this->str+i);
}

 主函数

#include <iostream>
#include<cstring>
#include<my_string.h>
using namespace std;




int main()
{
    //定义一个string类
    cout<<"=========string========="<<endl;
    string str(5,'A');
    cout<<str<<endl;
    //定义一个my_string类
    cout<<"=======my_string======="<<endl;
    my_string str1('A',5);
    cout<<str1.my_str()<<endl;
    //拷贝构造
    cout<<"======copy_create======"<<endl;
    my_string str2=str1;
    cout<<str2.my_str()<<endl;
    //拷贝赋值
    cout<<"======copy_assign======"<<endl;
    my_string str3;
    str3=str1;
    cout<<str3.my_str()<<endl;

    //判空
    cout<<"=======is_empty======="<<endl;
    my_string str4;
    str4="hhhh";
    if(str4.my_empty())
        cout<<"str4 empty"<<endl;
    else
        cout<<"str4 not empty"<<endl;
    if(str1.my_empty())
        cout<<"str1 empty"<<endl;
    else
        cout<<"str1 not empty"<<endl;

    //求长度
    cout<<"=======get_length======="<<endl;
    cout<<"str1 length is:"<<str1.my_size()<<endl;
    cout<<"str4 length is:"<<str4.my_size()<<endl;

    //字符串直接赋值
    my_string str5="ccc";
    cout<<str5.my_str()<<endl;
    str5="world";
    cout<<str5.my_str()<<endl;

    //判断大小
    cout<<"=======judge big/small======="<<endl;
    str1="aaa";
    str2="bbb";
    cout<<"aaa>bbb?->"<<(str1>str2?"yes":"no")<<endl;
    cout<<"bbb>aaa?->"<<(str2>str1?"yes":"no")<<endl;
    cout<<"aaa<bbb?->"<<(str1<str2?"yes":"no")<<endl;
    cout<<"bbb<aaa?->"<<(str2<str1?"yes":"no")<<endl;

    str2="aaa";
    cout<<"aaa>aaa?->"<<(str1>str2?"yes":"no")<<endl;
    cout<<"aaa<aaa?->"<<(str1<str2?"yes":"no")<<endl;
    cout<<"aaa==aaa?->"<<(str1==str2?"yes":"no")<<endl;
    cout<<"aaa!=aaa?->"<<(str1!=str2?"yes":"no")<<endl;
    cout<<"aaa>=aaa?->"<<(str1>=str2?"yes":"no")<<endl;
    cout<<"aaa<=aaa?->"<<(str1<=str2?"yes":"no")<<endl;
    str2="aab";
    cout<<"aaa>=aab?->"<<(str1>=str2?"yes":"no")<<endl;
    cout<<"aaa<=aab?->"<<(str1<=str2?"yes":"no")<<endl;
    //加减
    cout<<"==========plus========="<<endl;
    str2=(str1+str5);
    cout<<str2.my_str()<<endl;
    //取字符
    cout<<"==========get_char========="<<endl;
    cout<<str5[2]<<endl;

    return 0;
}

 运行结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林某某..

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

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

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

打赏作者

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

抵扣说明:

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

余额充值