Operator Overloading of sdut 4-1 complex classes
4-1 operator overloading of complex classes Time Limit: 1000 MS Memory limit: 65536 K Description
You can use this exercise to master the member Operator Overloading and friend Operator overloading.
It is required to define a plural class, And to reload addition and subtraction operators to meet the requirements for plural operations. The reload insertion operator (<) is used to output a plural value.
Input
The value of the object must be initialized when an object is created in the main function.
Output
There are 4 rows of output data, which respectively represent the values of a and B AND THEIR SUM and Difference values.
Sample Input
None
Sample output
a=3.2+4.5ib=8.9+5.6ia+b=12.1+10.1ia-b=-5.7-1.1i
Prompt Source
# Include
Using namespace std; class Complex // defines the Complex class {private: double real; double imag; public: Complex () // default value: 0 {real = 0; imag = 0 ;} complex (double a, double B) // value assignment function {real = a; imag = B;} Complex operator + (Complex & c ); // declare the overload operator "+" Complex operator-(Complex & c); // declare the overload operator "-" friend ostream & operator <(ostream &, Complex &); // declare the operator "<" to overload as a friend function}; Complex: operator + (Complex & c) // define the overload operator "+" function {Complex d; d. real = real + c. real; d. imag = imag + c. imag; return d;} Complex: operator-(Complex & c) // defines the function {Complex d; d. real = real-c.real; d. imag = imag-c.imag; return d;} ostream & operator <(ostream & output, Complex & c) // defines the operator '<' overload function {output <
= 0) output <"+"; output <