complex类

来源:百度文库 编辑:神马文学网 时间:2024/04/28 15:41:54
//主文件m.cpp
#include
#include "complex.h"
using namespace std;
void main()
{
double f;
complex c1(1,1);
f=c1.abs();
cout<}
//complex.h
#if !defined complex_h
#define complex_h
class complex
{
double real;
double image;
public:
complex();
complex(double,double);
complex(const complex&);//拷贝构造函数
double abs();
};                                                                                                                                                                             #endif
//complex.cpp
#include "complex.h"
#include
complex::complex()
{
real=0;
image=0;
}
complex::complex(double r,double i)
{
real=r;
image=i;
}
complex::complex(const complex&c)
{
real=c.real;
image=c.image;
}
double complex::abs()
{
return sqrt(real*real+image*image);
}