C++ Programming


C++ is one of most used programming languages to make software on PC.

You need a compiler to write C++ programs.



Visual Studio Installation

One of most used free compiler is Visual Studio Community that you can download on: https://visualstudio.microsoft.com/free-developer-offers/

Run Installer and select "Desktop Development with C++" in installer options.


In "Installation Locations", select where to install Visual Studio then click on "Install" to start Installation.



Project Creation

After Installation, launch Visual Studio Community.

Select "File" on menu then "New Project".
On New Project page, select "Empty Project" then click on Next.


Give a Name to your Project then select "Create".

A New Project has been created but it needs a C++ File to work.
A C++ File is where you write C++ Code to build a program.

Right-Click on "Source Files" in "Solution Explorer", then Select "New Item" in "Add" Menu.

In the new Window, select "C++ File" then click on "Add".

A C++ File has been added to your Project.

Inside "Source Files" Menu in "Solution Explorer", you can see the New Source File.
Double Click on this to open the programming Windows.



Programming Tutorial


Functions

A C++ program works by calling "functions".
A Function is an area where you write actions that your program must do.
You can write as much functions that you want then call them in the order that you want.

The first function must be called "main()".
That's a mandatory function where all programs start.

int main()
{
  // Code must be written here
}

"main" is the name of function

"(" and ")" is for parameters  that you can add to your functions.

"int" is the type of value that function must return.

You can choose "void" if you don't want return value for your functions but for the main function "int" label is mandatory.

"{"  and "}" are limits of the area where you write code.

All functions begin with "{" and finish with "}".

"//" means that compiler should not compile the next text.
When you want to add commentaries or explanations to your code you have to add a "//" before your text.


void FunctionA()
{

}

void FunctionB()
{

}

int main()
{

}
Example of two functions "FunctionA" and "FunctionB" added with main function.
These functions do nothing because they have no code.

C++ has pre-made functions to do elementary things like show text or record text that user type.
To call these functions you have to write the file where these functions are defined.

printf() is a function who shows a text on screen.
getchar() is a function who wait that user type on keyboard.
These two functions are defined in file "stdio.h".
To use them you have to write "#include <stdio.h>" at start of your code.


#include <stdio.h>

int main()
{
   printf("Programming Test");
   getchar();
}

printf("Programming Test");  
Show the text "Programming Test" in Screen.

getchar();
Wait user input before continue

All functions and instructions must always finish by ";".

Copy this code then select "Build Solution" in "Build" Menu.


An Executable (.exe File) has been created.

To Test this program you have to Select "Start Without Debugging" in "Debug" Menu.


A Window will appear showing the text "Programming Test" and waiting for user input.




Variables

A variable is a memory zone where you can store numbers or texts.
A variable can have any alphanumeric name
You have to declare variables type before you can use them.


#include <stdio.h>
#include <string>
using namespace std;

int main()
{
   int A;
   char B;
   string C;
}

A, B and C are three variables.
int is for storing Numbers
char is for storing Letters
string is for storing Texts

To use string you have to add "#include <string>" and "using namespace std;" at top of your file.

When your variables are declared, you can store values in them with "=" label.

#include <stdio.h>
#include <string>
using namespace std;

int main()
{
   int A;
   char B;
   string C;

   A = 725;
   B = 'G';
   C = "This is a Text.";
}

You can also store values immediately after variables declarations.

#include <stdio.h>
#include <string>
using namespace std;

int main()
{
   int A = 725;
   char B = 'G';
   string C = "This is a Text.";
}

To Show values of your variables in your program you can use cout or printf instructions.
You have to add "#include <iostream>" to use cout.
To show a line break, you have to use the "endl" symbol with cout or the "\n" symbol with printf.

#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
   int A = 725;
   char B = 'G';
   string C = "This is a Text.";

   cout << "Variable 1: " << A << endl;
   cout << "Variable 2: " << B << endl;
   cout << "Variable 3: " << C << endl;

   getchar();
}

Build and Run your program and you should see the three variables.

Variable 1: 725
Variable 2: G
Variable 3: This is a Text.

To Store values that you type in a DOS Window into a Variable, you can use cin or scanf instructions.
You have to add "#include <iostream>" to use cin.

#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
   int VarNumber;
   char VarLetter;
   string VarText;

   cin >> VarNumber;
   cin >> VarLetter;
   cin >> VarText;

   getchar();
}



Arithmetic

Arithmetic operations work like in mathematics.

int Addition = 5+2; 
int Substraction = 9-8;
int Division = 10/5;
int Multiplication = 58*2;

int Variable  = Addition + Substraction;
int Variable2 = Multiplication / Division;

Functions are used to simplify code.
For Example , you can make arithmetic functions that return the result of a calculation.
These functions receive values in their parameters then return a result.

int Addition(int Var1, int Var2)
{
   int Result = Var1 + Var2;
   return Result;
}
This function receives two numbers in its parameters (Var1 and Var2) then returns the result of the addition of these two numbers.

You can use a function that you wrote like a pre-made function.

int Var = Addition(10, 5);
The variable Var receives the result of the Addition of 10 and 5.


Example of four Arithmetics Functions.
Commentaries (after // symbols) give explanations for each line.

#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

// Definition of Addition Function
int Addition(int Var1, int Var2)
{
   int Result = Var1 + Var2;
   return Result;
}

// Definition of Substraction Function
int Substraction(int Var1, int Var2)
{
   int Result = Var1 - Var2;
   return Result;
}

// Definition of Multiplication Function
int Multiplication(int Var1, int Var2)
{
   int Result = Var1 * Var2;
   return Result;
}

// Definition of Division Function
int Division(int Var1, int Var2)
{
   int Result = Var1 / Var2;
   return Result;
}

// Main Function
// Programs always begin to execute functions by main() function
int main()
{
   // Var1 Receive Addition of 10 and 15
   int Var1 = Addition(10, 15);

   // Var2 Receive Substraction of 100 and 20
   int Var2 = Substraction(100, 20);

   // Var3 Receive Multiplication of 50 and 20
   int Var3 = Multiplication(50, 20);

   // Var4 Receive Division of 70 and 2
   int Var4 = Division(70, 2);

   // Show Var1 in Dos Window then add a line break (endl)
   cout << Var1 << endl;

   // Show Var2 then line break
   cout << Var2 << endl;

   // Show Var3 then line break
   cout << Var3 << endl;

   // Show Var4 then line break
   cout << Var4 << endl;  

   // Wait user Keyboard Input

   getchar();
}




Arrays

Individual Variables are usefull when you need a small number of variables but very difficult when you need a thousand.

Arrays are used to store series of Variables and permit to work with big numbers of them.

Example of Arrays using. 
// Declaration of an array of 5 Variables
int Variable[5];

// Declaration of an array of 3 Variables with immediate storing of 3 numbers
// 5 is record in position 0 - 7 is record in position 1 - 20 is record in position 2
int Store[3] = {5, 7, 20};

// Record of a number in "Variable" Array at position 0
Variable[0] = 3;

// Record of a number in "Store" Array at position 1
Store[1] = 5;

// Variable Var receives addition of Number at position 0 of "Variable" Array and Number at position 2 of "Store" Array
int Var = Variable[0] + Store[2];



Conditionals Instructions

Conditionals instructions permit to control what a program will do.

The usual shema is:

if ( Condition )
    Do Action 1
else
    Do Action 2

Example of if instruction
int Var = 5;
if ( Var > 10 )
    cout << "Var is bigger than 10";
else
    cout << "Var is smaller than 10";