复数类

/****************************************************************************************Complex.hpp:
	Copyright (c) Inc.(2016), All rights reserved.

Author:
	xxx

Created Time:
	2016-9-5
****************************************************************************************/

#pragma once
class Complex
{
public:
	// 带缺省值的构造函数
	Complex (double real = 0, double image = 0)
		:_real(real)
		,_image(image)
	{
		cout<<"Complex (double real = 0, double image = 0)"<<endl;
	}

	// 析构函数
	~Complex()
	{
		cout<<"~Complex()"<<endl;
	}

	// 拷贝构造函数
	Complex (const Complex& d)
		:_image(d._image)
		,_real(d._real)
	{
		cout<<"Complex (const Complex& d)"<<endl;
	}

	// 赋值运算符重载
	Complex& operator= (const Complex& d)
	{
		cout<<"operator= (const Complex& d)"<<endl;

		if (this != &d)
		{
			this->_real = d._real;
			this->_image = d._image;
		}

		return *this;
	}

	void Display()
	{
		cout<<"Real:"<<_real<<"--Image:"<<_image<<endl;
	}

public:
	//
	// 1.什么时候可以返回引用(Complex&),什么时候可以返回值
	// 2.前置++和后置++的区别
	//

	Complex& operator++() //前置++
	{
		this->_real++;
		this->_image++;

		return *this;
	}

	Complex operator++(int) //后置++
	{
		Complex tmp(*this);

		this->_real++;
		this->_image++;
		return tmp;
	}

	Complex& operator--();	// 前置--
	Complex operator--(int); //后置--

	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);

private:
	double _real;
	double _image;
};

void TestComplex()
{
	Complex c1(2.2, 1.1);

	Complex c2 = c1++;
	c1.Display();
	c2.Display();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值