【原创】C++_第一周_C++ complex类的实现(我的第一个c++程序)

本文介绍了作者的第一个C++程序,实现了一个complex类,包括类模板、操作符重载、赋值函数、相等比较等功能,并使用了友元函数和this指针。代码简洁并附有注释。
摘要由CSDN通过智能技术生成

如果喜欢可以关注我

本文是我真正意义上第一个c++程序,在此期间查阅了大量的资料,实现了一个complex类,实现了类模板,水平有限,如有错误请私信我。

本程序实现的功能:

1. +  -  *  /                              重载函数

2. +=  -=  *=  /=                      重载函数

3. 赋值重载函数

4.判断是否相等,是否不等的重载函数

5.取正,取负的重载函数

6.get_real ,get_imag  成员函数(成员函数互为友元)

7.友元函数,this指针的使用。

8.complex类的显示

因为程序比较简单,我都做了注释,就不详细展开,如有问题请私信我。

 

/*	本程序的注释代表仅代表个人理解,不一定完全正确,全是我亲自敲上去的,如有错误请联系我。  	*/


#ifndef __COMPLEX_B_H__
#define __COMPLEX_B_H__




template<typename T>		//定义模板 typename is T

class complex			/*	复数类	*/
{
public:
	complex(T r = 0, T i =0) : re(r), im(i)
	{
		;
	}
	complex& operator += (const complex&);	//成员函数
	complex& operator -= (const complex&);
	complex& operator *= (const complex&);
	complex& operator /= (const complex&);
	complex& operator = (const complex&);	//赋值运算符重载函数
	T real() const { return re; }
	T imag() const { return im; }

private:
	T re, im;

	friend complex& __doadd(complex *, const complex&);
	friend complex& __dosub(complex *, const complex&);
	friend complex& __domul(complex *, const complex&);
	friend complex& __dodiv(complex *, const complex&);
	friend complex& __doequ(complex*, const complex&);	//赋值实际运算函数

};



/************	全局函数声明区	***************/

double real(const complex<double>& x);	//取对象的实部(全局函数),不用在类外专门定义传参数
double imag(const complex<double>& x);	//取对象的虚部(全局函数),不用在类外专门定义传参数
inline complex<double>& __doequ(complex<double>* ths, const complex<double>& r);	//赋值实际运算函数

/**************************************************************************************************************************************************************/


/************			+= -= *= /=			***************************************************************************************************************************/
/*	复数 += 的实际运算函数	*/
inline complex<double>& 
__doadd(complex<double>* ths, const complex<double>& r)	
{
	ths->re += r.re;
	ths->im += r.im;
	return *ths;
}

/*	函数名 ooerator+=	运算符重载	*/
inline complex<double>&
complex<double>::operator +=(const complex<double>& r)
{
	return __doadd(this, r);
}

/*	复数 -= 的实际运算函数	*/
inline complex<double>&
__dosub(complex<double>* ths, const complex<double>& r)	
{
	ths->re -= r.re;
	ths->im -= r.im;
	return *ths;
}

/*	函数名 ooerator-=	*/
inline complex<double>&
complex<double>::operator -=(const complex<double>& r)
{
	return __dosub(this, r);
}

/*	复数 *= 的实际运算函数	*/
inline complex<double>&
__domul(complex<double>* ths, const complex<double>& r)
{
	ths->re *= r.re;
	ths->im *= r.im;
	return *ths;
}

/*	函数名 ooerator*=	*/
inline complex<double>&
complex<double>::operator *=(const complex<double>& r)
{
	return __domul(this, r);
}

/*	复数 /= 的实际运算函数	*/
inline complex<double>&
__dodiv(complex<double>* ths, const complex<double>& r)
{
	ths->re /= r.re;
	ths->im /= r.im;
	return *ths;
}

/*	函数名 ooerator/=	*/
inline complex<double>&
complex<double>::operator/=(const complex<double>& r)
{
	return __dodiv(this, r);
}

