9.2.1运算符成员画齦重载 ●●●● 下面以定义一个复数类,并重载+,-,*,/运算符来。 说明重载的过程。 ●●●● include <iostream.h>//9-2-1. cpp class Complex f private double real, Image;//复数的实部和虚部 public. Complexo i real image= 0.0; y Complex double r ireal = ri image =0.0Fy Complex(double r, double i) d real r image =ii 3i Complex operator +(const Complex &c); Complex operator-(const Complex &c; Complex operator k(const Complex &c);D ul
9.2.1 运算符成员函数重载 下面以定义一个复数类,并重载+,-, * ,/ 运算符来 说明重载的过程。 #include <iostream.h> // 9-2-1.cpp class Complex { private: double real, image; // 复数的实部和虚部 public: Complex() { real = image = 0.0; } Complex(double r) {real = r; image = 0.0; } Complex(double r, double i) { real = r; image = i; }; Complex operator + (const Complex &c); Complex operator - (const Complex &c); Complex operator * (const Complex &c);
9.2.1运算符成员函数重载 ●●●●● ●●●● Complex operator/(const Complex &ci ●●0 ●●● Complex operator-Oi ●●●● friend void print(const Complex &c)i Si inline Complex Complex; operator +(const Complex &c) d return Complex(real t Creal, image C image); inline Complex Complex operator-(const Complex &c) d return Complex(real -Creal, image-C image); inline Complex Complex; operator (const Complex &c) d return Complex (real C real -image * Cimage real C image image c real) <心画
9.2.1 运算符成员函数重载 Complex operator / (const Complex &c); Complex operator - (); friend void print(const Complex &c); }; inline Complex Complex::operator + (const Complex &c) { return Complex(real + c.real, image + c.image); } inline Complex Complex::operator - (const Complex &c) { return Complex(real - c.real, image - c.image); } inline Complex Complex::operator * (const Complex &c) { return Complex(real * c.real - image * c.image, real * c.image + image * c.real); }