C++
C plus abstractions with no run-time tax: classes, templates, and RAII. Powerful and large.
Level: intermediate
What is it?
C++ extends C with classes, templates, exceptions, and a rich standard library (vector, string, map, algorithms). Its defining idea is RAII: a resource is acquired in a constructor and released in the destructor, so cleanup is tied to scope and runs even when an exception unwinds the stack. Modern C++ (C++11 and later) favors smart pointers, move semantics, and range-based loops over raw new and delete.
Why does it matter?
C++ lets you write high-level, generic code that compiles down to what a hand-written C version would do — the 'zero-overhead' principle. It powers game engines, browsers, databases, trading systems, and much of the infrastructure other languages run on.
Mental model
A professional kitchen with every tool hung on the wall. You can cook anything, exactly the way you want, and nothing is done for you — so you need to know which tool you are reaching for and how to put it back.
How it works
- RAII: objects own resources and release them in destructors. Prefer std::vector, std::string, and smart pointers over manual new/delete.
- std::unique_ptr expresses sole ownership; std::shared_ptr shares ownership through a reference count.
- Templates generate type-specific code at compile time, giving generic containers and algorithms with no run-time overhead.
- Move semantics transfer resources from temporaries instead of copying them.
- Virtual functions provide run-time polymorphism through a vtable; non-virtual calls are resolved at compile time.
- Undefined behavior from C is still present. Compilers and sanitizers help, but the burden remains on you.
Steps
- Write classes and templates
- Compile with type checking
- Instantiate templates
- Link and run
- Destructors release resources at scope end
Code example: RAII: cleanup is tied to scope
#include <iostream>
#include <memory>
#include <vector>
int main() {
std::vector<int> v{3, 1, 2};
auto p = std::make_unique<int>(42); // freed automatically
for (int x : v) std::cout << x << ' ';
std::cout << *p << '\n'; // 3 1 2 42
} // v and p release their memory hereReal-world usage
- Game engines and graphics
- Browsers, databases, and compilers
- Low-latency finance and embedded systems
Common mistakes
- Mixing new/delete with smart pointers and freeing something twice.
- Returning a reference to a local object.
- Slicing a derived object when copying it into a base-class value.
Interview questions
What is RAII?
Resource Acquisition Is Initialization: an object acquires a resource in its constructor and releases it in its destructor. Because destructors run when scope ends, even during exception unwinding, resources are not leaked.
unique_ptr versus shared_ptr?
unique_ptr has exactly one owner and no overhead over a raw pointer; it can be moved but not copied. shared_ptr allows several owners with a reference count and frees the object when the last owner goes away.
What is a virtual function?
A member function resolved at run time through the object's dynamic type, usually via a vtable pointer. It enables polymorphism through base-class pointers or references.