- Explain what C++ is used for and where
- Describe how a compiler turns source code into a program
- Write a program with
#include <iostream>,int main()andstd::cout - Add comments to your code
Your favourite 3D game draws dozens of frames every second, and your browser handles hundreds of page elements at once. Programs like these must be very fast, and a large share of them are written in the same language — C++. In this lesson you will find out what C++ is and how a program is born, and you will write your first one.
What is C++?
C++ is a general-purpose programming language. It was created by the Danish computer scientist Bjarne Stroustrup: the work began in 1979 under the name “C with Classes”, and in 1983 the language was renamed C++. C++ is built on top of the older and very fast C language and adds classes, templates and a rich standard library.
A program that translates the source code a programmer writes (for example, the file hello.cpp) into machine code that the processor runs directly. The result is a separate executable file.
In languages like Python, the code is read line by line while the program runs. C++ is a compiled language: the whole code is first translated into machine code and only then run. That is why C++ programs are very fast, and many mistakes are caught at compile time, before the program even starts.
- 1Write the code
Create a file with the
.cppextension in a text editor or an IDE (for example Visual Studio, VS Code or Code::Blocks) and type your code. - 2Compile it
A compiler (GCC, Clang or Microsoft Visual C++) checks the code and produces an executable file. If there is a mistake, no file is produced and you see an error message.
- 3Run it
Run the resulting file and the program prints its result. Every time you change the code, you have to compile it again.
g++ -std=c++17 hello.cpp -o hello
./hellohello.exe.Where is C++ used?
- Games and game engines — for example, the well-known Unreal Engine is written in C++.
- Browsers and large applications — most of the Google Chrome browser is written in C++.
- Systems and devices — parts of operating systems, drivers, robots and programs for microcontrollers such as Arduino.
- Programming olympiads — thanks to its speed and ready-made algorithms, C++ is the most popular language among olympiad contestants.
Your first program
Programmers have a tradition: the first program in a new language prints “Hello, world!”. In C++ it looks like this:
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}Hello, world!
#include <iostream>brings in the input/output library. Without itstd::coutwould not work.int main()is the main function where every C++ program starts. The curly braces{ }mark the beginning and the end of its body.std::cout(character output) is the output to the screen. The<<operator “sends” a value into this stream, andstd::endlends the line.- The
std::prefix says thatcoutbelongs to the standard namespacestd. - Every statement ends with a semicolon
;.return 0;tells the operating system that the program finished successfully.
Printing several lines
A program runs its statements one by one, from top to bottom. In a single std::cout statement you can chain text, numbers and even the results of calculations with <<. To move to a new line, programmers often write the short character '\n' instead of std::endl.
#include <iostream>
int main() {
std::cout << "My name is Aysel.\n";
std::cout << "I live in Baku.\n";
std::cout << "I am " << 12 << " years old." << '\n';
std::cout << "2 + 3 = " << 2 + 3 << '\n';
std::cout << "2 + 3" << '\n';
return 0;
}My name is Aysel. I live in Baku. I am 12 years old. 2 + 3 = 5 2 + 3
"2 + 3" is just text, while 2 + 3 without quotes is calculated.Comments
Comments are notes for the people who read the code; the compiler ignores them. Everything from // to the end of the line is a comment. A comment spanning several lines starts with /* and ends with */.
#include <iostream>
// This program greets the learner
int main() {
/* A comment can also
span several lines */
std::cout << "Welcome to Educora!\n"; // greeting
return 0;
}Welcome to Educora!
| Feature | C++ | Python |
|---|---|---|
| How it runs | compiled | interpreted |
| Speed | very high | moderate |
| Variable types | declared in advance | determined automatically |
| Code block | { } | indentation |
| End of a statement | ; | end of the line |
Key points
- C++ is a fast, compiled language used in games, browsers, system software and olympiads.
- The compiler turns a
.cppfile into an executable program; after every change you compile again. - Every program starts in the
int main()function. std::cout << ...prints to the screen;'\n'orstd::endlstarts a new line.- Statements end with
;, and comments are written with//or/* */.
Check yourself
10 questions. Every correct answer earns XP.
std::cout?