•  c++ introduction
C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development costs. C++ is portable and can be used to develop applications that can be adapted to multiple platforms.

C++ is one of the world's most popular programming languages.

C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems.

Difference between C and C++

C++ was developed as an extension of c, and both languages have almost the same syntax.

The main difference between C and C++ is that C++ support classes and objects, while C does not.


Get Started

This tutorial will teach you the basics of C++.

It is not necessary to have any prior programming experience.


To start using C++, you need two things:

  • A text editor, like Notepad, to write C++ code
  • A compiler, like GCC, to translate the C++ code into a language that the computer will understand

There are many text editors and compilers to choose from. In this tutorial, we will use an IDE (see below).


👍

myfirstprogram.cpp

#include <iostream>
using namespace std;

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

output

hello world!


C++ Syntax

Let's break up the following code to understand it better:

Example


#include <iostream>
using namespace std;

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

Example explained

Line 1: #include <iostream> is a header file library that lets us work with input and output objects, such as cout (used in line 5). Header files add functionality to C++ programs.

Line 2: using namespace std means that we can use names for objects and variables from the standard library.



Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.

Line 4: Another thing that always appear in a C++ program, is int main(). This is called a function. Any code inside its curly brackets {} will be executed.

Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<) to output/print text. In our example it will output "Hello World!".

Note: Every C++ statement ends with a semicolon ;.

Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }

Remember: The compiler ignores white spaces. However, multiple lines makes the code more readable.

Line 6: return 0 ends the main function.

Line 7: Do not forget to add the closing curly bracket } to actually end the main function.


Omitting Namespace

You might see some C++ programs that runs without the standard namespace library. The using namespace std line can be omitted and replaced with the std keyword, followed by the :: operator for some objects:


Example

#include <iostream>

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

C++ Output (Print Text)

The cout object, together with the << operator, is used to output values/print text:


#include <iostream>
using namespace std;

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

Example


#include <iostream>
using namespace std;

int main() 
{
  cout<< "Hello World!";
  cout<< "I am learning C++";
  return 0;
}


New Lines

To insert a new line, you can use the \n character:

Example

#include <iostream>
using namespace std;

int main()
 {
  cout << "Hello World! \n";
  cout << "I am learning C++";
  return 0;
}


Both \n and endl are used to break lines. However, \n is most used.

But what is \n exactly?

The newline character (\n) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.



C++ Comments

Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Comments can be singled-lined or multi-lined.


Single-line Comments

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by the compiler (will not be executed).

This example uses a single-line comment before a line of code:

Example

// This is a comment
cout<< "Hello World!";

Example

cout << "Hello World!"// This is a commen

C++ Multi-line Comments

Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by the compiler:

Example

/* The code below will print the words Hello World!
to the screen, and it is amazing */

cout << "Hello World!";