[c++]复数类的实现

复数类的实现:

对复数类实现如下操作:

Complex operator*(Complex &c);
Complex operator/(Complex &c);
Complex operator+=(Complex &c);
Complex operator-=(Complex &c);
Complex operator*=(Complex &c);
Complex operator/=(Complex &c);
istream& operator>>(istream &in, Complex &c);
ostream& operator<<(ostream &out, const Complex &c);
Complex operator+(int x, Complex &c);
#include<iostream>
using namespace std;

class complex;
//输出<<
ostream& operator<<(ostream &out,const complex &c);
//输入>>
istream& operator>>(istream &in,complex &c);
//加法:整形值 + 对象 (整形值与实部相加)
complex operator+(const int &x,const complex &c);
//乘法:整形值 * 对象 (整形值与实部相加)
complex operator*(const int &x,const complex &c);
class complex
{
//构造函数
public:
	complex(const int real = 0,const int image = 0):m_real(real),m_image(image)
	{}
	~complex()
	{}
	complex(const complex &c)
	{
		m_real = c.m_real;
		m_image = c.m_image;
	}
	complex &operator=(const complex &c)
	{
		if(this == &c)
		{
			return *this;
		}
		m_image = c.m_image;
		m_real = c.m_real;
		return *this;
	}
	void show()
	{
		cout<<"real:"<<m_real<<" "<<"image:"<<m_image<<endl;
	}
//运算符重载
public:
	//加法:对象 + 对象
	complex operator+(const complex &c)
	{
		return complex(m_real + c.m_real,m_image + c.m_image);
	}
	//减法:对象 - 对象
	complex operator-(const complex &c)
	{
		return complex(m_real - c.m_real,m_image - c.m_image);
	}
	//乘法:对象 * 对象
	complex operator*(const complex &c)
	{
		return complex(m_real * c.m_real,m_image * c.m_image);
	}
	//除法:对象 / 对象
	complex operator/(const complex &c)
	{
		return complex(m_real / c.m_real,m_image / c.m_image);
	}
	//加等:对象 += 对象
	complex& operator+=(const complex &c)
	{
		m_real += c.m_real;
		m_image += c.m_image;
		return *this;
	}
	//减等:对象 -= 对象
	complex& operator-=(const complex &c)
	{
		m_real -= c.m_real;
		m_image -= c.m_image;
		return *this;
	}
	//乘等:对象 *= 对象
	complex& operator*=(const complex &c)
	{
		m_real *= c.m_real;
		m_image *= c.m_image;
		return *this;
	}
	//除等:对象 /= 对象
	complex& operator/=(const complex &c)
	{
		m_real /= c.m_real;
		m_image /= c.m_image;
		return *this;
	}
//友元函数
	friend ostream& operator<<(ostream &out,const complex &c);
	friend istream& operator>>(istream &in,complex &c);
	friend complex operator+(const int &x,const complex &c);
	friend complex operator*(const int &x,const complex &c);
private:
	int m_real;
	int m_image;
};
//友元函数定义
ostream& operator<<(ostream &out,const complex &c)
{
	out<<"("<<c.m_real<<","<<c.m_image<<")"<<endl;
	return out;
}
istream& operator>>(istream &in,complex &c)
{
	in>>c.m_real;
	in>>c.m_image;
	return in;
}
complex operator+(const int &x,const complex &c)
{
	return complex(x+c.m_real,c.m_image);
}
complex operator*(const int &x,const complex &c)
{
	return complex(x*c.m_real,c.m_image);
}

int main()
{
	complex c1(1,2);
	complex c2(2,3);

	cin>>c2;
	cout<<c2;

	complex c3 = c1 + c2;
	cout<<c3;

	complex c4 = c1 - c2;
	cout<<c4;

	complex c5 = c1 * c2;
	cout<<c5;

	complex c6 = c1 / c2;
	cout<<c6;

	complex c7(1,1);
	c7 += c1;
	cout<<c7;

	return 0;

}


[c++]复数类的实现_第1张图片

你可能感兴趣的:(复数类,实部,算数操作,虚部)