By long tradition, the first example
is a program that prints the message “Hello,World!”.
#include <iostream>
/*
* This is the famous Hello, World! program,
* C++ version.
*/
int main(int argc, char **argv)
{
// Send greetings.
std::cout << "Hello, world!" << std::endl;
return 0;
}
Things to note:
- The /* ... */ multi-line comment is the original C comment.
- The // one-line comment was added for C++ and adopted back
into C.
- Semicolon is a statement terminator.
- This #include imports the standard I/O facility. It
is used to include external library facilities,
similar to an import in Java.
- Lines starting with # are called pre-processor directives.
More on these as we go.
- In this file, names from the standard library are prefixed
with std::. Std is called a namespace,
and all the names from the standard library are part of this
namespace. More on this as we go, also.
- Start at function main: C has functions without class
- The cout << notation is C++ notation for writing to
the standard output stream (console).
- In this example, we are writing a familiar-looking string
constant, and the library item std::endl to the
the console display, known as std::cout (“console output”).
- The endl terminates the output line. In Java terms, it
makes the print a writeln instead of a write.
- The return value from main goes to the O/S, which uses it as an
indication of success. This is the last time we'll mention it.
- Identifiers rules are familiar: an identifier
may contain only letters, numbers and underscore,
and start with a letter or underscore.
[more info]