【原創】C++_第一週_C++ complex類的實現(我的第一個c++程序)

如果喜歡可以關注我

本文是我真正意義上第一個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;
}


 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章