定义一个复数类Complex

#include
#include
using namespace std;
class Complex{
public:
    Complex(double real0=0,double imaginary0=0);
    Complex(Complex &p);
    void add(Complex a);
    void show();
    double mod();
private:
    double real;
    double imaginary;
};
Complex::Complex(double real0,double imaginary0)
{
    real=real0;
    imaginary=imaginary0;
}
Complex::Complex(Complex &p)
{
    real=p.real;
    imaginary=p.imaginary;
}
void Complex::add(Complex b)
{
    real=real+b.real;
    imaginary=b.imaginary;
}
void Complex::show()
{
    cout<"+"<"i"<<endl;
}
double Complex::mod()
{
    double x;
    x=sqrt(real*real+imaginary*imaginary);
    return x;
}
int main()
{Complex c1(3,5); 
 Complex c2(4.5);
 Complex c3(c1);
 c3.show();
 cout<endl;
 return 0;
}
Complex

定义一个复数类Complex_第1张图片

刚开始没定义初始值,结果怎么都出不来,请同学帮我看了一下,问题就解决了。反思了一下自己,基本知识还是没掌握牢固。还是要打好基础,一步一步来。

转载于:https://www.cnblogs.com/lyc1103/p/10633319.html

你可能感兴趣的:(定义一个复数类Complex)