Operator Overloading is a tool used in class definition. The following is an example of adding or subtracting a complex number. The operation between two classes is achieved by reloading "+, that is, the addition and subtraction of two class objects;
# Include <iostream> using namespace STD; Class Complex {PRIVATE: Double real; double imag; public: complex (double r = 0.0, double I = 0.0) {real = R; imag = I;} complex operator + (complex C2); complex operator-(complex C2); void disp () {cout <"real =" <real <"; imag = "<imag <Endl ;}; complex: Operator + (complex C2) {Complex C; C. real = real + c2.real; C. imag = imag + c2.imag; return C;} complex: Operator-(complex C2) {Complex C; C. real = real-c2.real; C. imag = imag-c2.imag; return C;} int main () {complex C1 (5, 4), C2 (2, 10), C3; cout <"C1 ="; c1.disp (); cout <"C2 ="; c2.disp (); C3 = c1-c2; cout <"C3 = c1-c2"; c3.disp (); C3 = C1 + C2; cout <"C3 = C1 + C2"; c3.disp (); Return 0 ;}