c++复数类

写一个简单的类完成默认函数和简单的运算符重载

1.完成四个默认成员函数 

2.比较运算符 

3.前置后置++和+/+=的实现 


#ifndef __COMPLEX_H__
#define __COMPLEX_H__

#define _CRT_SECURE_NO_DEPRECATE 1
#include<iostream>
#include<stdlib.h>
using namespace std;

class Complex
{

public:
    Complex (int x,int y)		//列表初始化
		:_real(x),
		_img(y)
	{
	}

	Complex(const Complex& d)	//拷贝构造
	{
		this->_real = d._real;
		this->_img = d._img;
	}

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

	Complex& operator=(const Complex& d)  //赋值运算符的重载
	{
		if(&d != this)
		{
		this->_real = d._real;
		this->_img = d._img;
		}
		return *this;
	}
bool operator>(const Complex & d);
Complex& operator+=(const Complex& d);
Complex& operator++();
Complex operator++(int);


	
void Print_Complex();
	
private:
	int _real;
	int _img;

};

#endif // !__COMPLEX__H_


#include"Complex.h"

void Complex::Print_Complex()
{
	if(_img<0)
	cout<<_real<<_img<<"i"<<endl;
	else
	cout<<_real<<"+"<<_img<<"i"<<endl;
}
bool Complex::operator>(const Complex& d)                  //比较运算符(复数实不能这样比较大小的,这只是为实现>符号的重载才这样做)
{
	return (this->_real)>d._real && ((this->_img) >d._img || (this->_img) == d._img);
}
Complex& Complex::operator++()			      	  //前置++,默认给实部加1;
{
	this->_real+=1;
	return *this;
}
Complex Complex::operator++(int)			  //后置++,默认给实部加1;
{
	Complex tmp = *this;
	this->_real += 1;
	return tmp;
}
Complex& Complex::operator+=(const Complex& d)            //预算符+=重载;
{
	this->_real += d._real;
	this->_img += d._img;
	return *this;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值