C++ 运算符重载(Complex复数类)

Complex复数类

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include 
using namespace std;

class Complex
{
private:
	double real = 0;    //复数的实部
	double imag = 0;    //复数的虚部
public:
	Complex() = default;	 	  /*无参构造*/
	Complex(double r);            /*转换构造*/
	Complex(double a, double b);  /*有参构造*/
	/*负号运算符*/
	Complex  operator - ();
	/*加法运算符*/
	friend Complex  operator + (const Complex& Z1 ,const Complex& Z2);
	/*+=运算符*/
	Complex& operator += (const Complex& Z2);
	/*相等运算符*/
	friend bool operator == (const Complex& Z1, const Complex& Z2);
	/*不相等运算符*/
	friend bool operator != (const Complex& Z1, const Complex& Z2);
	/*输出运算符*/
	friend ostream& operator << (ostream& output, const Complex& Z);
	/*输出运算符*/
	friend	istream& operator >>  (istream& input, const Complex& Z);
	Complex & operator ++();    /*前置自增运算符*/
	Complex  operator ++(int);  /*后置自增运算符*/
	/*函数调用运算符*/
	Complex & operator() (double a, double b);
	/*强制类型转换运算符*/
	explicit operator unsigned int() const;
};

#endif // !COMPLEX_H

Complex.c

#include "Complex.h"

Complex::Complex(double r) :real(r) {}
Complex::Complex(double a, double b) :real(a), imag(b){}
/*负号运算符*/
Complex  Complex::operator - ()
{
	Complex temp;
	temp.real = -real;
	temp.imag = -imag;
	return temp;
}
/*加法运算符*/
Complex operator + (const Complex& Z1, const Complex& Z2)
{
	Complex temp;
	temp.real = Z1.real + Z2.real;
	temp.imag =  Z1.imag + Z2.imag;
	return temp;
}
/*+=运算符*/
Complex& Complex::operator += (const Complex& Z2)
{
	real += Z2.real;
	imag += Z2.imag;
	return *this;
}
/*相等运算符*/
bool operator == (const Complex& Z1, const Complex& Z2)
{
	if (Z1.real == Z2.real && Z1.imag == Z2.imag)
		return true;
	else
		return false;
}
/*不相等运算符*/
bool operator != (const Complex& Z1, const Complex& Z2)
{
	if (Z1.real == Z2.real && Z1.imag == Z2.imag)
		return false;
	else
		return true;
}
/*输出运算符*/
ostream& operator << (ostream& output, const Complex& Z)
{
	output << Z.real << " + " << Z.imag << 'i';
	return output;
}
/*输入运算符*/
istream& operator >> (istream& input, const Complex& Z)
{
	input >> Z.real >> Z.imag;
	return input;
}
/*前置自增运算符重载*/
Complex & Complex::operator ++()
{
	real += 1;
	imag += 1;
	return *this;
}
/*后置自增运算符重载*/
Complex Complex::operator ++(int)
{
	Complex temp;
	temp.real = real;
	temp.imag = imag;
	real += 1;
	imag += 1;
	return temp;
}

/*函数调用运算符*/
Complex& Complex::operator() (double a, double b)
{
	real = a;
	imag = b;
	return *this;
}
/*强制类型转换运算符*/
Complex::operator unsigned int() const
{
	return real;
}

main.c

#include "Complex.h"
#include "Shape.h"

#include 
using namespace std;

void Complex_Test(void)
{
	Complex Z;   /*无参构造*/
	Z(1, 1); 	/*函数调用运算符*/
	cout << Z << endl;
	cout << (unsigned int)Z << endl; 	/*强制类型转换运算符*/
	Complex Z3{12};   /*转换构造*/
	cout << Z3 << endl;
	Complex Z1{ 3,4 };  /*有参构造*/
	cout << "Z1 = " << Z1 << endl;
	Complex Z2{ 2,1 };  /*有参构造*/
	cout << "Z2 = " << Z2 << endl;
	Z = -Z1;
	cout << "-Z1 = " << Z << endl;
	Z = Z1 + Z2;
	cout << "Z1+Z2 = " << Z << endl;
	Z += {2, 2};
	cout << "Z+{2,2} = " << Z << endl;
	Z = Z1++;
	cout << "Z1++ = " << Z << endl;
	Z = ++Z2;
	cout << "++Z2 = " << Z << endl;
	if (Z1 != Z2)
		cout << "不相等" << endl;
	else
		cout << "相等" << endl;
}
int main(void)
{
  	Complex_Test();
	return 0;
}

结果:

C++ 运算符重载(Complex复数类)_第1张图片

你可能感兴趣的:(C++,c++,开发语言)