如果喜欢可以关注我
本文是我真正意义上第一个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 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& x); //取对象的实部(全局函数),不用在类外专门定义传参数
double imag(const complex& x); //取对象的虚部(全局函数),不用在类外专门定义传参数
inline complex& __doequ(complex* ths, const complex& r); //赋值实际运算函数
/**************************************************************************************************************************************************************/
/************ += -= *= /= ***************************************************************************************************************************/
/* 复数 += 的实际运算函数 */
inline complex&
__doadd(complex* ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}
/* 函数名 ooerator+= 运算符重载 */
inline complex&
complex::operator +=(const complex& r)
{
return __doadd(this, r);
}
/* 复数 -= 的实际运算函数 */
inline complex&
__dosub(complex* ths, const complex& r)
{
ths->re -= r.re;
ths->im -= r.im;
return *ths;
}
/* 函数名 ooerator-= */
inline complex&
complex::operator -=(const complex& r)
{
return __dosub(this, r);
}
/* 复数 *= 的实际运算函数 */
inline complex&
__domul(complex* ths, const complex& r)
{
ths->re *= r.re;
ths->im *= r.im;
return *ths;
}
/* 函数名 ooerator*= */
inline complex&
complex::operator *=(const complex& r)
{
return __domul(this, r);
}
/* 复数 /= 的实际运算函数 */
inline complex&
__dodiv(complex* ths, const complex& r)
{
ths->re /= r.re;
ths->im /= r.im;
return *ths;
}
/* 函数名 ooerator/= */
inline complex&
complex::operator/=(const complex& r)
{
return __dodiv(this, r);
}
/* 复数 赋值(=) 实际运算函数 */
inline complex&
__doequ(complex* ths, const complex& r)
{
ths->re = r.re;
ths->im = r.im;
return *ths;
}
/* 函数名 ooerator= 复数赋值(=) */
inline complex&
complex::operator=(const complex& r)
{
return __doequ(this, r);
}
/**************************************************************************************************************************************************************/
/* 函数名 ooerator+(复数,复数) */
inline complex
operator+(const complex& x, const complex& y)
{
return complex (real(x) + real(y), imag(x) + imag(y));
}
/* 函数名 ooerator+(复数,double) */
inline complex
operator+(const complex x, double y)
{
return complex(real(x) + y, imag(x));
}
/* 函数名 ooerator+(double,复数) */
inline complex
operator+(double x, const complex y)
{
return complex(x + real(y), imag(y));
}
/* 函数名 ooerator-(复数,复数) */
inline complex
operator-(const complex x, const complex y)
{
return complex(real(x) - real(y), imag(x) - imag(y));
}
/* 函数名 ooerator-(复数,double) */
inline complex
operator-(const complex x, double y)
{
return complex(real(x) - y, imag(x));
}
/* 函数名 ooerator-(double,复数) */
inline complex
operator-(double x, const complex y)
{
return complex(x - real(y), imag(y));
}
/* 函数名 ooerator*(复数,复数) */
inline complex
operator*(const complex x, const complex y)
{
return complex(real(x) * real(y), imag(x) * imag(y));
}
/* 函数名 ooerator*(复数,double) */
inline complex
operator*(const complex x, double y)
{
return complex(real(x) * y, imag(x));
}
/* 函数名 ooerator*(double,复数) */
inline complex
operator*(double x, const complex y)
{
return complex(x * real(y), imag(y));
}
/* 函数名 ooerator/(复数,复数) */
inline complex
operator/(const complex x, const complex y)
{
return complex(real(x) / real(y), imag(x) / imag(y));
}
/* 函数名 ooerator/(复数,double) */
inline complex
operator/(const complex x, double y)
{
return complex(real(x) / y, imag(x));
}
/* 函数名 ooerator/(double,复数) */
inline complex
operator/(double x, const complex y)
{
return complex(x / real(y), imag(y));
}
/* 函数名 ooerator==(复数,复数) 判断两个复数是否相等 重载 */
inline bool
operator==(const complex& x, const complex& y)
{
return real(x) == real(y) && imag(x) == imag(y);
}
/* 函数名 ooerator==(复数,double) 判断两个复数是否相等 重载 */
inline bool
operator==(const complex& x, double y)
{
return real(x) == y && imag(x) == 0;
}
/* 函数名 ooerator==(double,复数) 判断两个复数是否相等 重载 */
inline bool
operator==(double x, const complex& y)
{
return x == real(y) && imag(y) == 0;
}
/* 函数名 ooerator!=(复数,复数) 判断两个复数是否不相等 重载 */
inline bool
operator != (const complex& x, const complex& y)
{
return real(x) != real(y) || imag(x) != imag(y);
}
/* 函数名 ooerator!=(复数,double) 判断两个复数是否不相等 重载 */
inline bool
operator != (const complex& x, double y)
{
return real(x) != y || imag(x) != 0;
}
/* 函数名 ooerator!=(double,复数) 判断两个复数是否相等 重载 */
inline bool operator != (double x, const complex& y)
{
return x != real(y) || imag(y) != 0;
}
/* 函数名 ooerator+(复数) +正数 */
inline complex
operator+ (const complex& x)
{
return x;
}
/* 函数名 ooerator-(复数) -复数 */
inline complex
operator- (const complex& x)
{
return complex(-real(x), -imag(x));
}
inline double real(const complex& x) //取对象的实部(全局函数),不用在类外专门定义传参数
{
return x.real();
}
inline double imag(const complex& x) //取对象的虚部(全局函数),不用在类外专门定义传参数
{
return x.imag();
}
#endif
/* 本程序的注释代表仅代表个人理解,不一定完全正确,全是我亲自敲上去的,如有错误请联系我。 */
#include
#include"complex_B.h"
#include
using namespace std;
/* 对 cout<< complex 类型的重载 ostream 是 cout的类 */
ostream& operator<<(ostream& os, const complex& x)
{
return os << '(' << real(x) << ',' << imag(x) << ')';
//return os << '(' << x.real() << ',' << x.imag() << ')'; //直接调用成员函数 real() 和 imag() 不必在
}
int main(void)
{
complex c1(2, 1);
complex c2(4, 0);
complex c3(-1, 1);
complex 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(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;
}