Introduction to C++: history, features and applications
What is it and why?
Imagine you are giving commands to a robot. If you say "do something," it will be confused. But if you say "turn left, walk 5 steps, raise your hand," it will execute everything clearly. C++ is a language in which you write very precise instructions for the computer. It's like a LEGO set: you assemble every detail yourself, resulting in fast and powerful programs.
Why learn it? Firstly, games (e.g., Minecraft), browsers, and spacecraft are built with C++. Secondly, it teaches you to think like a programmer: you will understand how the computer stores numbers, how memory works. It's like knowing not just how to drive a car, but also what's under the hood.
Even if you are a beginner, C++ is not as scary as it seems. We'll start with the simplest things — like writing "Hello, world!" and counting candies.
Syntax and code examples
Syntax is the rules of a language, like grammar in English. In C++, each command ends with a semicolon ;, and code blocks are wrapped in curly braces {}. Let's look at some live examples.
Example 1: Hello, world!
#include <iostream> // Include library for input/output
using namespace std; // So we don't have to write std:: before cout
int main() { // Main function, where the program starts
cout << "Hello, world!" << endl; // Output text to the screen
return 0; // Indicate that the program finished successfully
}
How it works: #include is like taking a ready-made set of tools. int main() is the program's entry door. Inside {} we write commands. cout is the "typewriter" that outputs text. endl is a newline.
Example 2: Counting candies
#include <iostream>
using namespace std;
int main() {
int candies = 10; // Integer — number of candies
int friends = 3; // Number of friends
int each = candies / friends; // Divide candies equally (integer division)
cout << "Each person gets " << each << " candies" << endl;
return 0;
}
Explanation: int is a data type for integers (like 10, 3). / is division. But there's a catch: if you divide 10 by 3, you get 3, not 3.33, because we are working with integers. The remainder (1 candy) is lost — you need to remember this!
Example 3: Guess the number
#include <iostream>
using namespace std;
int main() {
int secret = 7; // The secret number
int guess; // Variable for the user's answer
cout << "Guess a number from 1 to 10: ";
cin >> guess; // Read the number entered by the user
if (guess == secret) { // If guessed correctly
cout << "You win!" << endl;
} else { // Otherwise
cout << "Wrong guess. It was " << secret << endl;
}
return 0;
}
New things: cin is the "microphone" that listens to what the user enters. if is a condition check. == is comparison (don't confuse with =, which assigns a value!). else means otherwise.
Example 4: Loop — repeat 5 times
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) { // Start: i=1; condition: while i<=5; step: i+1
cout << "Line number " << i << endl;
}
return 0;
}
How to understand: for is like "repeat 5 times". int i = 1 — we create a counter. i <= 5 — as long as the counter is not greater than 5, we keep looping. i++ — increase the counter by 1 after each step. As a result, 5 lines will be displayed on the screen.
Example 5: Function — your own helper
#include <iostream>
using namespace std;
// Create a function that greets by name
void greet(string name) { // void — the function returns nothing
cout << "Hello, " << name << "!" << endl;
}
int main() {
greet("Anna"); // Call the function with the name "Anna"
greet("Peter");
return 0;
}
The idea: Functions are like recipes. You write the greet recipe once, and then use it many times. string is the type for text. Inside the parentheses (string name) are the ingredients we pass to the function.
Comparison with Python
Python is like a spoken language: "Hey, computer, do this." C++ is like a technical manual: "Take register AX, write 5 into it, perform addition." Let's compare with examples.
| Task | C++ | Python |
|---|---|---|
| Output text | cout << "Hello"; |
print("Hello") |
| Input a number | cin >> x; |
x = int(input()) |
| Condition | if (x > 0) { ... } |
if x > 0: ... |
| Loop 5 times | for (int i=0; i<5; i++) |
for i in range(5): |
| Function | void f(int a) { ... } |
def f(a): ... |
Main differences:
- Semicolons: In C++ they are mandatory at the end of each statement. In Python, they are not needed.
- Data types: In C++, you must explicitly specify the type (
int,string, etc.). In Python, the type is determined automatically. - Braces: In C++, code blocks are enclosed in
{}. In Python, indentation is used. - Compilation: C++ code must be compiled before running. Python code is executed directly by the interpreter.