您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
第26讲-运算符重载2
发布时间:2021-05-18 22:22:54编辑:雪饮阅读()
在上篇中有提到过运算符的重载,其中只介绍了一个加法的重载,并且在在类的成员函数中实现。
那么不仅仅只有加法可以做运算符的重载,关于某个运算符是否可以被重载。
可重载运算符/不可重载运算符
下面是可重载的运算符列表:
双目算术运算符 + (加),-(减),*(乘),/(除),% (取模)
关系运算符 ==(等于),!= (不等于),< (小于),> (大于),<=(小于等于),>=(大于等于)
逻辑运算符 ||(逻辑或),&&(逻辑与),!(逻辑非)
单目运算符 + (正),-(负),*(指针),&(取地址)
自增自减运算符 ++(自增),--(自减)
位运算符 | (按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移)
赋值运算符 =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
空间申请与释放 new, delete, new[ ] , delete[]
其他运算符 ()(函数调用),->(成员访问),,(逗号),[](下标)
下面是不可重载的运算符列表:
.:成员访问运算符
.*, ->*:成员指针访问运算符
:::域运算符
sizeof:长度运算符
?::条件运算符
#: 预处理符号
减法运算符重载
那么这里再介绍下减法运算符的重载
#include <iostream>
using namespace std;
class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
double getLength(void)
{
return length;
}
void setBreadth( double bre )
{
breadth = bre;
}
void setHeight( double hei )
{
height = hei;
}
// 重载 + 运算符,用于把两个 Box 对象相加
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
//减法运算符符的重载
Box operator-(const Box& b)
{
Box box;
box.length = this->length - b.length;
box.breadth = this->breadth - b.breadth;
box.height = this->height -b.height;
return box;
}
private:
double length; // 长度
double breadth; // 宽度
double height; // 高度
};
// 程序的主函数
int main( )
{
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
Box Box3; // 声明 Box3,类型为 Box
Box Box4;
double volume = 0.0; // 把体积存储在该变量中
// Box1 详述
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// Box2 详述
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// Box1 的体积
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// Box2 的体积
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// 把两个对象相加,得到 Box3
Box3 = Box1 + Box2;
// Box3 的体积
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
Box4=Box3-Box2;
volume = Box4.getVolume();
cout << "Volume of Box4 : " << volume <<endl;
return 0;
}
编译并运行结果如:
非成员函数方式进行运算符重载
非成员函数方式进行运算符重载就比较麻烦复杂了些,不过说麻烦也只是多了一个友元罢了。
具体的实例如:
#include<iostream>
using namespace std;
class Complex{
public:
//一种带传参构造的方式,可并不只是空构造
Complex(double r=0.0,double i=0.0):real(r),imag(i){
}
//非成员函数方式的运算符重载需要在被重载类中增加友元定义,定义参与运算的类型
friend Complex operator + (const Complex &c1,const Complex &c2);
friend Complex operator - (const Complex&c1,const Complex &c2);
friend ostream & operator <<(ostream & out,const Complex & c);
private:
double real,imag;
};
//下面几个运算符重载都是非成员函数重载的实现方式
//重定义加法,合并两个Complex实例的real、imag为新的Complex实例的real、imag
Complex operator + (const Complex &c1,const Complex &c2)
{
return Complex(c1.real+c2.real,c1.imag+c2.imag);
}
//重定义减法
Complex operator - (const Complex &c1,const Complex &c2)
{
return Complex(c1.real-c2.real,c1.imag-c2.imag);
}
/*
ostream为输出流
&在这里为引用
"ostream&"=="ostream &"引用型输出流
流插入运算符“<<”重定义,这里的 "& out"相当于“&out”,第二个参数同理
总结:
当引用型类型只定义并实例化(不定义左值/变量名)则类型名和"&"可以靠紧在一起,也可以中间有空格
如果还同时定义变量,则&符号可以和类型名紧靠,也可以和变量名紧靠,甚至于可以这3个都紧靠在一起(刚用code blocks 20.03测试)
*/
ostream& operator <<(ostream&out,const Complex & c)
{
out<<"("<<c.real<<","<<c.imag<<")";
return out;
}
int main()
{
//加法、减法
Complex c1(5,4),c2(2,10),c3;
cout<<"c1="<<c1<<endl;
cout<<"c2="<<c2<<endl;
c3=c1-c2;
cout<<"c3=c1-c2="<<c3<<endl;
c3=c1+c2;
cout<<"c3=c1+c2="<<c3<<endl;
return 0;
}
则编译运行结果如:
c1=(5,4)
c2=(2,10)
c3=c1-c2=(3,-6)
c3=c1+c2=(7,14)
Process returned 0 (0x0) execution time : 0.027 s
Press any key to continue.
关键字词:运算符,运算符重载,非成员函数运算符重载
上一篇:第25讲-运算符重载
下一篇:第27讲-重载《操作符