栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ Complex类(without pointer member(s))

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ Complex类(without pointer member(s))

#ifndef __Complex__
#define __Complex__
#include
using namespace std;
//using std::cout;
//using std::ostream;


//class ostream;
class Complex;
Complex& _doapl(Complex* ths, const Complex& r);


class Complex {
public:
	Complex(double a=0,double b=0):re(a),im(b){}
	double real() const { return re; }
	double imag() const { return im; }
	Complex& operator+=(const Complex& r);
	//Complex& _doapl(Complex* ths, const Complex& r);
	
private:
	double re, im;
	friend Complex& _doapl(Complex* ths, const Complex& r);
};


//这个应该就是class类内互为友元
//inline Complex& Complex::_doapl(Complex* ths, const Complex& r) {
//	ths->re += r.re;
//	ths->im += r.im;
//	return *ths;
//}

//此函数如果不是友元函数就不能访问复数类中的私有成员变量
inline Complex& _doapl(Complex* ths, const Complex& r) {
	ths->re += r.re;
	ths->im += r.im;
	return *ths;
}
//+=运算符考虑重载运算符的时候返回值应该为complex&,而不能是void
//不然当有多个连续的 += 运算时会报错,因为返回值为void,不能再进行 += 运算
inline Complex& Complex::operator+=(const Complex& r) {
	return _doapl(this, r);
}



inline double imag(const Complex& x) {
	return x.imag();
}
inline double real(const Complex& x) {
	return x.real();
}
//为了对付client的三种可能用法,开发三个函数(非成员函数)
//因为返回值为临时对象所以返回值不能是引用
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) {
	return x;
}
//绝不可以return by reference,因为其返回的必定是个local object
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) && 0 == 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) != y || imag(x) != 0;
}
inline bool operator!=(double x, const Complex& y) {
	return x != real(y) || 0 != imag(y);
}

inline Complex conj(const Complex& x) {
	return Complex(real(x), -imag(x));
}
ostream& operator<<(ostream& os, const Complex& c) {
	return os << '(' << real(c) << ',' << imag(c)<<')';
}

int main() {
	Complex c1(2, 1);
	Complex c2(5);
	c2 += c1;
	c2 += c2 += c1;//+=重载返回值不能是void

	//client的三种可能用法
	c2 = c1 + c2;
	c2 = c1 + 5;
	c2 = 7 + c1;
//	cout << c2 << endl;

	cout << -c1 << endl;
	cout << +c1 << endl;

	cout << (c1 == c2);
	cout << (c1 == 2);
	cout << (0 == c2);
}

#endif
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/862270.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号