1.普通类
//class headclass 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.模板类
//导入模板templateclass 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 c1(2.5, 1.5);// complex c2(2, 6);//}