其他分享
首页 > 其他分享> > 课堂练习

课堂练习

作者:互联网

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

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

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

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

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

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

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

  

  

 

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