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

C and C++ C++ Basics Visual Studio and your first C++ console application

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

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

Visual Studio C++ components

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

In the New Project window, select the Visual C++ template -> Windows and in the next menu select Win32 Console Application. 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_na­me\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.

The next form will then appear - Win32 Application Wizard. We'll choose Next and select Console Application and Empty project.

C++ project settings in Visual Studio

Now, let's finish the entire thing using the "Finish" button.

Visual Studio Tutorial

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

Visual Studio for C++

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

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_na­me\Dropbox\cpp\Fir­stApplication, and find a file named FirstApplicati­on.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 FirstApplicati­on.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 :)


 

  Activities (5)

Article has been written for you by David Capka
Avatar
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 College Author learned IT at the Unicorn College - prestigious college providing IT and economical education.

Do you like this article?
Total (1 votes) :
55555


 


Thumbnail
All articles in this section
C++ basic constructs

 

 

Comments

Avatar
Adil Shafique
Member
Avatar
Adil Shafique:12. March 23:54

sir what the best ide for cpp .. before reading your article i learn cpp and practice on turbo cpp ide .. so there is many difference .. like i write a program
#include<iostre­am.h> // header file input output stream
#include<conio.h>
void main() or int main() // valueless main
{
int a,b,c
a=5;
b=2;
c=a+b;
cout<<c;
getch();
}
....
so you are using like return or cin.get i think for showing result .. sir can you clear the difference between visual studio or dev or netbean and turbo ide basic syntax main or you using namespace std.. etc etc

 
Reply 12. March 23:54
Avatar
David Capka
ICT.social team
Avatar
Replies to Adil Shafique
David Capka:13. March 4:45

Turbo C++ was mainly for DOS, Visual Studio is more modern and uses actual standards. Namespaces are a modern way to deal with a higher number of classes, they're basically packages for objects.

 
Reply 13. March 4:45
To maintain quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it is free.

2 messages from 2 displayed.