C++: Introduction to C++
If you ask ten programmers "what's the hardest language?", at least five will say C++. But if you ask "what language has the best performance, finest control, and can write low-level systems?", all ten will say C++. In this lesson we won't write any code — let's first get to know C++: who it is, where it came from, what it can do, and whether it's worth learning.
1. What is C++?
C++ is a compiled, statically typed, multi-paradigm programming language. In plain terms:
- Compiled — Your code must be compiled into machine code before it can run (like cooking: prep the ingredients first, then cook)
- Statically typed — Every variable's type is determined at compile time (like filling out a form: each field has a specified type)
- Multi-paradigm — Supports procedural, object-oriented, and generic programming (like a toolbox: wrench/screwdriver/drill — it has them all)
(1) Its Relationship with C
C++ is C's "super upgrade" — in 1983, Danish programmer Bjarne Stroustrup added classes to the C language, enabling C to do object-oriented programming as well.
2. Why Learn C++?
| Reason | Explanation |
|---|---|
| Performance ceiling | C++ compiles to machine code that runs directly on hardware — no VM, no interpreter, no garbage collector — blazingly fast |
| Extreme control | How memory is allocated, how CPU caches are used, how pointers jump — you're in charge |
| Broad career opportunities | Game engines, high-frequency trading, embedded systems, autonomous driving, operating systems — all C++ territory |
| Solid foundation | After learning C++, the "under-the-hood logic" of other languages (Java/Python/Go) becomes transparent |
| Long-term value | C++ has been around for 40+ years and is still evolving (C++23 just released) — it won't become obsolete |
▶ Example 1: Hello World (Difficulty ⭐)
#include <iostream>
int main() {
std::cout << "Hello, C++ World!" << std::endl;
return 0;
}
Output:
Hello, C++ World!
Run result:
Hello, C++ World!
#include <iostream>— Include the input/output library (like Python'simport)int main()— Program entry point (like Python'sif __name__ == "__main__":)std::cout << "..."— Output to screen (like Python'sprint())return 0;— Tell the OS "the program ended normally" (0 means success)
▶ Example 2: How Fast is C++? (Difficulty ⭐⭐)
#include <iostream>
#include <chrono>
int main() {
// Use chrono for high-precision timing
auto start = std::chrono::high_resolution_clock::now();
// Run 1 billion empty iterations
long count = 0;
for (long i = 0; i < 1'000'000'000; i++) {
count++;
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration<double>(end - start).count();
std::cout << "1 billion loops time: " << duration << " seconds" << std::endl;
return 0;
}
Output:
1 billion loops time: 0.5 seconds
chrono library used here was introduced in C++11 and provides nanosecond-level precision. You don't need to understand it yet — just get a feel for C++'s style: #include to import libraries, int main() as the entry point, std::cout for output, and using namespace std; to avoid writing std:: every time.
3. C++ vs C vs Python
| Dimension | C | C++ | Python |
|---|---|---|---|
| Type | Compiled | Compiled | Interpreted |
| Paradigm | Procedural | Multi-paradigm (procedural+OOP+generic) | Multi-paradigm (OOP+functional) |
| Memory management | Manual (malloc/free) | Manual+smart pointers (new/delete/unique_ptr) | Automatic (garbage collection) |
| Performance | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐ |
| Learning difficulty | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐ |
| Typical applications | OS kernels, drivers | Game engines, HFT, browser engines | Data analysis, AI, Web backends |
| Code volume | High | High | Low |
| Error friendliness | Poor (segfault? guess yourself) | Medium (compile errors are strict) | Good (clear error messages) |
(1) Real-life Analogy
- C — Manual transmission race car: fast, but you have to manage the clutch, shift gears, and control RPM yourself — beginners easily stall (segfault)
- C++ — Manual transmission race car + GPS + backup sensors: still fast, but with manyauxiliaryfeatures (classes/templates/smart pointers) — used well, it's safer than C
- Python — Self-driving electric car: comfortable, writing code is like talking, but when you want to "race" it just can't keep up
4. C++ Standard Evolution
C++ has gone through multiple standard updates since its inception. Each update adds a bunch of new features, making C++ increasingly powerful and convenient.
| Year | Standard | Alias | Key New Features |
|---|---|---|---|
| 1998 | C++98 | First international standard | Templates, exceptions, RTTI |
| 2003 | C++03 | Minor patch | Bug fixes |
| 2011 | C++11 | C++0x | Major update: auto, range-based for, nullptr, Lambda, shared_ptr, thread, initializer lists |
| 2014 | C++14 | — | Minor enhancements (make_unique, generic Lambda) |
| 2017 | C++17 | — | Structured bindings, if constexpr, fold expressions, string_view, filesystem library |
| 2020 | C++20 | — | Modules, Coroutines, Concepts, Ranges |
| 2023 | C++23 | — | std::print, flat_map, enhanced modularity |
(1) Which Standard Does This Course Use?
C++17 — striking a balance between "new features are useful" and "compiler support is solid".
- C++11 is the "must-learn" baseline (the starting point of modern C++)
- C++17 is the "recommended" sweet spot (most new features are stable)
- C++20 is cool, but some features have incomplete MinGW support, so we'll only touch on them briefly
auto you learn is from C++11, std::string_view is from C++17. If an interviewer asks "what new features does C++11 have?", that's a high-frequency question.
5. What Can C++ Do?
C++'s application domains can be summarized in one sentence: "Places that demand extreme performance, require fine-grained hardware control, and cannot tolerate errors."
(1) Scenario 1: Game Development (Most Iconic)
- Game engines: Unreal Engine is written in C++
- Game logic: Many AAA titles have core logic in C++ (graphics rendering/physics engines/AI pathfinding)
- Why C++?: Games must render 60 frames per second, and each frame must be computed within 16 milliseconds — Python can't do that
(2) Scenario 2: High-Frequency Trading (Most Lucrative)
- Quantitative trading systems: HFT systems on Wall Street/in London require microsecond-level latency
- Why C++?: Python takes microseconds just to start a function call; C++ can be precise to nanoseconds
(3) Scenario 3: Embedded Systems & IoT (Most Widespread)
- Microcontroller programs: Washing machines, microwaves, smartwatches — all C/C++
- Automotive electronics: Tesla's in-vehicle system has a lot of C++ code
- Why C++?: Embedded devices have limited memory (a few KB to a few MB) — only C++ can finely control memory
(4) Scenario 4: Browser & Database Engines (Most Fundamental)
- Browser engines: Chrome's V8 engine (which runs JavaScript), Edge's Blink — all C++
- Databases: MySQL, PostgreSQL, MongoDB core engines — all C/C++
- Why C++?: These infrastructure systems handle billions of requests daily — even a tiny performance hit causes global slowdowns
(5) Scenario 5: Operating Systems & Drivers (Lowest Level)
- Operating systems: Windows kernel (partially), Linux kernel (partially), macOS kernel (partially) — all C/C++
- Why C++?: Operating systems must interact directly with hardware — only C/C++ can directly manipulate memory and hardware registers
6. Common Application Scenarios
- High-performance servers: Instant messaging (WeChat backend), live streaming — C++ handles the networking layer, supporting millions of concurrent connections
- Graphics & image processing: Photoshop's core algorithms, OpenCV (computer vision library) — all C++
- Blockchain: Bitcoin, Ethereum core nodes — C++ ensures performance and security
- Scientific computing: Large-scale numerical simulations (weather forecasting/nuclear physics simulation) — C++ matrix operations can max out CPU
▶ Example 3: Simple User Interaction (Difficulty ⭐)
#include <iostream>
#include <string>
int main() {
std::string name;
int age;
std::cout << "Please enter your name: ";
std::cin >> name;
std::cout << "Please enter your age: ";
std::cin >> age;
std::cout << "Hello," << name << "!Next year you " << (age + 1) << " years old。" << std::endl;
return 0;
}
Output:
Please enter your name:
Please enter your age:
Hello, ! Next year you years old。
Run result:
Please enter your name: Xiao Ming
Please enter your age: 18
Hello,Xiao Ming!Next year you 19 years old。
std::cin reads user input from the keyboard, std::cout outputs to the screen. std::string is C++'s string type, which requires #include <string>.
❓ FAQ
new/delete yourself — forget and you get memory leaks. But Java has VM performance overhead, so game engines/HFT still use C++.std::string).vector/map), algorithms (sort/find), iterators. Learning STL boosts efficiency 10x; Phase 6 will cover it in depth.📖 Summary
- C++ is C's upgrade, supporting OOP, with top-tier performance
- Core reasons to learn C++: extreme performance, fine-grained control, broad career opportunities, solid foundation
- C++ vs Python: the former is compiled/fast/hard, the latter is interpreted/slow/easy — different scenarios, each has its strengths
- C++ standards keep evolving: C++11 (modern C++ starting point), C++17 (used in this course), C++20 (cool but incomplete support)
- C++ application domains: game engines, HFT, embedded systems, browser engines, operating systems — all areas demanding "extreme performance"
- STL (Standard Template Library) is C++'s killer feature, covered in depth in Phase 6
📝 Exercises
-
Basic (Difficulty ⭐): Search for "famous software/games/systems written in C++" and list at least 5 to appreciate C++'s breadth of applications.
-
Intermediate (Difficulty ⭐⭐): Do you have a C++ compiler installed on your computer? Open a terminal/command line and type
g++ --versionorclang++ --versionto see if there's any output. If not, note your OS version (Windows 10/11? macOS? Which Linux distro?) — next lesson we'll teach you how to install one. -
Challenge (Difficulty ⭐⭐⭐): C++ has a famous saying — "Learning only C++ syntax without learning computer fundamentals is basically useless." Search and read an article about "what computer fundamentals you need to learn C++" (try searching "do you need to learn computer architecture before C++"), and write a reflection of 200 words or less: do you think you need to learn C before C++? Why?