C ++ Getting Started (1) - Analysis Cout

zhaozj2021-02-11  180

Like most friends, I have encountered COUT to see the first C program - classic "Hello, World!", One of the best programs I have prepared by I have prepared (^ _ ^), It is probably like this:

#include

Using namespace std;

INT main () {cout << "Hello, World!" << endl; return 0;

Since I have learned C, the other part of this code is still "normal" in my opinion, but Cout is very unique: neither function, it seems that C specified like IF, FOR has special Syntax "statement". Since it is just a preliminary introduction, then the book is just a simple COUT is a "standard input output" object in C ... this is really a very deep term. This hasn't finished, and then I met CIN ... I don't know if I don't know how to use them. I have been sincere and fearful. I want to escape from the concise printf (), after all, I can say: I am calling a function. That has a long skewers <<, >>'s gain, what is going on? I always want to treat them as a keyword, but it is not, and actually uses C language "to do", huh! But printf () starts with more people, I started to criticize my programs "C language traces" ... Later, as learned, finally understand the ghost tricks of COUT / CIN / CERR / ...: Those East East is not a "kidney", in fact, it is a function call, but this function is somewhat special, and it is an operator overload, which is exactly (the following is also COUT as an example) is overloaded "<<" Operator. We now let it show the original face of the function, please see the Hello World!'S equivalent versions:

#include

Using namespace std;

INT main () {cout.operator << ("Hello, World!"); cout.operator << (endl); return 0;}

Compilation operation, the result is no longer with the classic version. The above program should be more likely to understand: COUT is an object of an iostream class, which has a member operator function Operator <<, will output Dongdong to the output device (generally on the screen) each time the call. Well, here there is a question: Why is the function operator << can accept different types of data, such as integer, floating point, strings, even pointers, etc. I think you have already guess it now, it's right, it is overloaded with operators. The operator function is basically no different from the general function, which can be overloaded any. Standard library designers have already customized iostream :: Operator << For the overloaded version of various C basic data types, this has enabled our beginners to enjoy Cout << "Hello, World ! "<< Endl; simple - etc, this line is connected by two <<" Hello, World "and" endl "operator, then our second Edition Hello, World! It seems to be written : Cout.operator << ("Hello, World!"). Operator << (endl); "strong equivalent". Can you write this way? Confirm to the compiler ... OK, No quoblem! Well, we have basically seen the essence of COUT, now you may wish to move your hands, you will realize a simplified version of a COUT (Lite), in order to distinguish, we name the cout object named by our design, Myout object belongs Myoutstream. What we have to do is to overload a series of different types of Operator << operators for MyoutStream classes, simply, here we only have overloaded for integer (int) and string type (char *). In order to indicate the relationship with the iostream, we no longer use the header file iostream, and the Printf function in the old stdio is used in an old STDIO, the program is simple, including the complete main function, the same as follows:

#include // In C and some old C is stdio.h, the new standard In order to distinguish the standard library //'s header file and the user's header file, it is recommended to use the version without extension //, for Original C library, no extension timer file name front to add CCLASS myoutstream {public: const myoutstream & operator << (int value) const; // overloaded constallStream & Operator << (char * STR) Const; // Overloaded on string};

Const myoutstream & myoutstream :: Operator << (int value) const {printf ("% d", value); return * this; // Note this return ...}

Const myoutstream & myoutstream :: Operator << (char * STR) const {printf ("% s", str); return * this; //, here, notice ...} myoutstream myout; // CAEACKEA Global object myout

INT main () {Int a = 2003; char * mystr = "Hello, World!"; myout << mystr << a << "/ n"; return 0;}

Our already Myout has begun to work, you can work for us. The comments in the program indicate that we should pay special attention to the two: ie the Operator << function is executed, always returns a reference to its own, the output has been completed, why do you want more? I still remember that a little strange cout.operator << ("Hello, World!"). Operator << (endl)? It can achieve we can write COUT << "Hello, World! << Endl; not COUT <<" Hello, World! "; Cout << Endl; why can it connect? We analyze: According to the order of execution, the system first calls Cout.operator << ("Hello, World!"), Then? Then Cout.operator << will return it itself, that is, the last line of the function will appear similar to Return * this, so Cout.operator << ("Hello, World!") Is returned to Cout Then, then it is followed by .Operator << (endl), which is equivalent to cout.operator << (endl) - then will carry out the next output, if there are many << operators, calling Will it be going on ... Wow, is it very clever? Now you understand our myoutstream :: Operator << The last row of the mystery! Note that the most exciting line in the main function: myout << mystr << a << "/ n"; we know that the last "/ n" can achieve a wrap, but we are tutorial when using C Always intentionally, let us use endl, both seem to be the same - what is the mystery? In the book, the book says ENDL is a manipulator, which not only implements the wrap operation, but also refreshes the output buffer. What does that mean? It turns out that the data is not immediately transmitted to the output device, but the first buffer is entered, and when the appropriate timing (such as the device is idle), it can be incorporated by the buffer, or forced to refresh by the manipulator Flush: Cout << "Hello, World!" << "Flush The screen !!!" << flush; so when the program executes to operator << (flash), there is a possibility that the string data in front is still in the buffer Not displayed on the screen, but after executing Operator << (flash), the program will force all the data of the buffer to the output device and empty it. And the manipulator ENDL is equivalent to the shorter version of << "/ n" << Flush, which first outputs a newline character, and then realize the refresh of the buffer. It is probably because the general output is ended in the end of the wrap, and the end is a habit of refreshing. It is convenient to combine the two into ENDL.

转载请注明原文地址:https://www.9cbs.com/read-5155.html

New Post(0)