#include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(double a= 0, double b= 0); Complex(Complex &t); void add(Complex x); void show(); double mod(); private: double real, imaginary; }; Complex::Complex(double a, double b) { real = a; imaginary = b; if (imaginary == 0) cout << real << endl; else cout << real << "+" << imaginary << "i" << endl; } Complex::Complex(Complex &t) { real = t.real; imaginary = t.imaginary; } void Complex::add(Complex x) { real = x.real + real; imaginary = x.imaginary + imaginary; } void Complex::show() { cout << real << "+" << imaginary<< "i" << endl; } double Complex::mod() { return sqrt(pow(real, 2) + pow(imaginary, 2)); } int main() { Complex c1(3, 5); Complex c2(4.5); Complex c3(c1); c1.add(c2); c1.show(); cout << c1.mod(); system("pause"); return 0; }
总结:在课堂上没有写void add(Complex x);void show();double mod();说明对程序还是不熟悉,做的时候还是要请教同学才能写出来。