C++ Programming
C++ Programming
C++ gives programmers a high level of control over system resources and
memory.
The language was updated 5 major times in 2011, 2014, 2017, 2020, and 2023
to C++11, C++14, C++17, C++20, and C++23
C++ can be found in today's operating systems, Graphical User Interfaces, and
embedded systems.
C++ is portable and can be used to develop applications that can be adapted to
multiple platforms.
The main difference between C and C++ is that C++ support classes and
objects, while C does not.
There are many text editors and compilers to choose from. In this tutorial, we
will use an IDE (see below).
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.
C++ Quickstart
Let's create our first C++ file.
Write the following C++ code and save the file as myfirstprogram.cpp (File >
Save File as):
My Frist Program
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
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
Don't worry if you don't understand how #include <iostream> and using
namespace std works. Just think of it as something that (almost) always
appears in your program.
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.
Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
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++ Statements
A computer program is a list of "instructions" to be "executed"
by a computer.
Example
cout << "Hello World!";
Many Statements
Most C++ programs contain many statements.
The statements are executed, one by one, in the same order as they are
written:
Example
cout << "Hello World!";
cout << "Have a good day!";
return 0;
Example explained
The first statement is executed first (print "Hello World!" to the screen).
Then the second statement is executed (print "Have a good day!" to the
screen).
And at last, the third statement is executed (end the C++ program
successfully).
int main() {
cout << "Hello World!";
return 0;
}