C++DAY3

文章展示了如何在C++中创建一个名为myString的自定义字符串类,包括默认构造函数、带有参数的构造函数、拷贝构造函数、赋值运算符重载以及加法赋值运算符重载。此外,还实现了比较运算符和一些辅助方法如显示字符串、判断是否为空及获取长度。在main函数中,这些功能得以应用和展示。
摘要由CSDN通过智能技术生成

 

#include"head.h"
#include <iostream>

#include <string.h>

#include<string>

using namespace std;



class myString
{
private:
    char *str;
    int  size;
public:
    myString():size(30)
    {
        str = new char [size];
        strcpy(str,"");
        cout << this->str<<endl;
    }

    myString(const char *str)
    {
        size = strlen(str);
        this->str = new char [size+1];
        strcpy(this->str , str);
        cout << this->str<<endl;
    }

    myString(const myString &other):str(new char[other.size+1]),size(other.size)
    {

        int i = 0 ;

        while (*(other.str+i) != '\0' )
        {
            this->str[i] = *(other.str+i);
            i++;
        }
        cout << this->str<<endl;
    }



    myString & operator = (const myString & other)
    {
        if (this != &other)
        {
            strcpy(this->str,other.str);
            this->size = other.size;
        }
        else
        {
        }
        cout<<"拷贝赋值"<<endl;
        cout<<this->str<<endl;
        return *this;
    }

    myString &operator +=(const myString &other)
    {
        strcat(this->str,other.str);
        this->size += other.size;
        cout<<"+=重载"<<endl;
        cout<<"size:"<<this->size<<endl;
        cout<<"str:"<<this->str<<endl;
        return *this;
    }

    bool operator == (const myString &other)const
    {

        return (!strcmp(this->str,other.str));
    }
    bool operator != (const myString &other)const
    {

        return strcmp(this->str,other.str);
    }

    bool operator < (const myString &other)const
    {
        return strcmp(this->str,other.str)<0? 1:0;
    }
    bool operator > (const myString &other)const
    {
        return strcmp(this->str,other.str)<0? 0:1;
    }
    bool operator <= (const myString &other)const
    {
        return strcmp(this->str,other.str)<=0? 1:0;
    }
    bool operator >= (const myString &other)const
    {
        return strcmp(this->str,other.str)<=0? 0:1;
    }


    ~myString()
    {
        delete []this->str;
        cout << "析构成功" << endl;

    }

    void judge_empty()
    {
        if(!*(this->str))
        {
            cout << "空" <<endl;
        }
        else
        {
            cout << "非空" <<endl;
        }
    }

    int len_size ()
    {
        return this->size;
    }

    const char *c_str()
    {
        return this->str;
    }

    char at (int i)
    {
        return  *(str+i);
    }



    void show()
    {
        cout<<this->str<<endl;
    }

    friend ostream &operator << ( ostream &L,const myString &R);
    friend istream &operator >> ( istream &L,const myString &R);
};


    ostream &operator << ( ostream &L,const myString &R)
{
    L << R.str ;

    return L;
}

    istream &operator >> (istream &L , const myString &R)
{
    L >> R.str ;

    return L;
}
int main()
{
    myString s1("hello");
    myString s2(s1);
    cout <<"s1 size:"<<s1.len_size()<<endl;
    cout<<"第1个字符为"<<s1.at(0)<<endl;
    cout<<"第2个字符为"<<s1.at(1)<<endl;
    cout<<"第3个字符为"<<s1.at(2)<<endl;
    cout << "s1";
    s1.judge_empty();
    cout<<"********************************************"<<endl;

    myString s3 ;
    s3 = s1 ;
    s3 += s1;
    s3.show();
    s2.show();
    s1.show();
    if(s3 == s1)
    {
        cout<<"相等"<<endl;
    }
    else
    {
        cout<<"不相等"<<endl;
    }
     cout<<"********************************************"<<endl;
    if((s3 != s1) == 0)
    {
        cout<<"相等"<<endl;
    }
    else
    {
        cout<<"不相等"<<endl;
    }
    if(s3 < s1)
    {
        cout<<"s3小"<<endl;
    }
    else
    {
        cout<<"s3大"<<endl;
    }

    if(s3 > s1)
    {
        cout<<"s3大"<<endl;
    }
    else
    {
        cout<<"s3小"<<endl;
    }
    if(s3>=s1)
    {
        cout<<"s3>=s1"<<endl;
    }
    else
    {
        cout<<"s3<s1"<<endl;
    }

    if(s3<=s1)
    {
        cout<<"s3<=s1"<<endl;
    }
    else
    {
        cout<<"s3>s1"<<endl;
    }

    cout<<s3<<endl;
    cin>> s2;
    s2.show();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值