Learn C++ - C++ Hello World






C++ Comments

The double slash // introduces a C++ comment.

A comment is a remark from the programmer to the reader.

The compiler ignores comments.

C++ comments run from the // to the end of the line.

A comment can be on its own line, or it can be on the same line as code.

C++ also recognizes C comments, which are enclosed between /* and */ symbols:

#include <iostream> /* a C-style comment */ 

Because the C-style comment is terminated by */ rather than by the end of a line, you can spread it over more than one line.

You can use either or both styles in your programs.





C++ Preprocessor

If your program is to use the usual C++ input or output facilities, you provide these two lines:

#include <iostream> 
using namespace std; 

The following code directive causes the preprocessor to add the contents of the iostream file to your program.

#include <iostream>    // a PREPROCESSOR directive 

Header Filenames

Files such as iostream are called include files or header files.

The following table summarizes the naming conventions for header files.

HeaderConventionExampleComments
C styleEnds in .hmath.hUsable by C and C++ programs
C++ new styleNo extensioniostreamUsable by C++ programs, uses namespace std




Namespaces

If you use iostream instead of iostream.h, you should use the following namespace directive to make the definitions in iostream available to your program:

using namespace std;

This is called a using directive.

Namespace support is a C++ feature designed to simplify the writing of large programs.

The cout variable used for output and defined in iostream is really called std::cout and that endl is really std::endl.

Thus, you can omit the using directive and, instead, code in the following style:

std::cout << "this is a test."; 
std::cout << std::endl; 

The following line means you can use names defined in the std namespace without using the std:: prefix:

using namespace std;

This using directive makes all the names in the std namespace available.

The preferred approaches are to use the std:: qualifier or to use something called a using declaration to make just particular names available:

using std::cout;   // make cout available 
using std::endl;   // make endl available 
using std::cin;    // make cin available 

If you use these directives instead of the following, you can use cin and cout without attaching std:: to them:

using namespace std;  // lazy approach, all names available 

C++ Output with cout

The code above uses the following C++ statement:

cout << "this is a test.";

The part enclosed within the double quotation marks is the message to print.

If string represents a string, you can do the following to display it:

cout << string;

The Manipulator endl

endl is a special C++ notation that represents the new line.

cout << endl;

Inserting endl into the output stream causes the screen cursor to move to the beginning of the next line.

Special notations like endl that have particular meanings to cout are dubbed manipulators.

Like cout, endl is defined in the iostream header file and is part of the std namespace.

cout does not move automatically to the next line when it prints a string.

C++ has another, more ancient, way to indicate a new line in output-the C notation \n:

cout << "What's next?\n";    // \n means start a new line

If you are displaying a string, you need less typing to include the newline as part of the string than to tag an endl onto the end.