简单的复数Complex类实现

实现一个很简单的复数类Complex  

主要实现了几个运算符重载。

具体代码如下:

头文件Complex.h 函数声明和类定义

//Complex.h
#include
#pragma once
class Complex
{
public:
	Complex(double real = 0.0, double image = 0.0);
	~Complex();
	Complex(Complex & c1);

	//运算符重载函数
	Complex & operator=(const Complex &d);  //赋值函数
	bool operator==(const Complex & d); //判断相等
	Complex operator+(const Complex &d);  //两个复数相加
	Complex& operator+=(const Complex &d);  //复数对象+=d
	Complex operator-(const Complex &d);  //两个复数相减
	Complex& operator-=(const Complex &d);  //复数对象-=d
	Complex operator++();  //前置++
	Complex operator++(int);  //后置++  且不用添加参数  特殊形式
	Complex operator--();  //前置--
	Complex operator--(int);  //后置--
	void Display();  //显示复数的实部和虚部

private:
	double _real;
	double _image;
};
源文件 Complex.cpp  类的成员函数实现:

//Complex.cpp
#include"Complex.h"
//构造函数
//Complex::Complex(double real, double image) 
//{
//	_real = real;
//	_image = image;
//}
Complex::Complex(double real, double image) 
:_real(real),
_image(image)
{
}
//拷贝构造函数
Complex::Complex(Complex & c1) :
_real(c1._real),
_image(c1._image)
{
}
//析构函数
Complex::~Complex()
{
}
Complex & Complex::operator=(const Complex &d)
{
	this->_real = d._real;
	this->_image = d._image;
	return *this;
}
bool Complex::operator==(const Complex & d) //判断相等
{
	return this->_image == d._image&&this->_real == d._real;
}
Complex Complex::operator+(const Complex &d)  //两个复数相加
{
	Complex tmp;
	tmp._image = this->_image + d._image;
	tmp._real = this->_real + d._real;
	return tmp;
}

Complex& Complex::operator+=(const Complex &d)  //复数对象+=d
{
	this->_image += d._image;
	this->_real += d._real;
	return *this;
}

Complex Complex::operator-(const Complex &d)  //两个复数相减
{
	Complex tmp;
	tmp._image = this->_image - d._image;
	tmp._real = this->_real - d._real;
	return tmp;
}

Complex& Complex::operator-=(const Complex &d)  //复数对象-=d
{
	this->_image -= d._image;
	this->_real -= d._real;
	return *this;
}

//这两者返回值不同而已
Complex Complex::operator++()  //前置++
{
	//*this自增
	this->_image++;
	this->_real++;
	//返回增加后的值
	return *this;
}
Complex Complex::operator++(int)  //后置++  一种特殊形式
{
	Complex tmp=*this;
	//*this自增
	this->_image++;
	this->_real++;
	//返回增加之前
	return tmp;
}

Complex Complex::operator--()  //前置--
{
	//*this自减
	this->_image--;
	this->_real--;
	//返回减小后的值
	return *this;
}

Complex Complex::operator--(int )  //后置--
{
	Complex tmp = *this;
	//*this自减
	this->_image--;
	this->_real--;
	//返回减小之前
	return tmp;
}
//输出
void Complex::Display()
{
	//输出复数
	std::cout << "real:" << _real<


源文件:Main.cpp 测试用例

//Main.cpp  测试用例
#include
#include
#include"Complex.h"
using namespace std;

void Test1()  //+=  -=  + - 测试
{
	Complex c1(1.0,2.0), c2(2.0,1.0);
	Complex c3=c1 -= c2;
	//Complex c3=c1 += c2;
	//Complex c3 = c1 + c2;
	//Complex c3=c1 - c2;
	c1.Display();
	c3.Display();
}
void Test2()  // ++测试
{
	Complex c1(1.0, 5.0);
	Complex c2=c1++;
	Complex c3 = ++c1;
	c1.Display();
	c2.Display();
	c3.Display();
}


void Test3()
{
	Complex c1(1.0, 5.0);
	Complex c2 = c1--;
	Complex c3 = --c1;
	c1.Display();
	c2.Display();
	c3.Display();
}

int main()
{
	Test3();
	system("pause");
	return 0;
}



你可能感兴趣的:(C++,类,复数类,Complex)