Directory input and output operators Arithmetic Operators and Relational operators subscript operators self-addition and auto-subtraction operators members access operator 1 input and output operators 1.1 output operators 1.1.1 example copy Code # include <iostream> # include <string> using namespace std; class A {friend ostream & operator <(ostream & out, const A & a); public: A (const string & s = "", int v = 0 ): ss (s), val (v) {}// constructor with default parameter private: int val; string ss ;}; ostream & operator <(ostream & out, const A & a) {out <". ss: "<. ss <"" <". val: "<. val; r Eturn out;} int main () {A ("hello", 23); cout <a <endl; a B; cout <B <endl ;} copy code result 1.1.2 description 1) IO operations must be non-member functions. cause: the I/O operation interface returns ostream & object (only the left value is returned for continuous output, for example, cout <a <B ). The custom output operator should be similar to this operator. If it is defined as a member function (there is a first default parameter this, that is, pointing to its own pointer), the left operand can only be an object of this type, it cannot be done. For example, Sales_item; item <cout; is opposite to the general definition, so it can only be a non-member function. 2) to access private members of a specified class, declare the output operator as a member function in the class. 3) The first parameter must be a reference. Because I/O objects cannot be copied. Similarly, the return value must be a reference. 4) The first form parameter cannot be considered a const, because the value of the first form parameter is changed when it is written to the stream. 5) The second is the reference, which can avoid copying. The parameter can be const and can receive const objects and non-const objects. Otherwise, if it is not const, it can only receive non-coust objects. It is usually const. After all, it is only output and does not change the object. 1.2 input operator 1.2.1 sample copy Code # include <iostream> # include <string> using namespace std; class A {friend ostream & operator <(ostream & out, const A & a); friend istream & operator> (istream & in, A & a); public: A (const string & s = "", int v = 0 ): ss (s), val (v) {}private: int val; string ss ;}; ostream & operator <(ostream & out, const A &) {out <". ss: "<. ss <"" <". val: "<. val; return out;} istrea M & operator> (istream & in, A & a) {in>. ss>. val; if (in) {cout <"Input right" <endl;} else {cout <"in. fail: "<in. fail () <endl; cout <"input wrong! "<Endl;} return in;} int main () {A (" hello ", 23); cout <a <endl; a B; cin> B; cout <B <endl;} copy the code