1.普通类
//class head
class complex
//class body {}
{
public:
complex(double r=0, double i)
:re(r), im(i)
{}//构造函数
complex& operator += (const complex&);
double real() const { return re; }//获取实部的函数
double imag() const { return im; }//获取虚部的函数
private:
double re, im;
friend complex& __doapl(complex*, const complex&);
};
2.模板类
//导入模板
template<typename T>
class complex
{
public:
complex(T r = 0, T i = 0)
:re(r), im(i)
{}
complex& operator += (const complex&);
T real() const { return re; }
T imag() const { return im; }
private:
T re, im;
friend complex& __doapl(complex*, const complex&);
};
//调用时需要制定类型<>
//{
// complex<double> c1(2.5, 1.5);
// complex<int> c2(2, 6);
//}