复数类

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

class Complex 
{ 
public:
	void Display()
	{
		cout<< "_real:"<<_real<<"    "<<"_image:"<<_image<<endl;
	}

	//Complex()//无参数构造函数
	//{
	//}
	//1.全缺省构造函数构造函数
	Complex(double real=0.0,double image=0.0)

	{
		cout<< "Complex(double real=0.0,double image=0.0)"<<endl;
		_real = real;
		_image = image;
	}

	//2.拷贝函数
	Complex(const Complex& d)//
		:_real(d._real)
		,_image(d._image)
	{
		cout<<" Complex(const Complex& d)"<<endl;//注意引用符,若为值传递则可能会引发无穷递归  
		
	}
	//3.析构函数
	~Complex()
	{
		cout<<"~Complex()"<<endl;
	}
public:
	Complex& operator=(const Complex& x)//赋值操作符重载
	{
		if(this!=&x)
		{
			this->_real = x._real;
			this->_image = x._image;
		}
		return *this;
	}
	bool operator==(const Complex& x)//比较两个复数是否相等
	{
		return (this->_image==x._image)&&(this->_real==x._real);
	}
	//void operator+(const Complex& x)
	//{
	//	this->_real = this->_real+x._real;
	//	this->_image = this->_image+x._image;
	//}
	Complex operator+(const Complex& x)
	{
		Complex tmp;
		tmp._real = this->_real + x._real;
		tmp._image = this->_image + x._image;
		return tmp;
	}
	Complex& operator+=(const Complex& x)
	{
		this->_real += x._real;
		this->_image += x._image;
		return *this;
	}
	Complex& operator++()//前置++
	{
		this->_real += 1;
		return *this;
	}
	Complex operator++(int) //后置++
	{
		Complex tmp(this->_real,this->_image);
		this->_real += 1;
		return tmp;
	}
	
private: 
	double _real; 
	double _image; 
};
void Test()  
{  
	//构造函数  
	//Complex p;   
	//p.Display();  
	Complex d1;  
	Complex d2(2.0,1.0);  
	d1.Display();  
	d2.Display();  
	//拷贝构造函数  
	Complex d3(d1);  
	Complex d4 = d1;  
	d3.Display();  
	d4.Display();  
}  
//操作符的重载  
void Test1()  
{
	Complex d1(2.0, 0.0);  
	Complex d2(2.0, 0.0);  
	
	Complex d;  
	d1.Display();  
	d2.Display();  
	d = d1;  
	d.Display();  
	bool ret;  
	ret = d1 == d2;  
	cout << "---------ret = " << ret << endl;  
	d = d1+d2; 
	d.Display(); 
	d1 += d2;
	d1.Display();
}  
//自增的重载  
void Test2()
{
	Complex d1(0.0,0.0);
	d1.Display();
	d1++;
	d1.Display();
}
int main()
{
	//Test();
	//Test1();
	Test2();
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值