2023.04.21 c++第四讲

在2023.04.20作业基础上,将能进行重载的运算符全部重载

#include <iostream>
#include <cstring>
using namespace std;
//定义类
class myString
{
private:
    char *str;          //记录c风格的字符串
    int size;           //记录字符串的实际长度
public:
    //无参构造
    myString();
    //有参构造
    myString(const char *s);
    //拷贝构造
    myString(const myString &other);
    //析构函数
    ~myString();
    //判空函数
    int myempty(void);
    //size函数
    int mysize();
    //c_str函数
    const char *myc_str();
    //at函数
    char &myat(int pos);
    myString& operator=(const myString &other)
    {
        this->str=other.str;
        this->size=other.size;
        return *this;
    }
    //实现=重载,赋值
    bool operator=(const myString &R)const
    {
        strcpy(this->str,R.str);
        return str;
    }

    //实现>重载,比较字符串大小
    bool operator>(const myString &R)const
    {
        if(this->str > R.str)
            return true;
        else
            return false;
    }
    //实现<重载,比较字符串大小
    bool operator<(const myString &R)const
    {
        if(this->str < R.str)
            return true;
        else
            return false;
    }
    //实现==重载,比较字符串大小
    bool operator==(const myString &R)const
    {
        if(this->str == R.str)
            return true;
        else
            return false;
    }

    //实现+重载,连接
    const myString operator +(const myString &R)
    {
        strcat(this->str,R.str);
        this->size+=R.size;
        return *this;
    }

    //重载[]运算符,访问指定位置的字符
    char &operator [](const int pos)
    {
        return this->str[pos];
    }

    //输出成员
    void show()
    {
        cout << "str = " << str <<"   size = "<<size <<endl;
    }
    friend ostream &operator<<(ostream &L,const myString &O);
    friend istream &operator>>(istream &L,const myString &O);
};
//无参构造
myString::myString():size(10)
{
    str = new char[size];         //构造出一个长度为10的字符串
    strcpy(str,"");               //初始化
    cout << "myString::无参构造" <<endl;
}

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

//拷贝构造
myString::myString(const myString &other)
{
    size = other.size;
    str = new char[size+1];
    strcpy(str,other.str);
    cout << "myString::拷贝构造" <<endl;
}

//析构函数
myString::~myString()
{
    delete []str;
    cout << "myString::析构函数" <<endl;
}
//判空函数
int myString:: myempty(void)
{
    if(0 == *str)
    {
        cout << "empty" <<endl;
        return 0;
    }
    else
    {
        cout << "not empty" <<endl;
        return 1;
    }
}
//size函数
int myString::mysize()
{
    size = 0;
    while(str[size] != 0)
    {
        size++;
    }
    cout << "size = " << size <<endl;
    return size;
}
//c_str函数
const char *myString::myc_str()
{
    cout << "" << str <<endl;
    return str;
}
//at函数
char &myString::myat(int pos)
{

    if(pos >= size || pos < 0)
    {
        cout<<"段错误"<<endl;
    }

    return str[pos];
}

//全局函数版实现<<运算符,输出所有内容
ostream &operator<<(ostream &L,const myString &O)
{

    L<<O.str<<"  "<<O.size;
    return L;
}
//全局函数版实现>>运算符
istream &operator>>(istream &L,const myString &O)
{

    L>>O.str;
    return L;
}
int main()
{
    myString s1;                  //无参构造
    s1.show();                    //str=   size=10

    myString s2("WYJie");         //有参构造
    s2.show();                    //str=WYJie   size=5

    myString s3(s2);              //拷贝构造
    s3.show();                    //str=WYJie   size=5

    //判空
    s1.myempty();                 //empty
    s2.myempty();                 //not empty

    //计算长度
    s2.mysize();                  //size=5

    //将c++风格的字符转换为c风格的字符
    s2.myc_str();                 //WYJie

    //查看相应下标上的字符
    char c = s2.myat(2);
    cout << "字符为: " << c <<endl;//J

    //=
    myString s4 = s2;
    s4.show();                    //str=WYJie   size=5

    //+
    myString s5 =s2 + s3;
    s5.show();                    //str=WYJieWYJie   size=10

    //>
    if(s4 > s5)
    {
        cout << "s4 > s5" <<endl;//s4 < s5
    }
    else if(s4 < s5)
    {
        cout << "s4 < s5" <<endl;
    }
    else if(s4 == s5)
        cout << "s4 == s5" <<endl;

    //<< >>
    myString s6;
    cout<<"s6 = ";
    cin >> s6;                   //wwwyyyjjj
    cout << s6 <<endl;           //wwwyyyjjj   10

    //[]
    s6[3] = 'Q';
    s6.show();                   //str=wwwQyyjjj  size=10

    return 0;
}

思维导图:

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值