Get up to 80 % extra points for free! More info:

Lesson 2 - Visual Studio and your first C++ console application

Lesson highlights

Are you looking for a quick reference on creating a C++ project in Visual Studio instead of a thorough-full lesson? Here it is:

Visual Studio C++ components - C++ Basic Constructs
  • Run Visual Studio and select File -> New -> Project in the application menu:
Creating a new project in Visual Studio - C++ Basic Constructs
  • Select the Visual C++ template -> Windows and in the next menu select Empty Project.
  • Edit the project name and location if needed and confirm the form.
  • Right-click the Source files folder in the Solution Explorer window on the right, and choose Add -> New Item:
New C++ source code - C++ Basic Constructs
  • Choose C++ File (.cpp) and name it Source.
  • Paste the Hello world program code in it:
#include <iostream>
using namespace std;

int main(void)
{
    cout << "Hello ICT.social!" << endl;
    cin.get();
    return 0;
}
  • Hit the green play button in the toolbar to run the project.

Would you like to learn more? A complete lesson on this topic follows.

In the previous lesson, Introduction to the C++ language, we talked about the language itself. In today's lesson, we're going to focus on the Visual Studio IDE. We'll show you how to use it and program a simple console application.

IDE stands for Integrated Development Environment. In a nutshell, it's an application in which we'll write source code, and then use it to run, test and debug our application.

Of course, we'll have to start by installing Visual Studio. If you have access to paid versions, which come with advanced features, go ahead and use them. However, the free Community version will be more than enough for us. You can download Visual Studio 2015 Community Edition from the official Microsoft site.

Installation

Even if English isn't your native language, I highly suggest installing the English version of Visual Studio. The advantage to using English applications is that if you run into problems, you will most likely not be able to find the answers in your language (e.g. with advanced configurations). All of your applications and programs should also be written in English. For now, this isn't a crucial step, so feel free to use your native language without using any accent characters, if your language has any. If you want to become a professional programmer, you should try to improve your language skills, and write everything in English.

The installation is very easy. Choose the Custom type when you execute it:

Visual Studio C++ installation - C++ Basic Constructs

Then, check the Visual C++ group (everything in it) and do the same with the Common Tools group.

Visual Studio C++ components - C++ Basic Constructs

You can install even more tools if you want, but this is all we're going to need for this course. Either way, you'll be able to install additional components later if needed.

Note: Be careful with installing too many components, the installation might take even up to several hours.

Then, we'll click "Next", then "Install", and wait... :)

After the installation, you may need to register your Visual Studio. Registration is free, after which you'll receive a serial number that allows you to use the program legally. Another option is to log in using your Microsoft account.

Note: Visual Studio is designed for developing Windows applications. If you want to create applications for different operating systems (or to program on another operating system), use something like the NetBeans IDE. You can download NetBeans from the official website or you can use Eclipse.

Backup and version control

Programmers usually need a tool that will provide version control and a backup of their work. We can't rely on the fact that we simply save the program because we're humans and humans make mistakes. When you lose a few days' or even a few weeks' work, it can be really demotivating. It's good to think about such situations right from the start. I highly recommend Dropbox, which is extremely simple, and automatically stores multiple versions of your files, which makes it possible to revert to previous versions of the project, and also synchronizes with a web repository. Even if you've accidentally deleted your project, overwrote it, somebody stole your laptop or your hard drive accidentally collapsed, your data will remain safe. Dropbox also allows you to share projects with several developers.

You can also use a tool called GIT for the same purposes, it can be integrated into Visual Studio if you check the "Git for Windows" option. However, its configuration would require the "while" article. Dropbox is perfect for our current intents and purposes.

Creating a project

Run Visual Studio and select File -> New -> Project in the application menu.

Creating a new project in Visual Studio - C++ Basic Constructs

In the New Project window, select the Visual C++ template -> Windows and in the next menu select Empty Project. Let's name this project "FirstApplication". Create a folder for your projects in your Dropbox folder, for example, "cpp/" (as in Plus Plus). Use the Browse button and select a folder C:\Users\your_name\Dropbox\cpp\. We will stick with console applications, the command line interface, for a while because it needs minimal knowledge of the object-oriented world, and they are ideal for learning the basics of the language. Confirm the form.

