C++ Tutorial For Beginner – 1

Welcome to a new training series, this training series will consist of 3 main levels (beginner, intermediate, advanced) and there will be 5 articles at each level.

We will use the C++ programming language to develop games, at this level, we will cover the basic issues and features.

In this tutorial, we will learn C++ Setup, Output and Input concepts, Variables, simple data types, the syntax of C++.

C++ Setup In Visual Studio Code

You can readily use IDEs such as Visual Studio or Clion, but Visual Studio Code editor will be used in this course. Let’s prepare Vs Code for C ++ projects step by step.

First, let’s download Mingw to run the C ++ files and install Mingw on your computer with an ordinary installation.

Adding MinGW Compiler To Path
  1. In the Windows search bar, type ‘settings’ to open your Windows Settings.
  2. Search for Edit environment variables for your account.
  3. Choose the Path variable and then select Edit.
  4. Select New and add the Mingw-w64 destination folder path, with \mingw64\bin appended, to the system path. The exact path depends on which version of Mingw-w64 you have installed and where you installed it. If you used the settings above to install Mingw-w64, then add this to the path: C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin.
  5. Select OK to save the updated PATH. You will need to reopen any console windows for the new PATH location to be available. (Source used: Visual Studio Code Documentation)
Test G++ On Console

After a correct installation is complete, we use the console to test g ++. With the basic command, you can find out if MinGW is working and its version.

g++ --version
gdb --version
C++ Plugins For A Good Coding Environment

C++ Syntax And Running C++ File

We will now examine the basic C ++ syntax and learn how to compile and build a project with g ++.

// Basic C++ File

#include <iostream>
using namespace std;

int main() 
{
  cout << "Hello World!";
  return 0;
}

Let’s examine the structures here with a single list method, we will look at all of these features in more detail later.

Line 1: The iostream library is one of the basic libraries in C ++ and contains the codes we will use.

Line 2: means that we can use names for objects and variables from the standard library. So we don’t need to add std:: into function.

Line 3: The function that the program runs is this main function. We will write the entire project in the fancy brackets of this function. (We will write new functions later.)

Line 4: Cout function is used to print text on the screen. the semicolon indicates that it is a statement and that the statement is finished.

Line 5: Return 0 It is enough to know that the current function shows that it is finished.

How To Run C++ File On VS Code

In Visual Studio Code you will need to build the project first, press Ctrl + Shift + B and click build current file.

An exe copy of the C ++ file will be created. Open the console and type in the .\ Filename command. The result should be as follows.

> .\FileName
Hello World!

Output In C++

In the Syntax section, we mentioned that we use the cout concept to print text on the screen. Now let’s learn this function exactly.

// Basic C++ File

#include <iostream>
using namespace std;

int main() 
{
  cout << "Hello World! ";
  cout << "Write On Console";
  return 0;
}
Output: Hello World! X2

As you can see, if we use the cout expression simply, it will print on the screen on only one line. Now let’s learn the endl structure.

// Basic C++ File

#include <iostream>
using namespace std;

int main() 
{
  cout << "Hello World!" << endl;
  cout << "Write On Console";
  return 0;
}
Output: 
Hello World!
X2

Basic Data Types And Variables

We will now learn how to store data and the types of this data we can store, then we will learn how to use these variables.

  • Int (Integer) = used to introduce integer values to the computer
  • double (Decimal) = used to introduce decimal values to the computer
  • char (letters) = used to introduce letters values to the computer
  • string (text) = used to introduce text values to the computer
  • bool (logic) = used to introduce true or false values to the computer
Creating Variables

To create a variable, it first specifies the type of the variable, then the name and value of the variable.

DataType VariableName = Value;

Now let’s create a variable in the project and print it on the screen. We will use the data types we learned earlier.

// Basic C++ File

#include <iostream>
using namespace std;

int main() 
{
  int num = 2;
  string text = "ASDF";

  cout << "Number:" << num << endl;
  cout << "Text:" << text << endl;

  num = 7;
  cout << "New Number:" << num;
  return 0;
}
Output: 
Number:2
Text:ASDF
New Number:7

If you want to print the cout statement more than one thing to the screen, you can use the << operator.

C++ Declare Multiple Variables

In C ++, instead of dealing with one by one, we can assign more than one variable at the same time.For this, commas are used with variables.

int x = 1 , y = 2 , z = 3;

Note: Do not abbreviate the variable names in projects too much, adjust them according to the environment you will use.

Const In C++

You can specify constant values in mathematics or any constant value with const in C ++.

// x always is 1
const int x = 1;
const double pi = 3.14

Input In C++

We can use the cin function to get data from the user, we can receive data from any type of user and store this data.

Syntax: cin >> VariableName;
// Basic C++ File

#include <iostream>
using namespace std;

int main() 
{
  int x;
  string y;

  cout << "Enter Number: ";
  cin >> x;

  cout << "\nEnter Text: ";
  cin >> y;

  cout << "Number: " << x << endl;
  cout << "Text: " << y << endl;
}
Enter Number: 2
Enter Text: Hello

Number: 2
Text: Hello

Leave a Reply

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