hwk4:C++ 运算符重载

用mystring实在部分C++运算符重载

mystring.h:

#ifndef MYSTRING_H
#define MYSTRING_H

#include <iostream>
#include <cstring>

using namespace std;

class myString
{
private:
    char *str;          //记录c风格的字符串
    int size;            //记录字符串的实际长度
public:
    //无参构造
    myString():size(10)
    {
        str = new char[size];         //构造出一个长度为10的字符串
        strcpy(str,"");         //赋值为空串
        cout<<"无参构造"<<endl;
    }
    //有参构造
    myString(const char *s)          //string  s("hello world")
    {
        size = strlen(s);
        str = new char[size+1];
        strcpy(str, s);
        cout<<"有参构造"<<endl;
    }

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

    //析构函数
    ~myString()
    {
        delete []str;
        cout<<"析构函数"<<endl;
    }
    //拷贝赋值函数
    myString &operator=(const myString &other);
    //判空函数
    bool string_empty()const;
    //size函数
    int string_size()const;
    //c_str函数
    const char* c_str()const;
    //at函数
    char &at(int pos);
    //加号运算符重载
    myString operator+(const myString &other)const;
    //加等于运算符重载
    myString operator+=(const myString &other);
    //关系运算符重载(>)
    bool operator>(const myString &other)const;
    //中括号运算符重载
    char operator[](int index)const;
};

#endif // MYSTRING_H

mystring.cpp:

#include "mystring.h"

//拷贝赋值函数
myString &myString::operator=(const myString &other)
{
    if(this!=&other)
    {
        if(str!=NULL)
        {
            delete []str;
        }
        str=new char[other.size];
        strcpy(str,other.str);
        this->size=other.size;
    }
    return *this;
}
//判空函数
bool myString::string_empty()const
{
    return strcmp(str,"")==0;
}
//size函数
int myString::string_size()const
{
    return size;
}
//c_str函数
const char* myString::c_str()const
{
    return str;
}
//at函数
char &myString::at(int pos)
{
    while(1)
    {
        if(pos<0||pos>=size)
        {
            cout<<"输入格式不对,范围为0~"<<size<<endl;
        }else{
            break;
        }
    }
    return *(str+pos);
}
//加号运算符重载
myString myString::operator+(const myString &other)const
{
    myString a=*this;
    char buf[a.size];
    strcpy(buf,a.str);
    if(a.str!=NULL)
    {
        delete []a.str;
    }
    a.size=a.size+other.size;
    a.str=new char[a.size+1];
    strcpy(a.str,buf);
    strcat(a.str,other.str);

    return a;
}
//加等于运算符重载
myString myString::operator+=(const myString &other)
{
    char buf[size];
    strcpy(buf,str);
    if(str!=NULL)
    {
        delete []str;
    }
    size=size+other.size;
    str=new char[size];
    strcpy(str,buf);
    strcat(str,other.str);

    return *this;
}
//关系运算符重载(>)
bool myString::operator>(const myString &other)const
{
    return strcmp(str,other.str)>0;
}
//中括号运算符重载
char myString::operator[](int index)const
{
    while(1)
    {
        if(index<0||index>=size)
        {
            cout<<"输入格式不对,范围为0~"<<size<<endl;
        }else{
            break;
        }
    }
    return *(str+index);
}

main.cpp:

#include "mystring.h"

int main()
{
    myString a("hello world");
    cout<<"字符串a为 "<<a.c_str()<<endl;
    cout<<"字符串大小为"<<a.string_size()<<endl;
    a.at(0)='H';
    cout<<"字符串a为 "<<a.c_str()<<endl;
    myString b;
    cout<<"字符串b为空?"<<b.string_empty()<<endl;
    b=a;
    cout<<"字符串b为 "<<b.c_str()<<endl;
    myString c=a+b;
    cout<<"字符串c为 "<<c.c_str()<<endl;
    myString d("hahaha");
    d+=a;
    cout<<"字符串d为 "<<d.c_str()<<endl;
    cout<<"字符串d比a大? "<<d.operator>(a)<<endl;
    return 0;
}

