Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Introduction to C++  ›  Lesson

Why Use C++

Why use C++ 12 minC++ is worth its extra effort when a program needs raw speed, predictable timing or direct control of memory and hardware.

Why Use C++

Rocket Run has a budget: at 120 frames per second, every frame must be computed and drawn in about 8 milliseconds. Miss that and the rocket stutters. Some languages pause now and then to clean up memory; C++ never does unless you ask it to. That is the kind of promise people pick C++ for.

The rule

Choose C++ when speed and control matter more than writing speed.

What you get:

  • Speed. Code is compiled to machine code and the compiler optimises it hard.
  • Zero overhead. A guiding rule of the language: you do not pay for features you do not use, and the features you do use should cost no more than code you could write by hand.
  • Predictable timing. Objects are cleaned up at a known moment, when they go out of scope, not whenever a garbage collector decides.
  • Control. You choose where data lives in memory and can talk to hardware and to existing C libraries directly.
  • Reach. Compilers exist for almost every processor, from microcontrollers to supercomputers.

That is why game engines, web browsers, databases, trading systems, audio software and car firmware are so often written in it.

What you pay:

  • Slower builds and a larger language to learn.
  • More responsibility: a mistake with memory can crash the program or, worse, quietly corrupt it.
// Called 120 times a second: no pauses, no hidden allocations
void update(double& rocketY, double speed, double seconds) {
    rocketY += speed * seconds;
}

The shortcut

Ask three questions. Two or more "yes" answers point to C++.

  1. Is the processor the bottleneck, or does timing need to be predictable?
  2. Must it run on limited hardware or talk to C and C++ code that already exists?
  3. Is the team ready to own memory and a longer build?
Rocket Run's physics engineStep 1 of 4
  1. Question 1: bottleneck?

    Yes. Collision checks for hundreds of asteroids run every 8 milliseconds.

The trap is using C++ out of habit. A one-off script to rename files answers "no" to all three questions, and a scripting language finishes the job before a C++ build has finished configuring.

Drills

Answer fast — trust the rule.

quick drill

Which project is the strongest fit for C++?

quick drill

What does the zero-overhead principle promise?

quick drill

Which is a real cost of choosing C++?

quick drill

Why might Rocket Run's leaderboard website not need C++?

Remember

  • Choose C++ when speed and control matter more than writing speed.
  • Shortcut: bottleneck or timing? Hardware or existing code? Ready for the cost? Two yeses means C++.
  • Trap: the power comes with responsibility for memory and longer builds.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainEngine or leaderboard?

Rocket Run has two parts: the game engine that draws 120 frames a second, and a small website that shows the top ten scores. Explain which part you would write in C++ and which you might not, and why.