Implementation of overloaded C++ stream insert and stream extract operators
- 2020-06-19 11:27:01
- OfStack
01 flow into
<
<
Operator overload
C++ The most common way to output content:
std::cout << 1 <<"hello";
Question:
So why does this statement work? What is cout?" < < "What about the operator on cout?The reason:
In fact, cout is an object of the ostream class defined in the iostream header file. " < < "Can be used on cout because, in the ostream class pair" < < "Has been reloaded.For std: : cout < < 1 < < "hello"; This statement may overload a member function of the ostream class as follows:
ostream & ostream::operator<<(int n)
{
.... // The output n Integer code
return *this;
}
ostream & ostream::operator<<(const char * s)
{
.... // The output s String code
return *this;
}
std::cout
<
<
1; Statement equivalent to ES42en.operator
<
<
(1);
std::cout
<
<
"hello"; Statement, equivalent to cout.operator
<
<
("hello");
std::cout
<
<
1
<
<
"hello"; Statement, equivalent to (cout.operator
<
<
(1) ).operator
<
<
("hello");
02 flow into
<
<
An example of operator overloading
Assuming we want to print out the contents of an object, we can override the flow insertion of the ostream class < < Operator.
Here's an example of the CStudent class:
class CStudent // Students in class
{
public:
// The constructor
CStudent(int id = 0, int age = 0, string name = ""):m_id(id), m_age(age), m_name(name) { }
// Declare the function as a friendly function
// The goal is to make the function accessible CStudent A private member variable of a class
friend ostream & operator<<(ostream & o, const CStudent & s);
private:
int m_age; // age
int m_id; // ID No.
string m_name; // The name
};
// overloading ostream Flow insertion of an object << Operator function
// The goal is to make it printable CStudent Object information
ostream & operator<<(ostream & o, const CStudent & s)
{
o << s.m_id << "," << s.m_age << "," << s.m_name;
return o;
}
int main()
{
CStudent stu(1, 20, " Xiao Lin coding");
std::cout << stu ; // The output std All information about the object
return 0;
}
Output results:
[
1, 20, xiao Lin coding
Note that it is ostream & operator < < (ostream & o, const CStudent & The s) function is global, so the first argument to the function must be passed in to an ostream object, and the CStudent class needs to declare the function as a friendly function so that the function can access private member variables of the CStudent class.
03 flow extraction
>
>
An example of operator overloading
Again, with the CStudent class as an example, assuming that the object is initialized with input from the keyboard, we can override the stream extraction of the istream class > > Operator.
class CStudent // Students in class
{
public:
// The constructor
CStudent(int id = 0, int age = 0, string name = ""):m_id(id), m_age(age), m_name(name) { }
// Declare the function as a friendly function
// The goal is to make the function accessible CStudent A private member variable of a class
friend ostream & operator<<(ostream & o, const CStudent & s);
// Declare the function as a friendly function
// The goal is to make the function give CStudent The private member variable of the class is assigned
friend istream & operator>>(istream & is, CStudent & s);
private:
int m_age; // age
int m_id; // ID No.
string m_name; // The name
};
// overloading ostream Flow insertion of an object << Operator function
// The goal is to make it printable CStudent Object information
ostream & operator<<(ostream & o, const CStudent & s)
{
o << s.m_id << "," << s.m_age << "," << s.m_name;
return o;
}
// overloading istream Flow extraction of an object >> Operator function
// The goal is to initialize CStudent Object content
istream & operator>>(istream & is, CStudent & stu)
{
string inputStr;
is >> inputStr;
int pos = inputStr.find(",", 0); // Find the position where the comma first appeared
string tmpStr = inputStr.substr(0, pos); // The interception from 0 to pos String of positions
stu.id = atoi(tmpStr.c_str()); // atoi Can be char* Type of content to int type
int pos2 = inputStr.find(",", pos + 1); // Find the first 2 The position of the secondary comma
tmpStr = inputStr.substr(pos + 1, pos2 - pos -1); // Take out the age The value of the
stu.age = atoi(tmpStr.c_str()); // atoi Can be char* Type of content to int type
tmpStr = inputStr.substr(pos2 + 1, inputStr.length() - pos2 - 1); // Take out the name The value of the
stu.name = tmpStr;
return is;
}
int main()
{
CStudent stu;
// Initialize the input information stu object
cin << stu;
// The output std Object information
cout >> stu;
return 0;
}
Input and output contents:
[
// Input:
1, 20, xiao Lin coding
// Output:
1, 20, xiao Lin coding
04 summary
To insert a stream < < Operator and stream extraction > > The operator can target a custom object, so we need to override the ostream class for that object < < Operator and istream > > Operator, and can only be overloaded as a global function, and then in the CStudent class need to declare the above two overloaded functions as a friendly function, so that the two overloaded functions can access and assign private member functions in the CStudent class.