Visual Studio Tutorial

The window should now look like this (I have resized it to fit :) ):

Visual Studio for C++ - C++ Basic Constructs

A very important element in the window is the green "Play" button in the upper bar, which compiles and runs our program. You man try it out, but our program won't do anything. It'll simply terminate right away. You can also run the program with the F5 keyboard shortcut. Shortcuts in VS are designed very well and they will simplify your work a lot. That is, if you decide to learn them.

Near the arrow icon, we have Debug selected. This means that the program will be compiled in Debug mode, and it'll contain certain routines to debug errors. It's mainly used to test the program while we're developing it, and the program may run slower due to it. Once you're sure that your program is complete, switch it to Release mode and run the program. As a result, we'll have a program which can be distributed among people.

There is no code yet, so let's add some :) We'll click the right mouse button on the "Source files" folder in the Solution Explorer window, and choose Add -> New Item.

New C++ source code - C++ Basic Constructs

We'll choose C++ File (.cpp) and name it Source.

Directory structure of the console application

Let's take a look at what's in our application folder. Open it up, mine's in C:\Users\your_name\Dropbox\cpp\FirstApplication\, and find a file named FirstApplication.sln. "Sln" stands for " Visual Studio solution ". It's a set of projects and it may contain multiple applications which are used for multi-tier applications, or for testing. We'll open our applications using this file. There should also be a folder named "FirstApplication/" containing our project. Let's open it up, and see what's inside.

The FirstApplication.vcxproj file contains a project file that we can use to open up our app. Source.cpp contains the actual source code. You'll also see the Debug/ and Release/ subfolders. There are .exe files of our application inside of it. That is, if we run the application at least once in the corresponding mode. In order to show your application to others, the .exe file in the Release/ subfolder is the file you would have to send them. Don't worry about any of the other files, for now.

Hello world

As tradition instructs, we'll make the first program most people make when they learn a new language - Hello World. This is a program that displays "Hello world" or some similar text.

The code will look like this:

#include <iostream>
using namespace std;

int main(void)
{
    cout << "Hello ICT.social!" << endl;
    cin.get();
    return 0;
}

Here's a detailed description of the code:

  • #include <iostream> attaches a library for console output (and input) to our project.
  • using namespace std simplifies command names from the iostream library. Without this line, we'd have to write to the console using the full form std::cout << "Hello ICT.social!" << std::endl;.
  • int main(void) {} - Main() is a function, the main one. We'll learn to declare functions later. We write all the program code in its curly brackets block. The main() function is executed when the application starts.
  • cout << "Hello ICT.social!" << endl; is how we print text to the console. We write text in quotes (""), to avoid C++ misinterpreting it as other commands. << endl prints the end of a line (breaks the line).
  • The << operator (after cout) "sends" data to the console in this case
  • cin.get() waits for Enter to be pressed so the program is not terminated immediately.
  • return 0 - We terminate the program using the return command, 0 value tells the system that everything went fine. (If there is an error in the program, the value 1 is returned instead).

Now, run the program by pressing F5.

Your first C++ app
Hello ICT.social!

Congratulations, you have just become a programmer :) That will be all for today, in the next lesson, Variables and the type system in C++, we'll look at the basic data types and create a simple calculator.

Today's project is attached as a file at the end of the article. You could always download the result below each lesson. I suggest that you create a project using the tutorial, and if something in your project doesn't work, then download it to find the mistake. If you download the lesson code before even trying to make it yourself, you won't learn anything :)


 

Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.

Download

By downloading the following file, you agree to the license terms

Downloaded 7x (7.29 MB)
Application includes source codes in language C++

 

Previous article
Introduction to the C++ language
All articles in this section
C++ Basic Constructs
Skip article
(not recommended)
Variables and the type system in C++
Article has been written for you by David Capka Hartinger
Avatar
User rating:
3 votes
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn university David learned IT at the Unicorn University - a prestigious college providing education on IT and economics.
Activities