复数类 运算符重载

#include<iostream>
using namespace std;


class Complex
{
public:
	friend ostream& operator<<(ostream& _cout, Complex c); //输出函数需要访问私有成员,声明为友元函数
	Complex(double real = 0.0, double image = 0.0);
	Complex(const Complex& c);
	Complex& operator=(const Complex& c);
	Complex operator+(const Complex& c);
	Complex operator-(const Complex& c);
	Complex operator*(const Complex& c);
	Complex operator/(const Complex& c);
	Complex& operator+=(const Complex& c);
	Complex& operator-=(const Complex& c);
	Complex& operator*=(const Complex& c);
	Complex& operator/=(const Complex& c);
	~Complex();


private:
	double _real;
	double _image;
};
Complex::Complex(double real, double image):// 构造函数
_real(real),
_image(image)
{
}
Complex::Complex(const Complex& c) :// 拷贝构造
         _real(c._real)
		 , _image(c._image)
{
	
}
Complex& Complex::operator=(const Complex &c) // 赋值运算符重载  返回值为引用
{
	if (this != &c)
	{
		this->_real = c._real;
		this->_image = c._image;
	}
      
	  return *this;
}
Complex Complex::operator+(const Complex &c)// 复数相加  +运算符重载
{
	Complex temp;
	temp._real = this->_real + c._real;
	temp._image = this->_image + c._image;
	return temp;
}
Complex Complex::operator-(const Complex &c)// 复数相减 -运算符重载
{
	Complex temp;
	temp._real = this->_real - c._real;
	temp._image = this->_image - c._image;
	return temp;
}
Complex Complex::operator*(const Complex &c)// 复数相乘 *运算符重载
{
	Complex temp;
	temp._real = this->_real*c._real - this->_image*c._image;
	temp._image = this->_real*c._image + c._real*this->_image;
	return temp;
}
Complex Complex::operator/(const Complex &c)// 复数相除 /运算符重载
{
	Complex temp;
	temp._real = (this->_real * c._real + this->_image*c._image) / (c._real*c._real + c._image *c._image);
	temp._image = (c._real * this->_image - this->_real*c._image) / (c._real*c._real + c._image *c._image);
	return temp;


}
Complex& Complex:: operator+=(const Complex &c)// 复数+=运算符重载
{
	this->_real = this->_real + c._real;
	this->_image = this->_image + c._image;
	return *this;
}
Complex& Complex:: operator-=(const Complex &c) 复数-=运算符重载
{
	this->_real = this->_real - c._real;
	this->_image = this->_image - c._image;
	return *this;
}
Complex& Complex::operator*=(const Complex& c)// 复数 *=运算符重载
{
	this->_real = this->_real*c._real - this->_image*c._image;
	this->_image = this->_real*c._image + c._real*this->_image;
	return *this;
}
Complex& Complex::operator/=(const Complex& c)// 复数 /=运算符重载
{
	this->_real = (this->_real * c._real + this->_image*c._image) / (c._real*c._real + c._image *c._image);
	this->_image = (c._real * this->_image - this->_real*c._image) / (c._real*c._real + c._image *c._image);
	return *this;
}
 Complex::~Complex()// 析构函数
{


}
 ostream& operator<<(ostream& _cout, Complex c)// 输出运算符<<重载
 {
	 _cout << c._real << " " << c._image;
	 return _cout;
 }
int main()
{
	Complex a(2, 0);
	Complex b(a);
	a *= b;
	cout << a << endl;
	system("pause");
	return 0;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值