C++ Introduction

C++ as a programming language

A programming language is a way of communication between a person and a machine on which the code is running. Hence machines communicate in what is known as machine code, people have developed special programs (compilers) so that our language can be converted into machine code. C++ is one of those languages.

In 1985, C++ was created in such a way that it resembles its predecessor C (by having the similar syntax, low level architecture, etc.), but had object-oriented paradigm. Before diving into object-oriented paradigm, we should look at the basic syntax and the features which were taken from C.

C++ Main function

The starting point in every C++ program is the main function.

We will give you an easy example below:

#include <iostream>

using namespace std;

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

Output:

Hello world

The purpose of this program is simple: 

– “#include <iostream>” provides basic input and output service (more about this later)

–  “using namespace std;” means that we use namespace std.  In this namespace are some defined components which are necessary for our input and output stream (more about this later)

– “int main()” is a declaration of the start of every program

– “{ … }” is a code block where you define what will the program do. Instructions in the code block will be executed line by line

– “cout << ” is a special instruction which prints out everything you give it on the right between the quotation marks “”.

– Every instruction MUST end with a semicolon ;

– “return 0;” represents the exit code of the program (more about this later)

Comments

A comment is a text which can be useful for a programmer, but will be ignored by a compiler. That means that only programmer can see that text. There are 2 types of comments in C++:

  • // … – to the end of the line
  • /* … */ – in several rows
int x = 1; //This is a comment. Compiler will only see the variable "x", but not this text

Leave a Reply