Every variable has a fixed type
In Python, a variable is a label that can point at anything: x = 5 then x = "five" is legal. C++ is statically typed: you declare what type a variable holds, and it holds that type forever.
int count = 5; // count is an int, always count = "five"; // COMPILE ERROR: cannot assign a string to an int
The compiler checks every assignment, every function call, and every operator against the declared types before the program runs. A whole category of bugs that Python discovers mid-run (or never) simply cannot compile in C++.
The cost is that you must write types out. The payoff is that the compiler becomes a tireless reviewer: if it builds, the types line up everywhere.
Reading compiler errors
When types do not line up, the compiler prints an error naming the file, line, and problem:
main.cpp:5:11: error: invalid conversion from 'const char*' to 'int'
Read it as: file main.cpp, line 5, column 11, and then the reason. Beginners often see a wall of errors and panic. The rule: fix the first error and recompile. Later errors are usually echoes of the first one.
You will meet the main types properly in unit 2. For now you only need int (whole numbers), double (decimals), and std::string (text).
Quiz
In lesson 1-1 you learned C++ compiles before running. Given that, when does C++ catch `count = "five";` where count is an int?
Code exercise · cpp
Your turn: this program does not compile because two variables have the wrong types. Fix the two declarations (do not change the values or the prints) so it compiles and prints the expected output.
Code exercise · cpp
Your turn: this program has two type errors — a decimal stored in an int and a number stored in a string. Fix the declarations so it compiles and prints: Sam is 21 with GPA 3.8