运算符重载(c++)

我们都知道,运算符分为算术运算符,逻辑运算符,位操作运算符,关系运算符,赋值运算符,指针运算符…
1. 比如对于算术运算符(+,-,*,/,++,–,%)它们运算的对象都是基本数据类型,比如可以是int,double,short,等基本类型的运算,除此之外,任何其他的运算都会报错,比如结构体和结构体运算,类和类运算,这就不符合算术运算符的基本运算法则,但有时候我们又需要这样子的运算,怎么办呢,所以c/c++语言就推出了一种操作:运算符重载。
2. 运算符重载
①定义:改变原运算符的运算对象和运算规则(自定义)
②写法:返回类型 + operator + 运算符 + (参数…){…}
③意义:应该是更加便于达到要求,更贴和思想(乱写的hhhh)
3. 开启运算符重载之旅
①重载:+ 号运算符

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<time.h>
using namespace std;
class Vector  //创建一个Vector(向量)类
{
    public: // public 代表一下属性或者函数是类的公开成员
        int x,y,z;
        //  重载 + 号运算符 ,-号类似,实现两个Vector相减
        Vector operator +(const Vector &v)
        {
            Vector ve;
            ve.x = this->x + v.x;
            ve.y= this->y + v.y;
            ve.z = this->z + v.z;
            return ve;
        }
        Vector(){}  //类的无参构造函数
        ~Vector(){}  //类的析构函数
        Vector(int x,int y,int z) //类的有参构造
        {
            this->x = x;
            this->y = y;
            this->z = z;
        }
};
int main(void)
{

    Vector a(1,1,1); //调用Vector的有参构造函数
    Vector b; //调用Vector的无参构造函数
    b = a + Vector(2,2,2);//后面的Vector(2,2,2)是匿名构造类
    cout<<b.x<<" "<<b.y<<" "<<b.z<<endl;
    return 0;
}

②重载:* 号运算符

using namespace std;
class Vector  //创建一个Vector(向量)类
{
    public: // public 代表一下属性或者函数是类的公开成员
        int x,y,z;
        //重载运算符:返回值+operator+运算符+(参数){.....}
        //  重载 * 号运算符,/号类似,实现两个Vector相乘
        int operator *(const Vector &v)
        {
            return this->x*v.x+this->y*v.y+this->z*v.z;
        }
     
        Vector(){}  //类的无参构造函数
        Vector(int x,int y,int z) //类的有参构造
        {
            this->x = x;
            this->y = y;
            this->z = z;
        }
};

int main(void)
{

    Vector a(1,1,1); //调用Vector的有参构造函数
    int ans = a * Vector(2,2,2);//后面的Vector(2,2,2)是匿名类
    cout<<ans<<endl;
    return 0;
}

③重载:前缀++ 号运算符

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<time.h>
using namespace std;
class Vector  //创建一个Vector(向量)类
{
    public: // public 代表一下属性或者函数是类的公开成员
        int x,y,z;
        //重载运算符:返回值+operator+运算符+(参数){.....}
        // 4.前缀重载 a++运算符,a--类似
        bool operator ++()
        {
            this->x++;
            this->y++;
            this->z++;
            return true;
        }
        Vector(){}  //类的无参构造函数
        Vector(int x,int y,int z) //类的有参构造
        {
            this->x = x;
            this->y = y;
            this->z = z;
        }
};

int main(void)
{

    Vector a(1,1,1); //调用Vector的有参构造函数
    ++a;
	cout<<a.x<<" "<<a.y<<" "<<a.z<<endl;
    return 0;
}

④重载:后缀++ 号运算符

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<time.h>
using namespace std;
class Vector  //创建一个Vector(向量)类
{
    public: // public 代表一下属性或者函数是类的公开成员
        int x,y,z;
        //重载运算符:返回值+operator+运算符+(参数){.....}
        // 后缀重载 a++运算符,a--类似
        bool operator ++(int)//int是占位符,没什么用,只是区分前缀和后缀两次不同的重载而已
        {
            return operator++();
        }

        // 前缀重载 a++运算符,a--类似
        bool operator ++()
        {
            this->x++;
            this->y++;
            this->z++;
            return true;
        }

        Vector(){}  //类的无参构造函数
        Vector(int x,int y,int z) //类的有参构造
        {
            this->x = x;
            this->y = y;
            this->z = z;
        }
};


int main(void)
{

    Vector a(1,1,1); //调用Vector的有参构造函数
    a++;
    cout<<a.x<<" "<<a.y<<" "<<a.z<<endl;
    return 0;
}

⑤重载:左移<< 号运算符

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<time.h>
using namespace std;
class Vector  //创建一个Vector(向量)类
{
    public: // public 代表一下属性或者函数是类的公开成员
        int x,y,z;
        //重载运算符:返回值+operator+运算符+(参数){.....}
        Vector(){}  //类的无参构造函数
        Vector(int x,int y,int z) //类的有参构造
        {
            this->x = x;
            this->y = y;
            this->z = z;
        }
};
//  重载左移运算符,也就是输出流符号
ostream &operator<<(ostream &cout,const Vector&v)
{
    cout<<v.x<<" "<<v.y<<" "<<v.z;
    return cout;
}

int main(void)
{

    Vector a(1,1,1); //调用Vector的有参构造函数
    cout<<"向量a为:"<<a<<endl;
    return 0;
}

完整代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<time.h>
using namespace std;
class Vector  //创建一个Vector(向量)类
{
    public: // public 代表一下属性或者函数是类的公开成员
        int x,y,z;
        //重载运算符:返回值+operator+运算符+(参数){.....}
        // 1. 重载 * 号运算符,/号类似,实现两个Vector相乘
        int operator *(const Vector &v)
        {
            return this->x*v.x+this->y*v.y+this->z*v.z;
        }
        // 2. 重载 + 号运算符 ,-号类似,实现两个Vector相减
        Vector operator +(const Vector &v)
        {
            Vector ve;
            ve.x = this->x + v.x;
            ve.y= this->y + v.y;
            ve.z = this->z + v.z;
            return ve;
        }
        // 3.后缀重载 a++运算符,a--类似
        bool operator ++(int)//int是占位符,没什么用,只是区分前缀和后缀两次不同的重载而已
        {
            return operator++();
        }

        // 4.前缀重载 a++运算符,a--类似
        bool operator ++()
        {
            this->x++;
            this->y++;
            this->z++;
            return true;
        }

        Vector(){}  //类的无参构造函数
        Vector(int x,int y,int z) //类的有参构造
        {
            this->x = x;
            this->y = y;
            this->z = z;
        }
};
//  5.重载左移运算符,也就是输出流符号
ostream &operator<<(ostream &cout,const Vector&v)
{
    cout<<v.x<<" "<<v.y<<" "<<v.z;
    return cout;
}

int main(void)
{

    Vector a(1,1,1); //调用Vector的有参构造函数
    ++a;
    cout<<"向量a为:"<<a<<endl;
    a++;
    cout<<"向量a为:"<<a<<endl;
    Vector b; //调用Vector的无参构造函数
    b = a + Vector(2,2,2);//后面的Vector(2,2,2)是匿名构造类
    cout<<"向量a+一个向量的和向量b为:"<<b<<endl;
    int ans = a * Vector(2,2,2);//后面的Vector(2,2,2)是匿名构造类
    cout<<"向量a+一个向量的乘积为:"<<ans<<endl;
    return 0;
}

其他运算符怎么重载大家琢磨去吧O(∩_∩)O哈哈~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

凌晨小街

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

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

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

打赏作者

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

抵扣说明:

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

余额充值