其他分享
首页 > 其他分享> > 课堂小练习: 设计、定义并实现Complex类

课堂小练习: 设计、定义并实现Complex类

作者:互联网

#include<iostream>
#include<cmath>
using namespace std;
class Complex{
    public:
        Complex(double x,double y);
        Complex(Complex &a);
        void add(Complex a);
        void show();
        double mod();
    private:
        double real;
        double imaginary;
};

Complex::Complex(double x,double y=0){
    real=x;
    imaginary=y;
}

Complex::Complex(Complex &a){
    real=a.real;
    imaginary=a.imaginary;
}

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

void Complex::show() {
    cout<<real<<'+'<<imaginary<<'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);
    c1.add(c2);
    c1.show();
    cout<<c1.mod()<<endl;
    return 0;
}
View Code

小结:程序运行的时候一直有错误后来问了同学是Complex c2=4.5;出现了问题,应该是Complex c2(4.5);运用到了后面的知识。

 

实验二评论:

https://www.cnblogs.com/csl-40/p/10583521.html

https://www.cnblogs.com/Yyaoyyy/p/10594051.html

https://www.cnblogs.com/q1831726125/p/10597633.html

标签:real,www,double,void,练习,Complex,课堂,imaginary
来源: https://www.cnblogs.com/fifi1224/p/10639198.html