In C + +, the standard library itself has overloaded the left shift operator < < and the right shift operator > > respectively, so that they can be used for input and output of different data, but the object of input and output can only be the built-in data types of C + + (such as bool, int, double, etc.) and the class types contained in the standard library (such as string, complex, ofstream, ifstream, etc.).
If we define a new data type and need to use input and output operators to handle it, we must overload them.
In fact, the C + + standard library has provided a complex class, which can well support complex operations, but here we define a complex class to help you better understand.
Let complex input and output as simple as int, float and other basic types. Assuming num1 and num2 are complex numbers, the output form is:
cout<<num1<<num2<<endl;
The input form is:
cin>>num1>>num2;
cout is an object of ostream class and cin is an object of istream class. To achieve this goal, we must overload < < and > > in the form of global function (friend function). Otherwise, we must modify the class in the standard library, which is obviously not what we expect.
Overload input operator > >
We overload > > in the form of global function, so that it can read data of two double types and assign them to real part and virtual part of complex number respectively:
istream & operator>>(istream &in, complex &A){ in >> A.m_real >> A.m_imag; return in; }
Istream represents the input stream. cin is an object of istream class, but this object is defined in the standard library. The reason to return the reference of the istream class object is to read the complex number continuously and make the code writing more beautiful. For example:
complex c1, c2; cin>>c1>>c2;
If the reference is not returned, it can only be read one by one:
complex c1, c2; cin>>c1; cin>>c2;
In addition, the operator overloaded function uses the private member variable of the complex class, which must be declared as a friend function in the complex class:
friend istream & operator>>(istream & in , complex &a);
>>Operators can be used as follows:
complex c; cin>>c;
When input 1.45 2.34 A kind of After that, the two decimals become the real part and the virtual part of Object c respectively. In fact, this statement can be understood as:
operator<<(cin , c);
Overloaded output operator<<
We can also simulate the above form to overload the output operator > > so that it can output complex numbers. See the following code:
ostream & operator<<(ostream &out, complex &A){ out << A.m_real <<" + "<< A.m_imag <<" i "; return out; }
Ostream represents the output stream, and cout is an object of ostream class. Because the method of reference is adopted for parameter passing and the reference of object is also returned, the overloaded operator can achieve continuous output.
In order to directly access the private member variables of the complex class, it is also necessary to declare the function as a friend function of the complex class:
friend ostream & operator<<(ostream &out, complex &A);
Demonstration of examples
Combined with the overload of input and output operators, the complex class is re implemented:
#include <iostream> using namespace std; class complex{ public: complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ }; public: friend complex operator+(const complex & A, const complex & B); friend complex operator-(const complex & A, const complex & B); friend complex operator*(const complex & A, const complex & B); friend complex operator/(const complex & A, const complex & B); friend istream & operator>>(istream & in, complex & A); friend ostream & operator<<(ostream & out, complex & A); private: double m_real; //real part double m_imag; //imaginary part }; //Overloaded addition operator complex operator+(const complex & A, const complex &B){ complex C; C.m_real = A.m_real + B.m_real; C.m_imag = A.m_imag + B.m_imag; return C; } //Overloaded subtraction operator complex operator-(const complex & A, const complex &B){ complex C; C.m_real = A.m_real - B.m_real; C.m_imag = A.m_imag - B.m_imag; return C; } //Overloaded multiplication operator complex operator*(const complex & A, const complex &B){ complex C; C.m_real = A.m_real * B.m_real - A.m_imag * B.m_imag; C.m_imag = A.m_imag * B.m_real + A.m_real * B.m_imag; return C; } //Overloaded division operator complex operator/(const complex & A, const complex & B){ complex C; double square = A.m_real * A.m_real + A.m_imag * A.m_imag; C.m_real = (A.m_real * B.m_real + A.m_imag * B.m_imag)/square; C.m_imag = (A.m_imag * B.m_real - A.m_real * B.m_imag)/square; return C; } //Overloaded input operator istream & operator>>(istream & in, complex & A){ in >> A.m_real >> A.m_imag; return in; } //Overloaded output operator ostream & operator<<(ostream & out, complex & A){ out << A.m_real <<" + "<< A.m_imag <<" i ";; return out; } int main(){ complex c1, c2, c3; cin>>c1>>c2; c3 = c1 + c2; cout<<"c1 + c2 = "<<c3<<endl; c3 = c1 - c2; cout<<"c1 - c2 = "<<c3<<endl; c3 = c1 * c2; cout<<"c1 * c2 = "<<c3<<endl; c3 = c1 / c2; cout<<"c1 / c2 = "<<c3<<endl; return 0; }
Operation result:
2.4 3.6↙ 4.8 1.7↙ c1 + c2 = 7.2 + 5.3 i c1 - c2 = -2.4 + 1.9 i c1 * c2 = 5.4 + 21.36 i c1 / c2 = 0.942308 + 0.705128 i