声明复数类complx,该类中有两个私有变量real,image分别表示一个复数的实部和虚部,
为complex类添加适当的构造类函数,并使用友元函数add实现复数加法
很基础的类定义(若是C++的)
class complex
{
public:
friend complex add(complex &x,complex &y);
complex()
{
real = 0.0;
imag = 0.0;
}
complex(float x, float y)
{
real = x;
imag = y;
}
private:
float real;
float imag;
};
complex add(complex &x,complex &y)
{
return complex( x.real+y.real,x.imag+y.imag);
}
热门标签: