C++ Tutorial - 02 - Hello World Example


Uploaded by ProgrammingVideos on 17.10.2008

Transcript:
To begin developing in C++ create an empty document with a cpp file extension and open
it in the editor of your choice. In my case I'll be using Visual Studio and have included
the file in an new empty project. If you don't have Visual Studio but would like to follow
along in a similar environment you can download Visual C++ Express from Microsoft's website.
This is one of several free version of this program.
The first thing we'll add to the source file is the main method. This is the entry point
of the program and the code inside the braces is what will be executed when the program
runs.
Our first application will simply output the text "Hello World" to the screen. Before we
can do that however we need to include the iostream header. This header provides input
and output functionality for our program and is one of the standard libraries that comes
with all compilers. What the include directive does is effectively to replace the line with
everything in the specified header before the file is compiled into an executable.
If we're wondering what the header file contains we can take a look at it by right clicking
on its name and selecting "open document". Among other things we can here find out the
exact location of the header. With IOStream now included we gain access to several new
functions. These are all located in the standard namespace called "std" which we can go inside
using a double colon, also called the scope operator.
In Visual Studio the IntelliSense window will now automatically show up displaying what
the namespace contains. Among the members we here find the "cout" stream which is the
standard output stream in C++ that we can use to print text to a console window. It
uses two less-than signs (<<) known as the insertion operator to indicate what to output.
We can now write our string here delimited by double quotes. After the statement we place
a semicolon, which is used in C++ to mark the end of all expression statements.
If we want to make things a bit easier we can add a line specifying that we're using
the standard namespace (std). We then no longer have to prefix cout with the namespace since
it's now used by default.