简单的复数Complex类实现

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

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

具体代码如下:

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

//Complex.h
#include<iostream>
#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<<std::endl;
	std::cout << "image:" << _image << std::endl;
}


源文件:Main.cpp 测试用例

//Main.cpp  测试用例
#include<stdlib.h>
#include<iostream>
#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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值