C++ Complex Number Class with Operator Overloading
class Complex {
public: Complex() {}; Complex(double a, double b) { real = a; image = b; }; // 运算符重载 friend Complex operator + (Complex& a,Complex& b); friend Complex operator *(Complex& a, Complex& b); void print() { cout << real; if (image> 0)cout << '+'; if (image != 0)cout << image << 'i' << endl; } private: double real, image; };
Complex operator+(const Complex& a, const Complex& b) { return Complex(a.real + b.real, a.image + b.image);
}
Complex operator*(const Complex& a, const Complex& b) { return Complex(a.realb.real - a.imageb.image, a.realb.image + a.imageb.real); }
原文地址: https://www.cveoy.top/t/topic/nEvO 著作权归作者所有。请勿转载和采集!