目录
运算符重载一般格式:
@表示要重载的运算符;若是重载"="则@用“=”替换;若是重载"+"则@用“+”替换;
参数表根据运算符的操作数个数决定;单目or双目;
c++中可重载运算符如下:
c++中有下列5个运算符不可重载:
重载运算符使用规则:
运算符重载两种形式:
成员运算符函数;友元运算符函数;
对于同一运算符,要么定义为成员,要么定义为友元;不可二者都定义;
运算符重载为友元函数:
友元才能访问类的私有属性;
案例1:友元方式重载运算符“+”、“==”
#include<iostream>
using namespace std;
class Add {
double real, image;
public:
Add(double r = 0, double i = 0)
{
real = r;
image = i;
}
void show()
{
if (image > 0)
{
if (image == 1)
cout << real << "+" << "i" << endl; //虚部系数为1,如:3+i
else
{
cout << real << "+" << image << "i" << endl; //虚部系数为其它正数,如:3+5i
}
}
else if (image < 0) {
if (image == -1)
cout << real << "-" << "i" << endl; //虚部系数为-1情况;如:2-i
else
{
cout << real << "-" << image << "i" << endl; //虚部系数为其它负数,如:3-5i
}
}
else
{
cout << real << endl;
}
}
friend Add operator + (const Add& c1, const Add& c2); //类中声明是友元,否则重载运算符的函数不可访问类私有属性
friend bool operator ==(const Add& c1, const Add& c2);
};
//类外重载运算符"+"
Add operator + (const Add& c1, const Add& c2) //引用方式传参,保证传参不变加const,返回值数据类型Add
{
Add temp;
temp.real = c1.real + c2.real;
temp.image = c1.image + c2.image;
return temp;
}
//类外重载运算符"=="
bool operator ==(const Add& c1, const Add& c2) {
if (c1.real == c2.real && c1.image == c2.image)
return true;
else
return false;
}
int main()
{
Add a1(10, 5), a2(2, -5);
a1.show();
a2.show();
Add a3 = a1 + a2; //隐式调用,两个自定义数据类型相加
// Add a3=operator+(a1,a2); //显式调用
a3.show();
if(a1==a2)
cout << "两个复数相等" << endl;
else
{
cout << "两个复数不相等" << endl;
}
return 0;
}
输出结果:
案例2:定义点类(Point),用以表示几何学点的概念,有属性×、y表示坐标,并重载“-”单目运算符和“=="双目运算符,要求“-”实现对象的成员变量的数值符号取反,而“==”实现判断两个Point类的对象坐标是否相同,采用友元函数的方法实现
例如:
Pointp1(3,4):
Pointp2=-p1;则p2为(-3,-4)
if(p1==p2)表示判断p1和p2坐标是否一样;
#include<iostream>
using namespace std;
class point {
int x, y;
public:
point(int xi=0,int yi=0) {
x = xi;
y = yi;
}
friend bool operator ==(const point& a, const point& b);
friend point operator -(const point& c);
void show() {
cout << "横坐标为:" << x << " 纵坐标为:" << y << endl;
}
};
bool operator ==(const point&a,const point&b) {
if (a.x == b.x && a.y == b.y)
return true;
else
return false;
}
point operator -(const point& c) {
point temp;
temp.x = -(c.x);
temp.y = -(c.y);
return temp;
}
int main() {
point a(3, 4);
point b(3, 5);
if (a == b)
cout << "a 与 b坐标相同" << endl;
else
cout << "a 与 b坐标不相同" << endl;
cout << "a坐标如下:" << endl;
a.show();
cout << "执行运算a=-b后,a坐标如下:" << endl;
a = -b;
a.show();
return 0;
}
输出结果:
运算符重载为成员函数:
如果是重载双目运算符,就只要设置一个参数作为右侧运算量,而左侧运算量就是该对象本身;
如果是重载单目运算符,就不必另外设置参数,运算符的操作量就是对象本身。
运算符重载为成员函数与重载为友元函数区别:成元函数方式参数个数少一个(即对象本身);
案例1:定义复数类(Add),采用成员函数实现运算符重载+和==
#include<iostream>
using namespace std;
class Add {
double real, image;
public:
Add(double r = 0, double i = 0)
{
real = r;
image = i;
}
void show()
{
if (image > 0)
{
if (image == 1)
cout << real << "+" << "i" << endl; //虚部系数为1,如:3+i
else
{
cout << real << "+" << image << "i" << endl; //虚部系数为其它正数,如:3+5i
}
}
else if (image < 0) {
if (image == -1)
cout << real << "-" << "i" << endl; //虚部系数为-1情况;如:2-i
else
{
cout << real << "-" << image << "i" << endl; //虚部系数为其它负数,如:3-5i
}
}
else
{
cout << real << endl;
}
}
Add operator + (const Add& c)
{
Add temp;
temp.real = this->real + c.real;
temp.image = this->image + c.image;
return temp;
}
bool operator ==(const Add& c) {
if (this->real == c.real && this->image == c.image)
return true;
else
return false;
}
};
int main()
{
Add a1(10, 5), a2(2, -3);
a1.show();
a2.show();
Add a3 = a1 + a2; //隐式调用
// Add a3=a1.operator+(a2); //显式调用
a3.show();
if (a1 == a2)
cout << "两个复数相等" << endl;
else
{
cout << "两个复数不相等" << endl;
}
return 0;
}
输出结果: