complex类

#include <iostream>
#include <cmath>
using namespace std;
class complex{
    public:
        complex(double real1=0,double imaginary1=0);
        complex(complex &t);
        void add(complex a);
        void show();
        double mod();
    private:
        double real;
        double imaginary;
};
complex::complex(double real1,double imaginary1){ 
    real=real1;
    imaginary=imaginary1;
}
complex::complex(complex &t){
    real=t.real;
    imaginary=t.imaginary;
}
void complex::add(complex a){
    real=real+a.real;
    imaginary+=a.imaginary;
}
void complex::show(){
    cout<<real<<"+"<<imaginary<<"i"<<endl;
}
double complex::mod(){ 
    cout<<sqrt(real*real+imaginary*imaginary)<<endl;
}
int main()
{complex c1(3,5);
 complex c2(4.5);
 complex c3(c1);
 c1.add(c2);
 c1.show();
 c1.mod();
 return 0;
}

  

 

posted on 2019-04-02 21:08  沐尘河  阅读(75)  评论(1编辑  收藏  举报

导航