/*	复数	赋值(=)	实际运算函数	*/
inline complex<double>&
__doequ(complex<double>* ths, const complex<double>& r)
{
	ths->re = r.re;
	ths->im = r.im;
	return *ths;
}

/*	函数名 ooerator=	复数赋值(=)	*/
inline complex<double>&
complex<double>::operator=(const complex<double>& r)
{
	return __doequ(this, r);
}


/**************************************************************************************************************************************************************/

/*	函数名 ooerator+(复数,复数)	*/
inline complex<double>
operator+(const complex<double>& x, const complex<double>& y)
{
	return complex<double> (real(x) + real(y), imag(x) + imag(y));
}

/*	函数名 ooerator+(复数,double)	*/
inline complex<double>
operator+(const complex<double> x, double y)
{
	return complex<double>(real(x) + y, imag(x));
}

/*	函数名 ooerator+(double,复数)	*/
inline complex<double>
operator+(double x, const complex<double> y)
{
	return complex<double>(x + real(y), imag(y));
}



/*	函数名 ooerator-(复数,复数)	*/
inline complex<double>
operator-(const complex<double> x, const complex<double> y)
{
	return complex<double>(real(x) - real(y), imag(x) - imag(y));
}

/*	函数名 ooerator-(复数,double)	*/
inline complex<double>
operator-(const complex<double> x, double y)
{
	return complex<double>(real(x) - y, imag(x));
}

/*	函数名 ooerator-(double,复数)	*/
inline complex<double>
operator-(double x, const complex<double> y)
{
	return complex<double>(x - real(y), imag(y));
}



/*	函数名 ooerator*(复数,复数)	*/
inline complex<double>
operator*(const complex<double> x, const complex<double> y)
{
	return complex<double>(real(x) * real(y), imag(x) * imag(y));
}

/*	函数名 ooerator*(复数,double)	*/
inline complex<double>
operator*(const complex<double> x, double y)
{
	return complex<double>(real(x) * y, imag(x));
}

/*	函数名 ooerator*(double,复数)	*/
inline complex<double>
operator*(double x, const complex<double> y)
{
	return complex<double>(x * real(y), imag(y));
}


/*	函数名 ooerator/(复数,复数)	*/
inline complex<double>
operator/(const complex<double> x, const complex<double> y)
{
	return complex<double>(real(x) / real(y), imag(x) / imag(y));
}

/*	函数名 ooerator/(复数,double)	*/
inline complex<double>
operator/(const complex<double> x, double y)
{
	return complex<double>(real(x) / y, imag(x));
}

/*	函数名 ooerator/(double,复数)	*/
inline complex<double>
operator/(double x, const complex<double> y)
{
	return complex<double>(x / real(y), imag(y));
}



/*		函数名 ooerator==(复数,复数)	判断两个复数是否相等     重载		*/
inline bool
operator==(const complex<double>& x, const complex<double>& y)
{
	return  real(x) == real(y) && imag(x) == imag(y);
}

/*		函数名 ooerator==(复数,double)	判断两个复数是否相等     重载		*/
inline bool
operator==(const complex<double>& x, double y)
{
	return  real(x) == y && imag(x) == 0;
}

/*		函数名 ooerator==(double,复数)	判断两个复数是否相等     重载		*/
inline bool
operator==(double x, const complex<double>& y)
{
	return  x == real(y) && imag(y) == 0;
}



/*		函数名 ooerator!=(复数,复数)	判断两个复数是否不相等     重载		*/
inline bool 
operator != (const complex<double>& x, const complex<double>& y)
{
	return real(x) != real(y) || imag(x) != imag(y);
}

/*		函数名 ooerator!=(复数,double)	判断两个复数是否不相等     重载		*/
inline bool 
operator != (const complex<double>& x, double y)
{
	return real(x) != y || imag(x) != 0;
}

