Programming

C++

What is C++?

C++ adds classes, templates, and a larger standard library on top of C. The program compiles to machine code. The programmer usually decides when memory is taken and returned. That gives speed, and it also makes a mistake expensive.

What is C++ used for?

C++ stays where every millisecond is counted.

  • A game engine
  • A fast part of a browser
  • Code close to the operating system
  • Scientific and graphics computation

What can you do with C++?

With C++ you can process a large amount of data with little memory, write a game loop, and call an existing C library. For a small web page and an ordinary mobile form it is usually too heavy.

Is C++ hard to learn?

It is hard to learn. Pointers, lifetimes, and template errors stop a beginner. Seeing C or another language first helps, but it is not required. The first useful program comes later than in Go or Python.

Advantages and limits

Advantage: speed, control, and a large body of industry code. Limit: the build can be slow, a memory mistake is dangerous, and it is too much for a simple page. Rust checks memory more strictly in the same area.

Basic ideas

A header holds a declaration. A source file holds the implementation. #include sees another file. new takes memory. delete or a smart pointer gives it back.

  • A class keeps state and functions together
  • A template fills in a type later
  • RAII ties a resource to an object’s life
  • CMake or another system describes the build

A short example

This program writes one line to standard output and ends with 0.

#include <iostream>

int main() {
    std::cout << "Hello\n";
    return 0;
}

Common questions

  • Are C and C++ the same? No. C++ adds classes and templates and often accepts old C code.
  • Is C++ for a website? Not the main path. It can appear in a fast service.
  • Must every new project be C++? No. For a simple tool, Go, Python, or Java is a shorter path.