CHAPTER 1

image

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 Microsoft’s website.1 This IDE has built-in support for the C++11 standard and also includes many features of C++14 as of the 2015 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 do so, 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 image New image 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 Win32 Console Application 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 and another dialog box will appear titled Win32 Application Wizard. Click next and a couple of application settings will be displayed. Leave the application type as Console application and check the Empty project checkbox. Then click Finish to let the wizard create your empty project.

Adding a Source File

You have now created a C++ project. In the Solution Explorer pane (View image 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 select Add image 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 also opened for you.

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 what will be 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 is one of the standard library files that come with all C++ 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.

#include <iostream>
int main() {}

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 what the namespace contains. 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 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 <iostream>

int main()
{
  std::cout << "Hello World";
}

Using 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 now used by default.

#include <iostream>
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.

________________

1http://www.microsoft.com/visualstudio

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.208.72