C++面向对象之类complex详解

类complex可作为入门C++面向对象的经典类。complex的原型是数学上的复数。我们都知道复数有实部和虚部。所以类complex有两个数据成员re, im。复数有加,减,乘,除,取反,求共轭等操作,类complex也有相对应的成员函数来实现这些操作。下面是类complex的定义和测试代码,已在vs测试过。

类complex的定义

//complex.h

#ifndef _MYCOMPLEX
#define _MYCOMPLEX

class complex;

complex& _doapl (complex* ths, const complex& r);
complex& _doami (complex* ths, const complex& r);
complex& _doaml (complex* ths, const complex& r);

class complex
{
public:
	//构造函数,采用初始化列表
	complex(double r = 0, double i = 0) : re(r), im(i) { }
	//返回complex&,允许 a+=b+=c 情况; 传参complex&,相比较传complex,效率更高
	complex& operator+= (const complex&);
	complex& operator-= (const complex&);
	complex& operator*= (const complex&);
	complex& operator/= (const complex&);
	//定义两个公有接口,方便普通函数访问私有成员;const禁止成员函数修改私有成员
	double real () const { return re; }
	double imag () const { return im; }

private:
	double re, im;

	//声明为友元函数,可直接调用私有成员
	friend complex& _doapl (complex*, const complex&);
	friend complex& _doami (complex*, const complex&);
	friend complex& _doaml (complex*, const complex&);
};

inline complex& _doapl (complex* ths, const complex& r)
{
	ths->re += r.re;
	ths->im += r.im;
	return *ths;
}

//重载操作符+=,两复数相加
inline complex& complex :: operator += (const complex& r)
{
	return _doapl(this, r);
}

inline complex& _doami(complex* ths, const complex& r)
{
	ths->re -= r.re;
	ths->im -= r.im;
	return *ths;
}

//重载操作符-=,两复数相减
inline complex& complex :: operator -= (const complex& r)
{
	return _doami(this, r);
}

inline complex& _doaml(complex* ths, const complex& r)
{
	double f = ths->re * r.re - ths->im * r.im;
	ths->im += ths->re * r.im + ths->im * r.re;
	ths->re = f;
	return *ths;
}

//重载操作符*=,两复数相乘
inline complex& complex :: operator *= (const complex& r)
{
	return _doaml(this, r);
}

//取实部
inline double imag(const complex& x)
{
	return x.imag();
}

//取虚部
inline double real(const complex& x)
{
	return x.real();
}

//重载操作符+,复数与复数相加(此处只能返回complex,因return的是一个局部变量complex)
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 complex operator* (const complex& x, const complex& y)
{
	return complex(real(x) * real(y) - imag(x) * imag(y),
		real(x) * imag(y) + imag(x) * real(y));
}

//重载操作符*,复数与实数相乘
inline complex operator* (const complex& x, double y)
{
	return complex(real(x) * y, imag(x) * y);
}

//重载操作符*,实数与复数相乘
inline complex operator* (double x, const complex& y)
{
	return complex(x * real(y), x * imag(y));
}

//重载操作符/,复数与实数相除
inline complex operator/ (const complex& x, double y)
{
	return complex(real(x) / y, imag(x) / y);
}

//取正
inline complex operator+ (const complex &x)
{
	return x;
}

//取反
inline complex operator- (const complex &x)
{
	return complex(-real(x), -imag(x));
}

//重载操作符==,复数等于复数
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 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;
}

#include 
//极坐标求坐标
inline complex polar(double r, double t)
{
	return complex(r * cos(t), r * sin(t));
}

//求共轭
inline complex conj(const complex& x)
{
	return complex(real(x), -imag(x));
}

//求绝对值
inline double norm(const complex& x)
{
	return real(x) * real(x) + imag(x) * imag(x);
}

#endif // !_MYCOMPLEX

测试代码

//complex_test.cpp

#include 
#include "complex.h"

using namespace std;
//重载操作符<<,可直接输出类complex;返回ostream&,允许cout << a << b 操作
ostream& operator<< (ostream& os, const complex& x)
{
	return os << '(' << real(x) << ',' << imag(x) << ')';
}

int main()
{
	complex c1(2, 1);
	complex c2(4, 0);

	cout << c1 << endl;
	cout << c2 << endl;

	cout << c1 + c2 << endl;
	cout << c1 - c2 << endl;
	cout << c1 * c2 << endl;
	cout << c1 / 2 << endl;

	cout << conj(c1) << endl;
	cout << norm(c1) << endl;
	cout << polar(10, 4) << endl;

	cout << (c1 += c2) << endl;

	cout << (c1 == c2) << endl;
	cout << (c1 != c2) << endl;
	cout << +c2 << endl;
	cout << -c2 << endl;

	cout << (c2 - 2) << endl;
	cout << (5 + c2) << endl;

	system("pause");
	return 0;
}

 

你可能感兴趣的:(C++)