█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/C++/C++ vs C and the Zero-Overhead Principle
30 minIntermediate

C++ vs C and the Zero-Overhead Principle

After this lesson, you will be able to: Explain what C++ adds over C, the zero-overhead principle, and when to choose C++ over C or a higher-level language.

C++ is C + classes + templates + STL + 40 more years of features. Used where you need control AND abstractions: game engines, browsers, embedded with state, HFT, robotics.

This is a free introductory lesson. No purchase required.

What C++ adds over C

Classes + RAII (resource cleanup on scope exit). Templates (compile-time generic programming). Standard Template Library (STL): vector, map, string, algorithms. Exceptions (alternative to error codes). Namespaces + operator overloading. Smart pointers (unique_ptr, shared_ptr). Modern features: move semantics, lambdas, constexpr, concepts (C++20), ranges (C++20).

The zero-overhead principle

Bjarne Stroustrup (C++ creator): 'You don't pay for what you don't use, and what you do use is as efficient as you could hand-write.' A virtual function call costs one indirect jump, same as a C function pointer. std::unique_ptr compiles to the same machine code as a raw pointer + a free() call. Templates instantiate per-type, generating optimal code for each. Result: C++ runs as fast as C while giving you abstractions C can't express cleanly.

First C++ program

Save as hello.cpp; compile with `g++ hello.cpp -o hello -std=c++20 -Wall -Wextra`.

tsx
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> langs = {"C", "C++", "Rust"};
for (const auto& lang : langs) {
std::cout << "Hello from " << lang << "\n";
}
return 0;
}
// Note vs C:
// std::vector (auto-grows, auto-frees memory)
// std::string (no buffer overflows)
// range-based for (since C++11)
// << operator overload on cout (vs printf format strings)

💡 C++ or C first?

C first if you want to learn how computers work. C is small (~30 keywords); C++ is huge (~90+). C++ first if you're going straight to game dev, finance, or robotics, that's where C++ is the working language. LastWrite's order: C → C++. C++ assumes you already have pointers, malloc/free, and basic compilation down.

Quick Check

What is the zero-overhead principle?

Pick the cleanest summary.

Common mistakes only experienced devs catch

Calling C++ 'C with classes' (it's a separate language with very different idioms). Using `new`/`delete` instead of smart pointers + RAII. Writing Java-style deep class hierarchies (modern C++ favors composition + templates). Ignoring `const` correctness. Skipping `-Wall -Wextra`, silent UB is the #1 source of C++ bugs.

Back to C++
Classes and OOP in C++→