我们都知道,运算符分为算术运算符,逻辑运算符,位操作运算符,关系运算符,赋值运算符,指针运算符…
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哈哈~