complex.h
#ifndef __COMPLEX__
#define __COMPLEX__
#include <cmath>
class ostream;
class complex;
complex&
__doapl (complex* ths, const complex& r);
class complex {
private:
double im, re;
friend complex& __doapl (complex* , const complex &);
public:
complex (double r = 0,double i = 0)
: re(r), im(i)
{ }
complex& operator += (const complex& );
double real () const { return re; }
double imaginary() const { return im; }
double foo(const complex& param) {
return param.re + param.im;
}
};
inline complex&
__doapl (complex* ths, const complex& r) {
ths->re += r.re;
ths->im += r.im;
return *ths;
}
inline complex&
complex::operator += (const complex& r) {
return __doapl(this, r);
}
inline double
imaginary(const complex& x){
return x.imaginary() ;
}
#endif
测试代码:
main.cpp
#include <iostream>
#include <windows.h>
#include "complex.h"
using namespace std;
int main(){
complex c1(1,2), c2(3,4);
__doapl(&c1, c2);
cout << "c1 = " << c1.real() << " + " << c1.imaginary() << "i" << endl;
cout << c1.foo(c1) << endl;
system("pause");
return 0;
}
|