1. Hello World
Choosing an IDE
To begin developing in C++ you need a text editor and a C++ compiler. You can get both at the same time by installing an Integrated Development Environment (IDE) that includes support for C++. A good choice is Microsoft's Visual Studio Community Edition, which is a free version of Visual Studio that is available from Microsofts website. This IDE has full support for the C++14 standard and includes most features of C++17 as of the 2017 version.
Two other popular cross-platform IDEs include NetBeans and Eclipse CDT. Alternatively, you can develop using a simple text editor such as Notepad, although this is less convenient than using an IDE. If you choose to use a simple text editor, just create an empty document with a .cpp file extension and open it in the editor of your choice.
Creating a Project
After installing Visual Studio, go ahead and launch the program. You then need to create a project, which will manage the C++ source files and other resources. Go to File New Project in Visual Studio to display the New Project window. From there, select the Visual C++ template type in the left frame. Then select the Empty Project template in the right frame. At the bottom of the window, you can configure the name and location of the project. When you are finished, click the OK button to let the wizard create your empty project.
Adding a Source File
You have now created a C++ project. In the Solution Explorer pane (choose View Solution Explorer), you can see that the project consists of three empty folders: Header Files, Resource Files, and Source Files. Right-click on the Source Files folder and choose Add New Item. From the Add New Item dialog box, choose the C++ file (.cpp) template. Give this source file the name MyApp and click the Add button. An empty .cpp file will now be added to your project and opened for you.
Selecting Language Standard
To enable the latest features of the C++ language outlined in this book it is necessary to manually change the language standard setting for your project. You can do this by first going to Project Properties to bring up the Property pages. From there, navigate to Configuration Properties C/C++ Language C++ Language Standard. Select the ISO C++17 standard from the drop-down list. Click OK and the project will now be configured to compile according to the C++17 language standard.
Hello World
The first thing to add to the source file is the main() function. This is the entry point of the program, and the code inside of the curly brackets is executed when the program runs. The brackets, along with their content, is referred to as a code block, or just a block.
int main() {}
The first application will simply output the text "Hello World" to the screen. Before this can be done the iostream header needs to be included. This header provides input and output functionality for the program, and it is one of the standard library files that comes with all C++ compilers. The #include directive effectively replaces the line with everything in the specified header before the file is compiled into an executable.
With iostream included, you gain access to several new functions. These are all located in the standard namespace called std , which you can examine by using a double colon, also called the scope resolution operator ( :: ) . After typing this in Visual Studio, the IntelliSense window will automatically open, displaying the namespace contents. Among the members you find the cout stream, which is the standard output stream in C++ that will be used to print text to a console window. It uses two less than signs, collectively known as the insertion operator ( << ), to indicate what to output. The string can then be specified, delimited by double quotes, and followed by a semicolon. The semicolon is used in C++ to mark the end of all statements.
#include
int main()
{
std::cout << "Hello World";
}
Using the Standard Namespace
To make things a bit easier, you can add a line specifying that the code file uses the standard namespace. You then no longer have to prefix cout with the namespace ( std:: ) since it is used by default.
#include
using namespace std;
int main()
{
cout << "Hello World";
}
IntelliSense
When writing code in Visual Studio, a window called IntelliSense will pop up wherever there are multiple predetermined alternatives from which to choose. This window can be also brought up manually at any time by pressing Ctrl+Space to provide quick access to any code entities you are able to use within your program. This is a very powerful feature that you should learn to make good use of.
Footnotes
http://www.microsoft.com/visualstudio
2. Compile and Run
Visual Studio Compilation
Continuing from the last chapter, the Hello World program is now complete and ready to be compiled and run. You can do this by going to the Debug menu and clicking on Start Without Debugging (Ctrl+F5). Visual Studio then compiles and runs the application, which displays the text in a console window.
If you select Start Debugging (F5) from the Debug menu instead, the console window displaying Hello World will close as soon as the main function is finished. To prevent this, you can add a call to the cin.get function at the end of main. This function, belonging to the console input stream, will read input from the keyboard until the Return key is pressed.
#include
using namespace std;
int main()
{
cout << "Hello World";
cin.get();
}
Console Compilation
As an alternative to using an IDE, you can also compile source files from a terminal window as long as you have a C++ compiler. For example, on a Linux machine you can use the GNU C++ compiler, which is available on virtually all UNIX systems, including Linux and the BSD family, as part of the GNU Compiler Collection (GCC) . This compiler can also be installed on Windows by downloading MinGW or on the Mac as part of the Xcode development environment.
To use the GNU compiler, you type its name g++ in a terminal window and give it the input and output filenames as arguments. It then produces an executable file, which when run gives the same result as the one compiled under Windows in Visual Studio.
g++ MyApp.cpp -o MyApp.exe
./MyApp.exe
Hello World
Comments
Comments are used to insert notes into the source code. They have no effect on the end program and are meant only to enhance the readability of the code, both for you and for other developers. C++ has two kinds of comment notations: single-line and multi-line. The single-line comment starts with // and extends to the end of the line.
// single-line comment
The multi-line comment may span more than one line and is delimited by /* and */ .
/* multi-line