本文意在通过 复数 运算来体现 C++ 符号重载 之 ( + - * / == != += -= *= /= )
1、通过全局函数作为友元函数实现( + - * / == != )的重载;
2、通过成员函数实现( += -= *= /= ) 的重载;
3、特别提醒需要注意( *= /= ) 的重载,在重载的过程中,需要借助临时变量;
代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
//创建复数类
class Complex
{public:
Complex();
Complex(double real, double imag);
void display() const;
//Complex operator+(const Complex &c) const;//以成员函数重载 + 号
//以友元函数重载 + - * / == !=
friend Complex operator+(const Complex &t1, const Complex &t2);
friend Complex operator-(const Complex &t1, const Complex &t2);
friend Complex operator*(const Complex &t1, const Complex &t2);
friend Complex operator/(const Complex &t1, const Complex &t2);
friend bool operator==(const Complex &t1, const Complex &t2);
friend bool operator!=(const Complex &t1, const Complex &t2);
//以成员函数重载 += -= *= /=
Complex & operator+=(const Complex &t);
Complex & operator-=(const Complex &t);
Complex & operator*=(const Complex &t);
Complex & operator/=(const Complex &t);
protected:
double m_real;
double m_imag;
string m_flag = " + ";//为避免出现虚部为负数的时候,出现+ -*i的形式设立符号标识
};
//构造函数实现
Complex::Complex() { m_real = 0.0; m_imag = 0.0; }
Complex::Complex(double real,double imag):m_real(real),m_imag(imag){}
//成员函数实现
void Complex::display() const
{
cout << m_real << m_flag << m_imag << "i" << endl;
}
//成员函数重载 + 号
//Complex Complex::operator+(const Complex &c) const
//{
// Complex tem;
// tem.m_real = this->m_real + c.m_real;
// tem.m_imag = this->m_imag + c.m_imag;
// return tem;
// //精简写法,会自动创建一个“无名”的临时变量作为返回值,本法只适用于成员函数重载+号
// //return Complex(this->m_real + c.m_real, this->m_imag + c.m_imag);
//}
//全局函数作为友元函数 重载 + 号
Complex operator+(const Complex &t1, const Complex &t2)
{
Complex tem;
tem.m_real = t1.m_real + t2.m_real;
tem.m_imag = t1.m_imag + t2.m_imag;
//虚部符号判断,如果为 负,则改写 m_flag为“ - ”,并将虚部m_imag乘以 -1 转为正值(取绝对值)
if (tem.m_imag < 0)
{
tem.m_flag = " - ";
tem.m_imag *= -1;
}
return tem;
}
//全局函数作为友元函数 重载 - 号
Complex operator-(const Complex &t1, const Complex &t2)
{
Complex tem;
tem.m_real = t1.m_real - t2.m_real;
tem.m_imag = t1.m_imag - t2.m_imag;
//虚部符号判断,如果为 负,则改写 m_flag为“ - ”,并将虚部m_imag乘以 -1 转为正值(取绝对值)
if (tem.m_imag < 0)
{
tem.m_flag = " - ";
tem.m_imag *= -1;
}
return tem;
}
//全局函数作为友元函数 重载 * 号
Complex operator*(const Complex &t1, const Complex &t2)
{
Complex tem;
tem.m_real = t1.m_real * t2.m_real - t1.m_imag * t2.m_imag;
tem.m_imag = t1.m_real * t2.m_imag + t1.m_imag * t2.m_real;
//虚部符号判断,如果为 负,则改写 m_flag为“ - ”,并将虚部m_imag乘以 -1 转为正值(取绝对值)
if (tem.m_imag < 0)
{
tem.m_flag = " - ";
tem.m_imag *= -1;
}
return tem;
}
//全局函数作为友元函数 重载 / 号
Complex operator/(const Complex &t1, const Complex &t2)
{
Complex tem;
tem.m_real = (t1.m_real*t2.m_real + t1.m_imag*t2.m_imag) / (t2.m_real*t2.m_real + t2.m_imag*t2.m_imag);
tem.m_imag = (t1.m_real*t2.m_imag + t1.m_imag*t2.m_real) / (t2.m_real*t2.m_real + t2.m_imag*t2.m_imag);
//虚部符号判断,如果为 负,则改写 m_flag为“ - ”,并将虚部m_imag乘以 -1 转为正值(取绝对值)
if (tem.m_imag < 0)
{
tem.m_flag = " - ";
tem.m_imag *= -1;
}
return tem;
}
//全局函数作为友元函数 重载 == 号
bool operator==(const Complex &t1, const Complex &t2)
{
if (t1.m_real == t2.m_real&&t1.m_imag == t2.m_imag)
return true;
else
return false;
}
//全局函数作为友元函数 重载 != 号
bool operator!=(const Complex &t1, const Complex &t2)
{
if (t1.m_real == t2.m_real&&t1.m_imag == t2.m_imag)
return false;
else
return true;
}
//以成员函数 重载 += 号
Complex & Complex::operator+=(const Complex &t)
{
//this->m_real = this->m_real + t.m_real;
//this->m_imag = this->m_imag + t.m_imag;
this->m_real += t.m_real;
this->m_imag += t.m_imag;
//虚部符号判断,如果为 负,则改写 m_flag为“ - ”,并将虚部m_imag乘以 -1 转为正值(取绝对值)
if (this->m_imag < 0)
{
this->m_flag = " - ";
this->m_imag *= -1;
}
return *this;
}
//以成员函数 重载 -= 号
Complex & Complex::operator-=(const Complex &t)
{
//this->m_real = this->m_real - t.m_real;
//this->m_imag = this->m_imag - t.m_imag;
this->m_real -= t.m_real;
this->m_imag -= t.m_imag;
//虚部符号判断,如果为 负,则改写 m_flag为“ - ”,并将虚部m_imag乘以 -1 转为正值(取绝对值)
if (this->m_imag < 0)
{
this->m_flag = " - ";
this->m_imag *= -1;
}
return *this;
}
//以成员函数 重载 *= 号,这里需要借助临时变量
Complex & Complex::operator*=(const Complex &t)
{
Complex tem = *this;//用临时变量存储原数据,
//因为在接下来的第一步操作过后,原数据的实数部分this->m_real发生了变化
//导致第二部计算虚数部分的操作,不在是原来的实部和虚部的计算
this->m_real = tem.m_real * t.m_real - tem.m_imag * t.m_imag;
this->m_imag = tem.m_real * t.m_imag + tem.m_imag * t.m_real;
//虚部符号判断,如果为 负,则改写 m_flag为“ - ”,并将虚部m_imag乘以 -1 转为正值(取绝对值)
if (this->m_imag < 0)
{
this->m_flag = " - ";
this->m_imag *= -1;
}
return *this;
}
//以成员函数 重载 /= 号,这里需要借助临时变量
Complex & Complex::operator/=(const Complex &t)
{
Complex tem = *this;//用临时变量存储原数据,
//因为在接下来的第一步操作过后,原数据的实数部分this->m_real发生了变化
//导致第二部计算虚数部分的操作,不在是原来的实部和虚部的计算
this->m_real = (tem.m_real * t.m_real - tem.m_imag * t.m_imag) / (t.m_real * t.m_real + t.m_imag * t.m_imag);
this->m_imag = (tem.m_real * t.m_imag + tem.m_imag * t.m_real) / (t.m_real * t.m_real + t.m_imag * t.m_imag);
//虚部符号判断,如果为 负,则改写 m_flag为“ - ”,并将虚部m_imag乘以 -1 转为正值(取绝对值)
if (this->m_imag < 0)
{
this->m_flag = " - ";
this->m_imag *= -1;
}
return *this;
}
//偷懒,定义了2个全局复数 A,B
Complex A(3.0, 4.0);
Complex B(2.0, 5.0);
//打印出来做对比用
void cpr()
{
cout << "=================================" << endl << endl;
printf("A = ");
A.display();
printf("B = ");
B.display();
}
//验证 + 号重载
void test01()
{
cout << endl << "========= + - * / ===============" << endl <<endl;
/*Complex A(-3.4, 4.5);
Complex B(6.7, 8.9);*/
Complex C = A + B;
//Complex C = A.operator+(B);//这种写法只适用于成员函数重载+号的方式
//C = A + B;
cout << "A + B = ";
C.display();
}
//验证 - 号重载
void test02()
{
//cout << "========= - 号===============" << endl;
/*Complex A(-3.4, 4.5);
Complex B(6.7, 8.9);*/
Complex C;
C = A - B;
cout << "A - B = ";
C.display();
}
//验证 * 号重载
void test03()
{
//cout << "========= * 号===============" << endl;
/*Complex A(-3.4, 4.5);
Complex B(6.7, 8.9);*/
Complex C;
C = A * B;
cout << "A * B = ";
C.display();
}
//验证 / 号重载
void test04()
{
//cout << "========= / 号===============" << endl;
/*Complex A(-3.4, 4.5);
Complex B(6.7, 8.9);*/
Complex C;
C = A / B;
cout << "A / B = ";
C.display();
}
//验证 == 号重载
void test05()
{
cout << endl << "=========== == != ===============" << endl << endl;
/*Complex A(-3.4, 4.5);
Complex B(6.7, 8.9);*/
bool C = A == B;
if (C)
{
cout << "A == B 成立" << endl;
}
else
{
cout << "A == B 不成立" << endl;
}
}
//验证 != 号重载
void test06()
{
//cout << endl << "======== != 号===============" << endl;
/*Complex A(-3.4, 4.5);
Complex B(6.7, 8.9);*/
bool C = A != B;
if (C)
{
cout << "A != B 成立" << endl;
}
else
{
cout << "A != B 不成立" << endl;
}
}
//验证 += -= *= /= 符号号重载
void test07()
{
cout << endl << "========= += -= *= /= ============" << endl << endl;
/*Complex A(-3.4, 4.5);
Complex B(6.7, 8.9);*/
A += B;
cout << "A += B = " ;
A.display();
//cout << "===============================" << endl;
A -= B;
cout << "A -= B = ";
A.display();
//cout << "===============================" << endl;
A *= B;
cout << "A *= B = ";
A.display();
//cout << "===============================" << endl;
A /= B;
cout << "A /= B = " ;
A.display();
}
int main()
{
cpr();
test01();
test02();
test03();
test04();
test05();
test06();
test07();
cout << endl;
system("pause");
return EXIT_SUCCESS;
}
运行结果:
=================================
A = 3 + 4i
B = 2 + 5i
========= + - * / ===============
A + B = 5 + 9i
A - B = 1 - 1i
A * B = -14 + 23i
A / B = 0.896552 + 0.793103i
=========== == != ===============
A == B 不成立
A != B 成立
========= += -= *= /= ============
A += B = 5 + 9i
A -= B = 3 + 4i
A *= B = -14 + 23i
A /= B = -4.93103 - 0.827586i