算术类运算符重载

1> 种类:+、-、*、/、%

2> 表达式格式:L # R //L表示左操作数 #表示运算符 R表示右操作数

3> 左操作数:既可以是左值也可以是右值,运算过程中不会被修改

4> 右操作数:既可以是左值也可以是右值,运算过程中不会被修改

5> 结果:结果是一个同类的右值,不能被改变

6> 定义格式:

全局函数版:const 类名 operator# (const 类名 &L, const 类名 &R)

成员函数版:const 类名 operator# ( const 类名 &R)const

关系类运算符重载

1> 种类:>、=、

2> 表达式格式:L # R //L表示左操作数 #表示运算符 R表示右操作数

3> 左操作数:既可以是左值也可以是右值,运算过程中不会被修改

4> 右操作数:既可以是左值也可以是右值,运算过程中不会被修改

5> 结果:bool类型,表示真假

6> 定义格式:

全局函数版:bool operator# (const 类名 &L, const 类名 &R)

成员函数版:bool operator# ( const 类名 &R)const

赋值类运算符重载

1> 种类:=、+=、-=、*=、/=、%=

2> 表达式格式:L # R //L表示左操作数 #表示运算符 R表示右操作数

3> 左操作数:只能是左值

4> 右操作数:既可以是左值也可以是右值,运算过程中不会被修改

5> 结果:自身的引用

6> 定义格式:

全局函数版:类名 &operator# (类名 &L, const 类名 &R)

成员函数版:类名 & operator# ( const 类名 &R)

单目运算符重载

1> 种类:!、~、*、&、-

2> 表达式格式: # O // #表示运算符 O表示操作数

3> 操作数:既可以是左值也可以是右值,运算过程中不能更改

4> 结果:同类的右值

5> 定义格式:

全局函数版:类名 operator# (const 类名 &O)

成员函数版:类名 operator# ( )const

自增自减运算符

1> 前置自增运算

1、表达式格式:++O

2、操作数:只能是左值

3、结果:更改后自身的引用,是一个左值

4、定义格式

全局函数版:类名& operator++(类名 & other)

成员函数版:类名 & operator++()

2> 后置自增

1、表达式格式:O++

2、操作数:只能是左值

3、结果:临时值,是一个右值,更改之前的值

4、定义格式:为了区分跟前置自增的区别,后置自增多一个哑元进行占位

全局函数版:类名 operator++(类名 & other, int)

成员函数版:类名 operator++(int)

插入提取运算符的重载(>)

1> 这两个运算符的使用:cout>对象名 //cout.operator

2> cin和cout所在的类分别为istream和ostream类,如果想要实现成员函数版的运算符重载,需要对这两个类重新更改内容

3> 对于这两个运算符重载,只能使用全局函数版,借助友元的威力来实现

4> 由于不需要访问istream或ostream类中的私有成员或受保护成员,所以无需将该全局函数在istream和ostream类中设置成友元函数

5> 由于该函数要访问自定义类的私有成员,所以只需在自定义类中将该全局函数设置成友元函数即可

6> 表达式:L#R //L表示cin或cout #表示>运算符 R表示自定义的类对象

7> 左操作数:istream或ostream类对象

8> 右操作数:自定义的类对象

9> 结果:左操作数自身的引用

10> 定义格式

ostream &operator

istream &operator>>(istream &L, 类名 &R);

不能重载的运算符

1> 成员运算符 .

2> 成员指针运算符 .*

3> 作用域限定符 ::

4> 求字节运算符 sizeof

5> 三目运算符 ?:

运算符重载注意事项

1> 运算符重载,不能改变运算符的优先级

2> 运算符重载,不能改变运算符操作数个数

3> 运算符重载,不能更改运算符的本质逻辑,例如不允许在加法运算符重载函数中,实现加法

4> 运算符重载,不能自己造运算符,是在已有的运算符的基础上进行重载操作

5> 运算符重载,不能改变运算符的结合律

6> 运算符重载,一般不设置默认参数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值