Let's parse it:
std::cout is the qualified name of the standard output stream object, cout. This object is
automatically created whenever you #include <iostream>.
The overloaded insertion operator<< comes next. It takes an argument and passes it on tocout.
In this case, the argument is a literal string that we want to print on the screen.
The second argument,std::endl, is amanipulator that appends the newline character after the
string and forces buffer flushing.
As trivial as this program seems, it exposes some of the most important features of C++. For starters, C++ is
an object-oriented language. Therefore, it uses objects rather than functions to perform I/O. Secondly, the
standard libraries of C++, including<iostream>, are declared in the namespacestd (std stands for
standard. An exhaustive discussion about namespaces is available in the "Namespaces" section.). Thus,
instead of declaring standard functions, classes, and objects globally, C++ declares them in a dedicated
namespace\u2014thereby reducing the chances of clashing with user code and third-party libraries that happen
to use the same names.
The operator<< is particularly interesting. It behaves as a function that takes an argument, yet it looks like
the built-in left-shift operator. In reality, it's an overloaded operator. Operator overloading is another
fundamental concept of C++. An overloaded operator is a function whose name is an operator rather than a
string. You can override almost all the built-in operators of C++ (+, *, >, and so on) this way. Operator
overloading may sound revolutionary and even dangerous at first; after all, if programmers are free to
override the meaning of built-in operators, what's to stop them from defining operator+ as the subtraction
operator? Rest assured. C++ imposes certain restrictions on operator overloading. The combination of these
restrictions and good object-oriented practices guarantee that this feature makes code uniform and intuitive.
Consider the notion of a string. In C, assigning strings with the= operator is not allowed. By contrast, the
C++std::string class overloads this operator and enables you to perform direct assignments of
strings. For example:
s= "please fasten your seatbelt";
Without operator overloading, the snippet would look like this:
s.assign("please fasten your seatbelt");
When dealing with numeric libraries, the ability to overload operators (such as==,+,and-) for user-defined
types is particularly useful.
Overloading in C++ isn't limited to operators. You can also overload functions by giving two or morefunctions an identical name. The overloaded functions are distinguished by their parameter list. Forexample, a function that opens a file may have three different overloaded versions:
int file_open(int descriptor); // #1
int file_open(const char *pathname); // #2
int file_open(const string & pathname); // #3
The compiler determines which of the three overloaded versions is to be called by examining the function's
argument. For example:
file_open(file); // call file_open() #3
Перед камерой появился агент Смит. - Мы выстрелили в него новым «Джей-23», это нервно-паралитическое вещество продолжительного действия. Конечно, это чертовски болезненно, но нам нужно было его остановить. - Не волнуйтесь, мадам, - заверил второй агент. - С ним все будет в порядке.