Introduction to C++ Programming for Beginners

Learn the fundamentals of C++ programming through our series of tutorials. You can use any C++ compiler in this tutorial series, but we will use the Visual Studio C++ compiler.

In this tutorial, you learn how to download and install Microsoft Visual Studio Community and the C++ Language Pack. It is free and easy to learn. It also offers robust features on programming in C++ as well as other languages that can be installed as needed.

You will learn C++ starting with Console applications. A Console application (text input from keyboard / text output to console window), is all you need to get started in C++.

Later we will go into more advanced topics like Object Oriented Programming. You can learn all this in a Console Project as well. The skills you learn will easily transfer to C++ for Windows desktop and many other applications that are C++ specific. Learning new languages based on C++ will also come easy for you, such as C#, Java, JavaScript.

You will learn:

  • Installing Visual Studio For Windows
  • Launch a Console Application
  • What a “.cpp” file is
  • The “iostream” in C++
  • The “using namespace std” in C++
  • The “int main()” Function
  • The “cout” Keyword
  • The “return 0” in C++
  • Comments in C++
  • Basics of C++ Syntax
    • Curly Braces
    • Semi-Colons

So lets get started

Your first program is the simple “Hello World” example:

int main()
{
    cout << "Hello World!\n";

    return 0;
}

Installing Visual Studio For Windows

Note: If you already have Visual Studio installed but do not have the C++ Workload, open VS and go to Tools / Get Tools on the menu. This will open the VS installer window. Make your selections and click the modify button to download the workload.

If you already have VS but no C++ workload, just open the installer window

In your web browser go to visualstudio.microsoft.com and download the free community version for windows (current version as of this post is 2019). Note that C++ in Visual Studio is only for Windows operating systems.

Open or double click the installer file.

The installation screen will give you options on how to set up Visual Studios. The most important are Workloads & Language Packs. Be sure to include the workload for C++ along with the default selections.

Follow all instructions.

Then click the install button to start the full download. On slower computers or slow connections the download will take a bit of time.

Open Visual Studio. Now you are ready to start a console project.

Starting a Console App in Visual C++

Lets start a bare bones console application. Open Visual Studios. On the Menu, go to File / New / Project. Then under Other Languages choose Visual C++ and then Console App in the list to the right. Note: below is Visual Studio Community 2017 version, but the steps are much the same in all versions.

Name your App, in this case we will name it “ConsoleApplication1” then click ok.

Your first program – “Hello World”

We will start with a simple “Hello World” program. Not a real App, just a text print to the screen. The project opens with a default .cpp file.

#include <iostream>

// Program execution begins and ends with the 'main' function.
int main()
{
    std::cout << "Hello World!\n";
}

Cpp files are organized in the Source Files folder, where most of your code will go. This is displayed in the Solutions Explorer window.

There are Header and Resource folders as well for other file names which we don’t need for simple console applications as we are concentrating on the fundamentals of C++.

We will modify the code by adding the standard library (namespace std). This will make the :: double colon redundant when using console keywords like cout. We will also add return 0. Some compilers don’t require this as it is automatically inserted, but we will use it throughout the remainder of these tutorials.

#include <iostream>

using namespace std; 

// Program execution begins and ends with the 'main' function.
int main()
{
    cout << "Hello World!\n";

    return 0;
}

To start the program for the first time it has to be Built by the Compiler. You can go to Build / Build Solution first, or if you are sure there are no syntax errors, go to Debug / Start Without Debugging, which will compile and start the program. It is best to Build the program first as the compiler will show errors in the Error List tab next to your .cpp tab.

After building your solution and if there are no errors, click Debug / Start Without Debugging. The console window pops up and you see Hello World! Press any key to exit the console.

A Closer Look

Lets take a closer look at the code in this simple application. If you have no coding experience, it might look confusing at first.

#include <iostream>

using namespace std; 

// Program execution begins and ends with the 'main' function.
int main()
{
    cout << "Hello World!\n";

    return 0;
}

The first and second lines preceding the main() function gives you access to the std (standard library) to use keywords like cout that print text to the console.

Next is the main() function in which code is placed inside curly braces. In fact all functions must be enclosed in these braces. You will learn all about functions in the Part 3 tutorial below in the links.

The cout keyword will print the text Hello World! to the console window. The text to be printed is in quotations and is called a string.

The \n is for a carriage return to the next line.

The last statement is return 0 because main() by default is a function returning data type int (integer). You will learn about data types in Part 2.

Semicolons & Comments

Take note that all statements must end in a semicolon ;

cout << "Hello World!\n";

return 0;

The double “slash marks” is a comment and is ignored by the compiler. It is for your reference only. You need to make more comments as your code grows. When you come back weeks or months later to examine the code, the comments are like a summary of whats going to happen.

// Program execution begins and ends with the 'main' function.

Next Tutorial

You will start learning the basics of C++ .

You might also want to read:

Part 2 – The Basics of C++

Part 3 – Conditional “if” Statement

Part 4 – else if Statement in C++

Part 5 – “switch and loops”

Part 6 – Arrays & Strings

Part 7 – Pointers

Part 8 – Functions in C++

If you like this page please share with your friends.

Leave a Comment

Your email address will not be published. Required fields are marked *