Modern C++: The Good Parts/Anatomy of a global greeting
Let's examine our program from last time in more detail.
Code & explanation
[edit | edit source]#include <iostream>
#include <string>
These includes are required by the standard, so anyone who wants to offer a proper C++ compiler must provide them. Several includes are required that way, and the set of them is called the standard library.
<iostream>
allows us to usestd::cout
andstd::cin
. All of our programs will include it, because they're all console programs.<string>
is needed anytime you want a string (a value that represents text), which will also be all of our programs.
int main()
{
std::cout << "Hello, world!\n";
}
As mentioned before, int main
is required for all C++ programs. Ignore the parentheses and curly brackets for now; just know that they're necessary.
std::cout
and std::cin
are for console output and input, respectively. std
is the namespace, which basically means who gave them to us, and this one is the standard namespace - i.e., these are from the standard library. The "c" just means C++ inherited them from its "parent" language, C.
The "\n" at the end of the string is a line break or newline; it causes further text to be printed on an additional line.
Vocabulary
[edit | edit source]- standard library
- several useful pieces of code required by the C++ standard, shipped with all C++ compilers, all under the namespace
std
. - string
- a value that represents text.
- namespace
- a directory of names, or where to look for something by name.
- newline
- a character which separates two lines of text. Also called a line break. Syntax:
\n