The C + + stream insert operator << and stream extraction operator ">>" are provided by C + + in the class library, and all C + + compilation systems provide the input stream class IStream and the output stream class Ostream in the class library. CIN and cout are objects of the IStream class and the Ostream class, respectively. "<<" and ">>" have been overloaded in the header file provided by the class library as the stream insert operator and stream extraction operator, which can be used to output and enter data of the C + + standard type. Therefore, all the "cout<<" and "cin>>" to the standard type of data input and output, you have to use # include head file included in this program file.
User-defined types of data cannot be exported and entered directly with "<<" and ">>". If you want to use them to output and enter data of the type that you declare, you must overload them.
The functions for the "<<" and ">>" overloads are as follows:
IStream & operator >> (IStream &, custom classes &);
Ostream & operator << (ostream &, custom classes &);
The first parameter of the function that overloads the operator ">>" and the type of the function must be the istream& type, and the second argument is the class to enter the operation. The first argument and function of a function that overloads "<<" must be of type ostream&, and the second argument is the class to which the output operation is performed. Therefore, you can only overload functions that are overloaded with ">>" and "<<" as friend functions or normal functions, and you cannot define them as member functions.
Overloaded stream insert operator ' << '
In the program, people want to be able to use the insert operator "<<" to output information about the objects of the class that the user declares themselves, which requires overloading the stream insert operator "<<".
[Example 10.7] on the basis of example 10.2, with the overloaded "<<" output complex numbers.
#include <iostream>using namespace Std;class Complex{public:complex () {real=0;imag=0;} Complex (double r,double i) {real=r;imag=i;} Complex operator + (Complex &c2); Operator "+" overloads for member functions friend ostream& operator << (ostream&,complex&); The operator "<<" overload is a friend function private:double real;double imag;}; Complex Complex::operator + (Complex &c2)//define operator "+" overloaded function {return Complex (REAL+C2.REAL,IMAG+C2.IMAG);} ostream& operator << (ostream& output,complex& C)//define operator "<<" overloaded function {output<< "(" << c.real<< "+" <<c.imag<< "I" <<endl;return output;} int main () {Complex C1 (2,4), C2 (6,10), C3;c3=c1+c2;cout<<c3;return 0;}
Note that when running in the Visual C + + 6.0 environment, you need to change the first line to # include <iostream.h>, and delete the 2nd line, or the compilation will not pass. The result of the operation is:
(8+14i)
You can see that the "<<" in the program not only outputs standard type data, but also outputs the user-defined class object after the operator "<<" overload. The value of the plural object C3 can be output in plural form with "cout<<c3". The form is intuitive, readable and easy to use.
Here are some instructions on how to implement operator overloading. The operator "<<" is overloaded in the program, and the parameter output in an operator overloaded function is a reference to the Ostream class object, and the parameter name output is arbitrary from the user. Analyze the last second line of the main function:
cout<<c3;
The left side of the operator "<<" is cout, previously mentioned cout is a Ostream class object. The right side of "<<" is C3, which is the complex class object. Since the overloaded function of the operator "<<" has been declared as a friend function of the complex class, the compilation system interprets "COUT<<C3" as
operator<< (Cout, C3)
That is, using cout and C3 as arguments, call the following operator<< function:
ostream& operator<< (ostream& output,complex& c)
{
output<< "(" <<c.real<< "+" <<c.imag<< "i)" <<endl;
return output;
}
When the function is called, the parameter output becomes a reference to the cout, and the formal parameter C becomes a reference to the C3. So the process of calling a function is equivalent to executing:
Cout<<″ (″<<c3.real<<″+″<<c3.imag<<″i) ″<<endl; return cout;
Note that the "<<" in the previous line is a predefined stream caret for C + + because the operand to the right is a string constant and a double type of data. Executes the cout statement to output the plural form of information. Then execute the return statement.
Think, what is the function of return output? The answer is that you can continuously insert information into the output stream. Output is an object of the Ostream class, which is a reference to the argument cout, that is, cout sends the address to output so that both share the same storage unit, or that output is an alias of cout. Therefore, return output is the return cout, returning the status of the output stream cout, that is, preserving the current state of the output stream.
Where can I return to? I was just doing it.
cout<<c3;
The return value of the known cout<<c3 is the current value of cout. If you have the following output:
cout<<c3<<c2;
Process first
Cout<<c3
That
(COUT<<C3) <<c2;
The result of execution (COUT<<C3) is a stream object cout with new content, so (COUT<<C3) <<C2 is equivalent to cout (new value) <<c2. The operator "<<" on the left is the Ostream class object cout, and the right is the complex class object C2, then the operator "<<" overloaded function is called again, then the C2 data is inserted into the output stream. It is now understandable why C + + specifies that the first parameter of the operator "<<" overload function and the type of the function must be a reference to the Ostream type, in order to return the current value of the cout for continuous output.
Readers are asked to be aware of the case where "<<" is a stream caret of standard type data, and under what circumstances "<<" is an overloaded stream caret. Such as
cout<<c3<<5<<endl;
The underscore is to call the overloaded stream caret, followed by two "<<" instead of the overloaded stream caret, because its right side is not a complex class object but a standard type of data that is handled with a predefined stream caret.
It is also important to note that in this program, the operator "<<" overload function is defined as a friend function in the complex class, so the overloaded operator can only be used when outputting the complex class object and is not valid for other types of objects. Such as
cout<<time1; Time1 is a time class object and cannot use overloaded operators for the complex class
Overloaded stream extraction operator ">>"
The function of the predefined operator ">>" of C + + is to extract data from an input stream, such as "cin>>i;" Represents the extraction of an integer from the input stream to the variable i (assuming that I is defined as an int type). The purpose of the overloaded stream extraction operator is to use ">>" to enter information for the object of the custom type.
[Example 10.8] on the basis of example 10.7, add the overloaded stream extraction operator ">>", with "cin>>" input complex number, with "cout<<" output complex number.
#include <iostream>using namespace std;class Complex{public:friend ostream& operator << (ostream&,complex&); //declaring overloaded operator "<<" Friend istream & operator >> (istream&,complex&); //declaring overloaded operator ">>" private:double real;double imag;};o stream& operator << (ostream& output,complex& c) //define overloaded operator " << "{output<<" ("<<c.real<<" + "<<c.imag<<" i) "; return output;} istream& operator >> (istream& input,complex& c) // Define overloaded operators ">>" {cout<< "input real part and imaginary part of Complex number: "; input>>c.real>>c.imag;return input;} Int main ( ) {complex c1,c2;cin>>c1>>c2;cout<< "c1=" <<c1<<endl;cout << "c2=" <<c2<<endl;return 0;}
The operating conditions are as follows:
Input real part and imaginary part of complex Number:3 6
Input real part and imaginary part of complex Number:4 10
C1= (3+6i)
C2= (4+10i)
The result of the above operation is undoubtedly correct, but not perfect. When the imaginary part of the input complex is positive, the result of the output is no problem, but if the imaginary part is negative, it is not ideal, please observe the output result.
Input real part and imaginary part of complex Number:3 6
Input real part and imaginary part of complex number:4-10
C1= (3+6i)
C2= (4+-10i)
According to the first debugging through, the final perfect principle, can make necessary changes to the program. Modify the overloaded operator ' << ' function as follows:
ostream& operator << (ostream& output,complex& C) {output<< "(" <<c.real;if (c.imag>=0 output<< "+";//The imaginary part is positive, add "+" number output<<c.imag<< "I" in front of the imaginary part <<endl; When the imaginary part is negative, the "+" sign is not added before the imaginary part to return output;}
In this way, the last behavior of the runtime output is c2= (4-10i).
As you can see, in C + +, operator overloading is important and useful. It makes the design of the class more colorful, expands the function and the scope of use of the class, makes the program easy to understand, easy to manipulate the object, it embodies for the sake of the user, user-friendly ideas. With operator overloading, once a class is declared, people can use their declared classes as if they were standard types. class declaration is often once and for all, with a good class, the user in the program does not have to define a number of member functions to complete certain operations and input and output functions, making the main function more simple and easy to read. Good operator overloading can embody the idea of object-oriented programming.
You can see the importance of using references (reference) in operator overloading. Using reference as a formal parameter of a function can be used in the process of calling a function not to pass the value of the actual situation combined, but by means of the way to make the formal parameters of the parameter alias, so do not generate temporary variables (copy of the argument), reducing the cost of time and space. In addition, if the return value of an overloaded function is a reference to an object, the return is not a constant, but rather a reference to the object represented, which can appear to the left of the assignment number as an lvalue (left value), can be assigned or participate in other operations (such as preserving the current value of the cout stream for continuous use "<< "Output). Use caution when using references, because modifying a reference is tantamount to modifying the object it represents.
C + + overloaded stream insert operators and stream extraction operators