基于习题4-20要求,设计并实现Complex类:

#include<iostream>
#include<cmath>
using namespace std;

class Complex{
    public:
        Complex(float real=0,float imaginary=0);
        void add(Complex c);
        void show();
        double mod();
    private:
    float r,i; 
};

Complex::Complex(float real,float imaginary){
    r=real;
    i=imaginary;
}

void Complex::add(Complex c){
    r+=c.r;
    i+=c.i;
}

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

double Complex::mod(){
    double m;
    m=sqrt(r*r+i*i);
    return m;
}

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

 

我寻思着,大家咋都有个9.01388,原来还有个求模啊

posted on 2019-03-31 20:39  蓬得得蓬蓬  阅读(128)  评论(2编辑  收藏  举报