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.
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.
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 . An IDE (Integrated Development
Environment) is used to edit AND compile the code.
Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These
are all free, and they can be used to both edit and debug C++ code.
We will use Code::Blocks in our tutorial, which we
believe is a good place to start.
Let's create our first C++ file. Open Codeblocks and go to
File > New > Empty File. Write the following
C++ code and save the file as
myfirstprogram.cpp (File > Save File as):
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" ;
return 0;
}
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 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:
// This is a comment
cout << "Hello World!" ;
This example uses a single-line comment at the end of a line of code:
cout << "Hello World!" ;
// This is a comment
Multi-line comments start with
Any text between
/* The code below will print the words Hello World! to the screen, and
it is amazing */
cout << "Hello World!" ;
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
Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a value:
type variable = value;
Where type is one of C++ types (such as int), and variable is the
name of the variable (such as x or myName). The
equal sign is used to assign values to the variable.
To create a variable that should store a number, look at the following
example:
int myNum = 15;
cout << myNum;
You can also declare a variable without assigning the value, and assign the value later:
int myNum;
myNum = 15;
cout << myNum;
Note that if you assign a new value to an existing variable, it will overwrite the previous value:
int myNum = 15;
// myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum; // Outputs 10
Other Types
A demonstration of other data types:
int myNum = 5;
// Integer (whole number without decimals)
double myFloatNum = 5.99;
// Floating point number (with decimals)
char myLetter = 'D';
// Character
string myText = "Hello";
// String (text)
bool myBoolean = true;
// Boolean (true or false)
All the information in this page is taken from the w-3 schools c++ tutorial