<>C++ heavy load Implementation of input and output stream of array object !!!
heavy load
One for c++ learner , Necessary skills . Simply put, let some symbols have more important functions according to the different objects they use , So why do you say array overloading ? That's because multiple arrays are output at once , Isn't it a little uncomfortable ? Xiao Tang, take your time. This thing will be clear !
Input / output stream
You must have read the document iostream Right
What he meant was : Input / output stream
Then it comes out
Input stream
istream If you don't, you can cin Medium i, Connect
Output stream
ostream If you don't, you can cout Medium o, Connect
!!!!!!!!!!!!!!!! Here's the point
We usually go directly to class Inside public To define the overload of the input / output stream , Public ownership
public: friend istream & operator >> (istream&input, shuzu&A);// Define input array friend
ostream& operator << (ostream&output, shuzu&A);// Define output array
friend, Is a kind of good friend , You can directly access private array elements , That's your array
Then your input stream is not the same as mentioned above istream( Input stream ),ostream( Output stream ), This thing is actually equivalent to your function type, okay ?
Usually, the overload of our addition is int operator +( type ) Look at you carefully operator Previous type , No, just change it ,int—>istream/ostream
Because of your >>( Input stream ) <<( Output stream ) Symbols belong to input and output streams istream,ostream This type
well , Then you look back
(ostream&, shuzu&)
He is equivalent to using it directly ostream This class , References used , The following words are to determine his array object
ostream & operator << (ostream&ou, shuzu&A) { for (int i = 1; i < 5; i++) { for
(int j = 1; j < 5; j++) ou << setw(2)<<A.a[i][j]; cout << endl; } return ou; }
This is not very clear , We use one ou To name ( Can be changed ), One more thing you need to know at this time is ou Is equivalent to cout
Then arrays, right , So you should , It looks better when outputting, doesn't it , We're in too much trouble , You can use it directly #include Inside
setw(2) To fill in the space , There's a mistake here. It's yours cout And you ou There is no conflict here , Can be used directly
Come back at the last time , Because you are the object in the output stream , So you should return an output stream class , that is return ou
code :
#include <iostream> #include <iomanip> using namespace std; class shuzu {
public: friend istream & operator >> (istream&, shuzu&);// Define input array friend ostream
& operator << (ostream&, shuzu&);// Define output array private: int a[4][4]; }; istream &
operator >> (istream&in, shuzu&A) { for (int i = 1; i < 5; i++) for (int j = 1;
j< 5; j++) in >> A.a[i][j]; return in; } ostream & operator << (ostream&ou,
shuzu&A) { for (int i = 1; i < 5; i++) { for (int j = 1; j < 5; j++) ou << setw(
2)<<A.a[i][j]; cout << endl; } return ou; } int main() { shuzu A; cin>>A; cout<<
A; return 0; }
Technology