C++

C++ was created by Bjarne Stroustrup at Bell Labs, starting in 1979 as "C with Classes" and renamed in 1983. It keeps the machine-level control of C and adds abstraction on top, under one rule: what you do not use, you do not pay for.

Paradigm: Systems Multi-Paradigm Languages

What Makes C++ Special

  • Zero-overhead abstraction. Classes, templates and lambdas are resolved at compile time, so well-written high-level code can run as fast as hand-written low-level code.
  • Classes and virtual dispatch. Inheritance and virtual functions give object-oriented design, and you choose where to pay for run-time dispatch.
  • Templates (generic programming). A function or class written once works for any type that supports the operations it uses. The Standard Template Library (STL) is built this way.
  • RAII. Resources are tied to object lifetime. A destructor runs when the object goes out of scope, so memory, files and locks are released without a garbage collector. Smart pointers such as std::unique_ptr apply this to heap memory.
  • Functional additions. Lambdas (C++11), std::function, algorithms like std::accumulate, and ranges (C++20) support a functional style.
  • Compile-time computation. constexpr lets ordinary functions run during compilation, and concepts (C++20) state constraints on template arguments.

Example: Objects, templates, RAII and a lambda

#include <algorithm>
#include <iostream>
#include <memory>
#include <numeric>
#include <string>
#include <vector>

// object-oriented: an interface with virtual dispatch
struct Shape {
    virtual ~Shape() = default;
    virtual double area() const = 0;
};
struct Square : Shape {
    double s;
    explicit Square(double s) : s(s) {}
    double area() const override { return s * s; }
};
struct Rect : Shape {
    double w, h;
    Rect(double w, double h) : w(w), h(h) {}
    double area() const override { return w * h; }
};

// generic: works for any type that can be compared with <
template <typename T>
T largest(const std::vector<T>& v) {
    return *std::max_element(v.begin(), v.end());
}

int main() {
    // RAII: unique_ptr frees each shape automatically
    std::vector<std::unique_ptr<Shape>> shapes;
    shapes.push_back(std::make_unique<Square>(2));
    shapes.push_back(std::make_unique<Rect>(3, 4));

    // functional: a lambda passed to a standard algorithm
    double total = std::accumulate(shapes.begin(), shapes.end(), 0.0,
        [](double acc, const auto& s) { return acc + s->area(); });

    std::cout << total << "\n";                                        // 16
    std::cout << largest(std::vector<int>{3, 9, 4}) << "\n";           // 9
    std::cout << largest(std::vector<std::string>{"pear", "apple"}) << "\n";  // pear
}

How It Works

  • Shape is an abstract class. Square and Rect override area(), and the call s->area() picks the right one at run time.
  • largest is a template. The compiler generates one version for int and another for std::string, with no run-time cost for the generality.
  • std::unique_ptr owns each shape. When shapes goes out of scope, every shape is destroyed. There is no delete in the program.
  • The lambda [](double acc, const auto& s) {...} is passed to std::accumulate to fold all areas into a total.

History and Where It Is Used

C++ is used for operating systems, browsers, game engines, databases, compilers, trading systems and embedded software, wherever performance and control matter. It is standardized by ISO, with a new revision every three years (C++11, 14, 17, 20, 23). Its long history means old and modern styles coexist, so the language is large and takes time to learn. See also the full C++ roadmap on this site.

Learn More