C++ Identifiers

C++ Identifiers

All C++ variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

Note: It is recommended to use descriptive names in order to create understandable and maintainable code: Continue reading C++ Identifiers

C++ Declare Multiple Variables

Declare Many Variables

To declare more than one variable of the same type, use a comma-separated list:

Example

int x = 5, y = 6, z = 50;
cout << x + y + z;

Continue reading C++ Declare Multiple Variables

C++ Variables

C++ Variables

Variables are containers for storing data values.

In C++, there are different types of variables (defined with different keywords), for example:

  • int – stores integers (whole numbers), without decimals, such as 123 or -123
  • double – stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char – stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotes
  • string – stores text, such as “Hello World”. String values are surrounded by double quotes
  • bool – stores values with two states: true or false

Continue reading C++ Variables

C++ Comments

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.

Continue reading C++ Comments

C++ New Lines

New Lines

To insert a new line in your output, 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;
}

You can also use another << operator and place the \n character after the text, like this : Continue reading C++ New Lines

C++ Output Numbers

C++ Print Numbers

You can also use cout() to print numbers.

However, unlike text, we don’t put numbers inside double quotes:

Example

#include <iostream>
using namespace std;

int main() {
  cout << 3;
  return 0;
}

You can also perform mathematical calculations: Continue reading C++ Output Numbers

C++ Output

C++ Output (Print Text)

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

Just remember to surround the text with double quotes (""): Continue reading C++ Output

C++ Statements

C++ Statements

A computer program is a list of “instructions” to be “executed” by a computer.

In a programming language, these programming instructions are called statements.

The following statement “instructs” the compiler to print the text “Hello World” to the screen: Continue reading C++ Statements

C++ Syntax

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. Continue reading C++ Syntax

C++ Getting Started

C++ Get Started

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).

Continue reading C++ Getting Started