complex类的实现

#include  
#include 
using namespace std;
class Complex{
    public:
        Complex(double real1,double imaginary1);
        Complex(Complex &x);
        Complex(double real2);
        void add(Complex y);
        void show();
        double mod();
    private:
        double real;
        double imaginary;
};

Complex::Complex(double real1,double imaginary1):real(real1),imaginary(imaginary1){
}

Complex::Complex(Complex &x):real(x.real),imaginary(x.imaginary){
}

Complex::Complex(double real2):real(real2){
}

void Complex::add(Complex y){
    real=real+y.real;
    imaginary=imaginary+y.imaginary;
}

void Complex::show(){
    cout<"+"<"i"<<endl;
}

double Complex::mod(){
    double z;
    z=sqrt(real*real+imaginary*imaginary);
    return z;
}

int main(){
    Complex c1(3,5);
    Complex c2(4.5);
    Complex c3(c1);
    c1.add(c2);
    c1.show();
    cout<<c1.mod();
    return 0;
}
View Code

complex类的实现_第1张图片

 

实验总结:看到题目时,并不能把类的框架给搭出来,对类这一块的内容还是没有掌握。复制构造函数在使用上还是有点困难,看书看了好久才依葫芦画瓢完成。

 

转载于:https://www.cnblogs.com/mzy-1229/p/10631883.html

你可能感兴趣的:(complex类的实现)