一、运算符重载
1.C++中可以重载的运算符::
2.C++中不可以重载的运算符:
3.C++中只能进行类内重载的运算符:
=、()、[]、->
自增自减运算符(++、–)、输入输出(>>、<<)、四则运算符(+、-、、/、+=、-=、=、/=)和关系运算符(>、<、<=、>=、==、!=)都是数学运算符,它们在实际开发中非常常见,被重载的几率也很高。本文以复数类 CComplex 为例对它们进行类外重载,演示运算符重载的语法以及规范。代码如下所示:
#include <iostream>
using namespace std;
class CComplex
{
double m_fReal;
double m_fImag;
public:
CComplex()
{
m_fReal = m_fImag = 0;
}
CComplex(double d1, double d2)
{
this->m_fReal = d1;
this->m_fImag = d2;
}
//声明友元函数
friend CComplex operator+(const CComplex& c1, const CComplex& c2);
friend CComplex operator-(const CComplex& c1, const CComplex& c2);
friend CComplex operator*(const CComplex& c1, const CComplex& c2);
friend CComplex operator/(const CComplex& c1, const CComplex& c2);
friend CComplex& operator++(CComplex &c1);
friend CComplex& operator--(CComplex &c1);
friend CComplex operator++(CComplex &c1, int);
friend CComplex operator--(CComplex &c1, int);
friend CComplex& operator+=(CComplex& c1, const CComplex& c2);
friend CComplex& operator-=(CComplex& c1, const CComplex& c2);
friend CComplex& operator*=(CComplex& c1, const CComplex& c2);
friend CComplex& operator/=(CComplex& c1, const CComplex& c2);
friend bool operator==(const CComplex& c1, const CComplex& c2);
friend bool operator!=(const CComplex& c1, const CComplex& c2);
friend bool operator>(const CComplex& c1, const CComplex& c2);
friend bool operator>=(const CComplex& c1, const CComplex& c2);
friend bool operator<(const CComplex& c1, const CComplex& c2);
friend bool operator<=(const CComplex& c1, const CComplex& c2);
friend ostream& operator<<(ostream& out, const CComplex &c1);
friend istream& operator >> (istream& in, CComplex &c1);
};
//重载+-*/运算符
CComplex operator+(const CComplex& c1, const CComplex& c2)
{
return CComplex(c1.m_fReal + c2.m_fReal, c1.m_fImag + c2.m_fImag);
}
CComplex operator-(const CComplex& c1, const CComplex& c2)
{
return CComplex(c1.m_fReal - c2.m_fReal, c1.m_fImag - c2.m_fImag);
}
CComplex operator*(const CComplex& c1, const CComplex& c2)
{
return CComplex(c1.m_fReal * c2.m_fReal, c1.m_fImag * c2.m_fImag);
}
CComplex operator/(const CComplex& c1, const CComplex& c2)
{
return CComplex(c1.m_fReal / c2.m_fReal, c1.m_fImag / c2.m_fImag);
}
//重载自增自减运算符
//前加加
CComplex& operator++(CComplex &c1)
{
++c1.m_fReal;
++c1.m_fImag;
return c1;
}
//前减减
CComplex& operator--(CComplex &c1)
{
--c1.m_fReal;
--c1.m_fImag;
return c1;
}
//后加加
CComplex operator++(CComplex &c1,int)
{
CComplex c = c1;
++c1.m_fReal;
++c1.m_fImag;
return c;
}
//后减减
CComplex operator--(CComplex &c1, int)
{
CComplex c = c1;
--c1.m_fReal;
--c1.m_fImag;
return c;
}
//重载 += -= *= /=运算符
CComplex& operator+=(CComplex& c1, const CComplex& c2)
{
c1.m_fReal += c2.m_fReal;
c1.m_fImag += c2.m_fImag;
return c1;
}
CComplex& operator-=(CComplex& c1, const CComplex& c2)
{
c1.m_fReal -= c2.m_fReal;
c1.m_fImag -= c2.m_fImag;
return c1;
}
CComplex& operator*=(CComplex& c1, const CComplex& c2)
{
c1.m_fReal *= c2.m_fReal;
c1.m_fImag *= c2.m_fImag;
return c1;
}
CComplex& operator/=(CComplex& c1, const CComplex& c2)
{
c1.m_fReal /= c2.m_fReal;
c1.m_fImag /= c2.m_fImag;
return c1;
}
//重载 == != 运算符
bool operator==(const CComplex& c1, const CComplex& c2)
{
return c1.m_fReal == c2.m_fReal && c1.m_fImag == c2.m_fImag;
}
bool operator!=(const CComplex& c1, const CComplex& c2)
{
return c1.m_fReal != c2.m_fReal || c1.m_fImag != c2.m_fImag;
}
//重载 > >= < <=运算符
bool operator>(const CComplex& c1, const CComplex& c2)
{
if (c1.m_fReal > c2.m_fReal)
return true;
if (c1.m_fReal < c2.m_fReal)
return false;
return c1.m_fImag > c2.m_fImag;
}
bool operator<(const CComplex& c1, const CComplex& c2)
{
if (c1.m_fReal < c2.m_fReal)
return true;
if (c1.m_fReal > c2.m_fReal)
return false;
return c1.m_fImag < c2.m_fImag;
}
bool operator>=(const CComplex& c1, const CComplex& c2)
{
if (c1.m_fReal >= c2.m_fReal)
return true;
else
return false;
}
bool operator<=(const CComplex& c1, const CComplex& c2)
{
if (c1.m_fReal <= c2.m_fReal)
return true;
else
return false;
}
//重载 << 运算符
ostream& operator<<(ostream& out, const CComplex &c1)
{
return out << c1.m_fReal << (c1.m_fImag >= 0 ? "+":"") << c1.m_fImag << "i";
}
istream& operator>>(istream& in, CComplex &c1)
{
return in >> c1.m_fReal >> c1.m_fImag;
}
//简单测试 +-*/ << >> ++
int main()
{
CComplex c1(12,5), c2(2,8);
cout << "c1+c2:" << c1 + c2 << endl;
cout << "c1-c2:" << c1 - c2 << endl;
cout << "c1*c2:" << c1 * c2 << endl;
cout << "c1/c2:" << c1 / c2 << endl;
CComplex c3 = ++c1, c4 = c2++;
cout << "c3 = " << c3 << endl;
cout << "c4 = " << c4 << endl;
CComplex c5;
cin >> c5;
cout << "c5 = " << c5 << endl;
return 0;
}
运行结果:
c1+c2:14+13i
c1-c2:10-3i
c1*c2:24+40i
c1/c2:6+0.625i
c3 = 13+6i
c4 = 2+8i
3 -7
c5 = 3-7i
来源:CSDN
作者:飞fig
链接:https://blog.csdn.net/weixin_42844163/article/details/104127021