Interesting overloads:
Experiment:
Move 1 left to the Cout object.
Rename test to console, at this point we want to let this cout represent the current command line:
Cout represents an instance of the command line, intended to print 1 to the command line.
We print in the overloaded function with printf:
The results of the operation are as follows:
You can see that 1 is printed out.
We can also reload the output characters:
The results of the operation are as follows:
To improve overloaded functions:
In this way, the 24th line achieves continuous transmission.
Define a line break as a constant:
The final Perfect program:
1#include <stdio.h>2 3 Const CharEndl ='\ n';4 5 classConsole6 {7 Public:8console&operator<< (inti)9 {Tenprintf"%d", i); One A return* This; - } -console&operator<< (Charc) the { -printf"%c", c); - - return* This; + } -console&operator<< (Const Char*s) + { Aprintf"%s", s); at - return* This; - } -console&operator<< (Doubled) - { -printf"%f", d); in - return* This; to } + }; - the Console cout; * $ intMain ()Panax Notoginseng { -cout <<1<<Endl; thecout <<"D.t.software"<<Endl; + A DoubleA =0.1; the Doubleb =0.2; + -cout << A + b <<Endl; $ $ return 0; -}
The results of the operation are as follows:
After overloading we can use the console instance to output instead of typing the formatted string every time.
C + + Standard library:
It is not a creative thing to reinvent the wheel, and it is more efficient to stand on the shoulders of giants to solve problems.
The above output stream objects are already implemented in the C + + standard library.
The C + + standard library has a sub-library that covers the functions of the C library.
The composition of the C + + compilation Environment:
The C library Submodule functionality in the C Language compatibility library and the C + + standard library is duplicated, but the header files they contain are not the same.
Common data structures for standard libraries:
The right-most column is the C-language sub-Library. The functionality provided in the library for compatibility with the C language compiler.
c libraries in the C + + standard library are compatible:
The #include <stdio.h> that we used in the program above is the C-compatible library, not the C library, and the C-compatible library is provided by the compiler vendor, not part of the standard library.
Example:
The header file contained here is neither the C + + standard library nor the C language library, it is a C-compatible library provided by C + + compiler vendors to promote their products.
We replaced the C-compatible library with the C-compatible module in the C + + standard library:
Then you have to open the Std namespace.
The results of the operation are as follows:
C + + Standard library:
The predecessors implemented the display and keyboard as objects in C + +.
Input and output in C + +:
1#include <iostream>2#include <cmath>3 4 using namespacestd;5 6 7 intMain ()8 {9cout <<"Hello world!"<<Endl;Ten One DoubleA =0; A Doubleb =0; - -cout <<"Input A:"; theCIN >>A; - -cout <<"Input B:"; -CIN >>b; + - Doublec = sqrt (A * a + b *b); + Acout <<"C ="<< C <<Endl; at - return 0; -}
The results of the operation are as follows:
Summary:
The 32nd lesson on C + + standard library