课堂练习Complex类

时间:2022-07-13 16:28:36

Complex类

 #include<iostream>
#include<cmath>
using namespace std;
class Complex {
public:
Complex(double nreal = 0.0, double nimaginary = 0.0);
Complex(Complex &c);
void add(Complex com);
void show();
double mod();
private:
double real;
double imaginary; };
Complex::Complex(double nreal, double nimaginary) :real(nreal), imaginary(nimaginary) {};
Complex::Complex(Complex &c) {
real = c.real;
imaginary = c.imaginary;
}
void Complex::add(Complex com) {
real += com.real;
imaginary += com.imaginary;
}
void Complex::show() {
if (imaginary == )
cout << real << endl;
else if (imaginary > )
cout << real << "+" << imaginary << "i" << endl;
else
cout << real << imaginary << "i" << endl;
}
double Complex::mod() {
return sqrt(real*real + imaginary * imaginary);
}
int main() {
Complex c1(,);
Complex c2=4.5;
Complex c3(c1);
c1.add(c2);
c1.show();
cout << c1.mod();
return ;
}

课堂练习Complex类