/*		函数名 ooerator!=(double,复数)	判断两个复数是否相等     重载		*/
inline bool operator != (double x, const complex<double>& y)
{
	return x != real(y) || imag(y) != 0;
}




/*	函数名 ooerator+(复数)	+正数	*/
inline complex<double>
operator+ (const complex<double>& x)
{
	return x;
}

/*	函数名 ooerator-(复数)	-复数	*/
inline complex<double>
operator- (const complex<double>& x)
{
	return complex<double>(-real(x), -imag(x));
}




inline double real(const complex<double>& x)	//取对象的实部(全局函数),不用在类外专门定义传参数
{
	return x.real();
}

inline double imag(const complex<double>& x)	//取对象的虚部(全局函数),不用在类外专门定义传参数
{
	return x.imag();
}


#endif

 

/*	本程序的注释代表仅代表个人理解,不一定完全正确,全是我亲自敲上去的,如有错误请联系我。  	*/


#include<iostream>
#include"complex_B.h"
#include<windows.h>

using namespace std;

/*	对 cout<< complex 类型的重载    ostream 是 cout的类	*/
ostream& operator<<(ostream& os, const complex<double>& x)
{

	return os << '(' << real(x) << ',' << imag(x) << ')';
	//return os << '(' << x.real() << ',' << x.imag() << ')';	//直接调用成员函数 real() 和 imag()  不必在
}

int main(void)
{
	complex<double> c1(2, 1);
	complex<double> c2(4, 0);
	complex<double> c3(-1, 1);
	complex<double> c4;		//c4未初始化(构造函数会赋初值0)

	cout << "c1=" << c1 << endl;
	cout << "c2=" << c2 << endl;
	cout << "c3=" << c3 << endl;
	cout << "c4=" << c4 << endl << endl;

	cout << "c1+c2" << c1 + c2 << endl;
	cout << "c1+4" << c1 + 4 << endl;
	cout << "2+c2" << 2 + c2 << endl << endl;

	cout << "c1-c2" << c1 - c2 << endl;
	cout << "c1-4" << c1 - 4 << endl;
	cout << "2-c2" << 2 - c2 << endl << endl;

	cout << "c1*c2" << c1 * c2 << endl;
	cout << "c1*4" << c1 * 4 << endl;
	cout << "2*c2" << 2 * c2 << endl << endl;

	cout << "c1/c2" << c1 / c2 << endl;
	cout << "c1/4" << c1 / 4 << endl;
	cout << "2/c2" << 2 / c2 << endl << endl;

	cout << "+c3" << +c3 << endl;	//测试运算符  +
	cout << "-c3" << -c3 << endl << endl;	//测试运算符  -

	c3 = complex<double>(1, -1);	// 运算符 =   (赋值)
	cout << "c3=(1,-1)" << endl;
	cout << "c3=" << c3 << endl;	// 测试运算符 = (赋值)		复数变量=临时复数
	c3 = c4;
	cout << "c3=c4" << endl;
	cout << "c3=" << c3 << endl << endl;	// 测试运算符 = (赋值)		复数变量1=复数变量2
	
	//cout << "(c1 += c2)="  << (c1 += c2) << endl;
/*	
	c1 += c2
	相当于c1.operator+=(c2)
	也就是c1调用的+=,所以this指向c1
*/
	//cout << "(c1 -= c2)="   << (c1 -= c2) << endl;
	//cout << "(c1 *= c2)=" << (c1 *= c2) << endl;
	cout << "(c1 /= c2)=" << (c1 /= c2) << endl << endl;

	cout << "c1==c2  =" << (c1 == c2) << endl;	// 测试运算符 ==  (两个复数是否相等判断)  复数变量1==复数变量2
	cout << "c1==2  =" << (c1 == 2) << endl;	// 测试运算符 ==  (两个复数是否相等判断)	复数变量==实数
	cout << "2==c2  =" << (4 == c2) << endl;	// 测试运算符 ==  (两个复数是否相等判断)	复数变量==实数

	system("pause");

	return 0;
}


 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值