运算符号重载实际上是通过函数实现,通过创建 operator 函数就可以实现重
载运算符
运算符重载语法:
返回值类型 类名::operator+(参数) //返回值类型一般是这个类的名字 +是重载的运算符号
{
//具体代码
}
复数加法示例代码:
#include <iostream>//实现复数的加法
class Complex
{
public:
Complex(); //构造函数
Complex(double r, double i); //构造函数 重构
Complex operator+(Complex &d); //方法
void printf();
private:
double real; //属性
double imag;
};
Complex::Complex()
{
/*
real = 0;
imag = 0;
*/
}
Complex::Complex(double r, double i)
{
real = r;
imag = i;
}
Complex Complex::operator+(Complex &d)
{
Complex c;
c.real = real + d.real;
c.imag = imag + d.imag;
return c;
}
void Complex::printf()
{
std::cout << "(" << real << "," << imag << "i)\n";
}
int main()
{
Complex c1(3, 4), c2(5, -10), c3;
c3 = c1 + c2;
std::cout << "c1 = ";
c1.printf();
std::cout << "c2 = ";
c2.printf();
std::cout << "c1 + c2 = ";
c3.printf();
return 0;
}
运行结果:
例子中的c1+c2,编译系统把他解释为c1.operator+(c2)
即通过对象c1调用运算符重载函数,并以表达式中第二个参数(运算符右侧的类对象c2)z作为函数的实参
分数加减乘除重载:
#include<iostream>//运算符重载2 课后作业
#include<string>
#include<stdlib.h>
class Rational
{
public:
Rational(int num, int denom);//num=分子 ,denom=分母
Rational operator+(Rational rhs);
Rational operator-(Rational rhs);
Rational operator*(Rational rhs);
Rational operator/(Rational rhs);
void printf();
private:
void normalize();//负责对分数的简化处理
int numerator; //分子
int denominator;//分母
};
Rational::Rational(int num, int denom)
{
numerator = num;
denominator = denom;
normalize();
}
//normalize()对分数进行简化操作包括:
//1、只允许分子为负数,如果分母为负数则把负数挪到分子部分,如1/-2 == -1/2
//2、利用欧几里德算法(辗转求余原理)将分数进行简化:2/10 =》1/5
void Rational::normalize()
{
//确保分母为正
if (denominator < 0)
{
numerator = -numerator;
denominator = -denominator;
}
//欧几里德算法
int a = abs(numerator);
int b = abs(denominator);
//求出最大公约数
while (b > 0)
{
int t = a%b;
a = b;
b = t;
}
//分子、分母分别➗最大公约数得到最简化分数
numerator /= a;
denominator /= a;
}
// a c a*d c*b a*d + c*b
// - + - = --- + --- = ----------
// b d b*d b*d b*d
Rational Rational::operator+(Rational rhs)
{
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
int e = a*b + c*d;
int f = b*d;
return Rational(e,f);
}
// a c a -c
// - - - = - + ---
// b d b d
Rational Rational::operator-(Rational rhs)
{
rhs.numerator = -rhs.numerator;
return operator+(rhs);
}
// a c a*d
// - * - = ---
// b d b*d
Rational Rational::operator*(Rational rhs)
{
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
int e = a*c;
int f = b*d;
return Rational(e,f);
}
// a c a d
// - / - = - * -
// b d b c
Rational Rational::operator/(Rational rhs)
{
int t = rhs.numerator;
rhs.numerator = rhs.denominator;
rhs.denominator = t;
return operator*(rhs);
}
void Rational::printf()
{
if (numerator%denominator == 0)
std::cout << numerator / denominator;
else
std::cout << numerator << "/" << denominator;
}
int main()
{
Rational f1(2, 16);
Rational f2(7, 8);
//测试有理数加法
Rational res = f1 + f2;
f1.printf();
std::cout << "+";
f2.printf();
std::cout << "=";
res.printf();
std::cout << "\n";
//测试有理数减法
res = f1 - f2;
f1.printf();
std::cout << '-';
f2.printf();
std::cout << '=';
res.printf();
std::cout << "\n";
//测试有理数乘法
res = f1 * f2;
f1.printf();
std::cout << "*";
f2.printf();
std::cout << "=";
res.printf();
std::cout << "\n";
//测试有理数除法
res = f1 / f2;
f1.printf();
std::cout << '/';
f2.printf();
std::cout << '=';
res.printf();
std::cout << "\n";
return 0;
}
运行结果:
重载需要注意的一些规则:
- 重载不能改变运算符运算对象(操作数)个数
- 重载不能改变运算符的优先级别
- 重载不能改变运算符的结合性
- 重载运算符的函数不能又默认参数
- 重载的运算符必须和用户定义的自定义类型的对象一起使用,其参数至少应该有一个应该是类对象或者类对象的引用,(也就是说,参数不能全部都是C++的标准类型,这样约定是为了防止用户修改用于标准类型结构的运算符性质)
- C++不允许用户定义新的运算符,只能对已有的C++运算符进行重载
- 以下5个运算符不允许重载
. 成员访问运算符 .* 成员指针访问运算符 :: 域运算符 sizeof 尺寸运算符 ?: 条件运算符
详细参考小甲鱼视频[25]运算符重载_哔哩哔哩_bilibili