
In C++, the standard library has already overloaded the left shift operator <span><<</span> and the right shift operator <span>>></span> to allow for input and output of different data types. However, the objects for input and output can only be C++ built-in data types (such as bool, int, double, etc.) and class types included in the standard library (such as string, complex, ofstream, ifstream, etc.).If we define a new data type and want to use input and output operators, we must overload them. This section will use the previous complex class as an example to demonstrate the overloading of input and output operators.
Actually, the C++ standard library already provides a complex class that supports complex number operations well, but here we define our own complex class solely for teaching demonstration purposes.
The goal of this section is to make the input and output of complex numbers as simple as basic types like int and float. Suppose num1 and num2 are complex numbers, then the output format would be:
cout<<num1<<num2<<endl;
The input format would be:
cin>>num1>>num2;
cout is an object of the ostream class, and cin is an object of the istream class. To achieve this goal, we must overload <span><<</span> and <span>>></span> as global functions (friend functions). Otherwise, we would have to modify the classes in the standard library, which is clearly not what we want.

Overloading the Input Operator >>
Next, we will overload the <span>>></span> operator as a global function so that it can read two double type values and assign them to the real and imaginary parts of the complex number:
istream & operator>>(istream &in, complex &A){
in >> A.m_real >> A.m_imag;
return in;
}
istream represents the input stream, and cin is an object of the istream class, which is defined in the standard library. The reason for returning a reference to the istream class object is to allow for continuous reading of complex numbers, making the code more elegant, for example:
complex c1, c2;cin>>c1>>c2;
If we do not return a reference, we would have to read them one by one:
complex c1, c2;cin>>c1;cin>>c2;
Additionally, since the operator overload function uses the private member variables of the complex class, we must declare this function as a friend function in the complex class:
friend istream & operator>>(istream & in , complex &a);
<span>>></span> operator can be used as follows:
complex c;cin>>c;
When inputting <span>1.45 2.34↙</span>, these two decimals become the real and imaginary parts of object c respectively.<span>cin>> c;</span> can actually be understood as:
operator<<(cin , c);

Overloading the Output Operator <<
Similarly, we can also overload the output operator <span><<</span> in a similar manner to output complex numbers. Please see the code below:
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 the ostream class. Since we use references for parameter passing and also return a reference to the object, the overloaded operator can achieve continuous output.
To directly access the private member variables of the complex class, we also need to declare this function as a friend function of the complex class:
friend ostream & operator<<(ostream &out, complex &A);

Comprehensive Demonstration
Combining the overloaded input and output operators, we can re-implement the complex class:
#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
};
// Overload 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;
}
// Overload 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;
}
// Overload 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;
}
// Overload 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;
}
// Overload input operator
istream & operator>>(istream & in, complex & A){
in >> A.m_real >> A.m_imag;
return in;
}
// Overload 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;
}
Running results:2.4 3.6↙4.8 1.7↙c1 + c2 = 7.2 + 5.3 ic1 – c2 = -2.4 + 1.9 ic1 * c2 = 5.4 + 21.36 ic1 / c2 = 0.942308 + 0.705128 i
The editor has compiled a set of C language learning materials. If you need them, you can DM @C Language Learning Alliance and reply to receive the materials. Everyone is welcome to follow, and I will share relevant technical articles in a timely manner. Your follow and likes are very important to me, thank you all for clicking to follow~
*Disclaimer:This article is compiled from the internet, and the copyright belongs to the original author. If there is any error in the source information or infringement of rights, please contact us for deletion or authorization matters.