自己实现一个complex class(复数类)


前言

最近在听侯捷老师的C++课程,第一部分是实现一个不带指针成员的类(class without pointer members),举的例子是标准库中的complex类。听完后就自己试着写了一下,目前能实现的操作有:

  1. complex class object的创建及初始化;
  2. 获得一个复数的实部和虚部;
  3. 取正和取反(+,-);
  4. 加法、减法、乘法(+、-、*);
  5. 判断是否相等与不等关系(==、!=)
  6. 输入(>>)与输出(<<);
  7. 求共轭复数、求复数的模。

一、complex class的声明与定义

我将complex class的声明与定义均包含在了头文件中,代码如下:

#ifndef __COMPLEX__    //防卫式声明,__COMPLEX__:这个名字可以自己取
#define __COMPLEX__
#include <iostream>
using namespace std;

class complex {
public:
	complex(double r = 0, double i = 0) : re(r), im(i) {}       //构造函数,带有默认实参
	complex& operator += (const complex& x);
	complex& operator -= (const complex& x);
	complex& operator *= (const complex& x);
	double real() const { return re; };
	double imag() const { return im; };
private:
	double re, im;

	friend complex& _doapl(complex* ths, const complex& x);
	friend complex& _doami(complex* ths, const complex& x);
	friend complex& _doaml(complex* ths, const complex& x);
	friend istream& operator >> (istream& is, complex& x);    //因为>>操作要改变对象的数据,所以必须获得访问对象private区的权力
};

/*三个友元函数,它们访问了x的私有成员,所以这三个函数必须声明为complex的友元*/
inline complex& _doapl(complex* ths, const complex& x)
{
	ths->re += x.re;
	ths->im += x.im;
	return *ths;
}
inline complex& _doami(complex* ths, const complex& x)
{
	ths->re -= x.re;
	ths->im -= x.im;
	return *ths;
}
inline complex& _doaml(complex* ths, const complex& x) //复数的乘法不是简单的对应相乘!
{
	double temp = ths->re * x.re - ths->im * x.im;     //后面得到虚部会改变ths->re的值,所以需要临时变量temp来存放结果的re
	ths->im = ths->re * x.im + ths->im * x.re;
	ths->re = temp;
	return *ths;
}
/*获得x的实部和虚部的(非成员)函数*/
inline double real(const complex x)
{
	return x.real();
}
inline double imag(const complex x)
{
	return x.imag();
}
/*正负号函数*/
inline complex operator + (const complex& x)
{
	return x;
}
inline complex operator - (const complex& x)
{
	return (-real(x), -imag(x));
}
/*+=、-=、*=、/=操作符重载函数(成员函数)*/
inline complex& complex::operator += (const complex& x)
{
	return _doapl(this, x);
}
inline complex& complex::operator -= (const complex& x)
{
	return _doami(this, x);
}
inline complex& complex::operator *= (const complex& x)
{
	return _doaml(this, x);
}
/*+、-(加减法)函数*/
inline complex operator + (const complex& x, const complex& y)
{
	return complex(real(x) + real(y), imag(x) + imag(y));
}
inline complex operator + (const complex& x, double y)
{
	return complex(real(x) + y, imag(x));
}
inline complex operator + (double x, const complex& y)
{
	return complex(x + real(y), imag(y));
}
inline complex operator - (const complex& x, const complex& y)
{
	return complex(real(x) + real(y), imag(x) + imag(y));
}
inline complex operator - (const complex& x, double y)
{
	return complex(real(x) - y, imag(x));
}
inline complex operator - (double x, const complex& y)
{
	return complex(x - real(y), -imag(y));
}
/*==、!=函数*/
inline bool operator == (const complex& x, const complex& y)
{
	return (real(x) == real(y) && imag(x) == imag(y));
}
inline bool operator == (const complex& x, double y)
{
	return (real(x) == real(y) && imag(x) == 0);
}
inline bool operator == (double x, const complex& y)
{
	return (x == real(y) && imag(y) == 0);
}
inline bool operator != (const complex& x, const complex& y)
{
	return (real(x) != real(y) || imag(x) != imag(y));
}
inline bool operator != (const complex& x, double y)
{
	return (real(x) != y || imag(x) != 0);
}
inline bool operator != (double x, const complex& y)
{
	return (x != real(y) || imag(y) != 0);
}
/*<<的重载函数*/
inline ostream& operator << (ostream& os, const complex& x)
{
	os << '(' << real(x) << ',' << imag(x) << ')';
	return os;
}
/*>>的重载函数*/
inline istream& operator >> (istream& is, complex& x)
{
	is >> x.re >> x.im;
	return is;
}
/*共轭复数函数conj*/
inline complex conj(const complex& x)
{
	return complex(real(x), -imag(x));
}
/*复数求模函数hypot*/
#include <cmath>
inline double hypot(const complex& x)
{
	return sqrt(real(x) * real(x) + imag(x) * imag(x));
}
#endif // !__COMPLEX__

二、测试文件

1.main函数

在main函数中测试了class的各种成员函数和非成员函数操作,代码如下:

#include "complex.h"
int main()
{
	complex c1(2, 3), c2(3, 5);         //对象的创建和初始化
	double c3 = 10;
	cout << "c1 = " << c1 << '\n' << "c2 = " << c2 << endl;   //测试输出
	cout << "c3 = " << c3 << endl;
	cout << "c1 + c2 = " << c1 + c2 << endl;                  //测试+、-、*
	cout << "c1 - c2 = " << c1 - c2 << endl;
	cout << "c1 + c3 = " << c1 + c3 << endl;                  //测试+、-、*
	cout << "c3 - c1 = " << c3 - c1 << endl;
	//cout << "c1 * c2 = " << c1 * c2 << endl;
	cout << "c1的共轭复数是:" << conj(c1) << endl;
	cout << "c1的模是:" << hypot(c1) << endl;
	//测试输入
	complex c4;
	cout << "Please input a complex, you can only do it by inputing its real part first and imag part second! \n";
	cin >> c4;
	cout << "刚才输入的复数c4 = " << c4 << endl;

	return 0;
}

2.输出结果

输出结果如下:
测试结果

总结

目前写到这里,以后再继续更新,欢迎提出意见!

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值