C++ Introduction

C++ · Lesson 1 of 32 7 min read Updated

C++ is one of the most widely used programming languages in the world. The browser you are reading this in, the game engine behind many mobile and PC games, the database that stores your bank balance and the software inside a car's braking system are very likely written, at least partly, in C++.

In this lesson you will learn what C++ is, why people still choose it after more than forty years, how a C++ program goes from text to a running app, and which version of the language this tutorial teaches.

What is C++?

C++ is a general-purpose, compiled programming language. "General-purpose" means you can build almost anything with it: command-line tools, desktop apps, games, servers and code for small devices. "Compiled" means a program called a compiler translates your source code into machine code (the instructions your processor understands) before the program runs.

Bjarne Stroustrup started creating C++ in 1979 at Bell Labs. He wanted the speed and hardware control of the C language plus a way to organise large programs into classes. The early version was called "C with Classes" and was renamed C++ in 1983. The name is a small joke: ++ is the operator that adds one to a variable, so C++ means "one more than C".

Here is a tiny C++ program. Do not worry about every symbol yet; you will write it yourself in Your First C++ Program.

C++
#include <print>

int main() {
    std::println("Namaste from C++!");
    std::println("2 + 3 = {}", 2 + 3);
}
Output
Namaste from C++!
2 + 3 = 5

The {} inside the text is a placeholder. std::println replaces it with the value that follows, here the result of 2 + 3, and then moves to a new line.

Why learn C++?

  • Speed. C++ code compiles to fast machine code with no virtual machine in between. That is why game engines, trading systems and video tools use it.
  • Control. You decide how memory is used, which matters on phones, cars, routers and other devices with limited resources.
  • Zero-overhead abstractions. You can write high-level code with classes, templates and ready-made containers, and the compiler still produces code as fast as hand-written low-level code. You do not pay for features you do not use.
  • Huge existing codebase. Chrome, Firefox, Unreal Engine, MySQL, Microsoft Office and Adobe tools contain large amounts of C++. Companies need people who can maintain and improve that code.
  • Strong foundation. Once you understand C++, learning Java, C#, Rust or Go becomes much easier, because you know what happens underneath.

The price is that C++ is a large language with sharp edges. You can make mistakes that other languages prevent, such as reading memory you no longer own. This tutorial teaches the modern style, which avoids most of those mistakes by design.

How a C++ program is built

Running a C++ program is a two-step process. First you compile the source file, then you run the program file the compiler produced.

Terminal
g++ -std=c++23 hello.cpp -o hello
./hello
Output
Namaste from C++!
2 + 3 = 5

The first command asks the GCC compiler (g++) to read hello.cpp, follow the C++23 rules (-std=c++23) and write a program called hello (on Windows, hello.exe). The second command runs it. If your code has a mistake, the compiler stops with an error message and no program is created. This is helpful: many bugs are caught before the program ever runs.

Behind the scenes the compiler does several jobs: the preprocessor handles lines that start with #, the compiler turns each source file into an object file, and the linker joins the object files and libraries into one program. You will see these words in error messages, so it helps to know them.

C++ compared with C, Java and Python

FeatureCC++JavaPython
Runs asMachine codeMachine codeBytecode on the JVMInterpreted bytecode
Classes and objectsNoYesYesYes
Memory managed byYouYou, helped by RAII and smart pointersGarbage collectorGarbage collector
Typical speedVery fastVery fastFastSlower
Generic codeMacrosTemplates and conceptsGenericsDuck typing

C++ started as an extension of C, and most C code still compiles as C++. Modern C++ code, however, looks very different from C, as you will see throughout this course.

Versions of C++: old and new

C++ is defined by an international standard written by an ISO committee called WG21. A new version comes out roughly every three years, named after its year.

VersionHighlights
C++98 / C++03First standard: classes, templates, the Standard Template Library (STL)
C++11A huge update: auto, lambdas, smart pointers, move semantics, threads
C++14 / C++17Smaller updates: std::optional, std::string_view, structured bindings
C++20Concepts, ranges, modules, std::format, coroutines
C++23std::print, std::expected, the std module, more ranges tools

This tutorial teaches C++23, published by ISO in 2024 as ISO/IEC 14882:2024. The next version, C++26, is being finalised; you will get a preview in C++26 Preview. The full story of how the language changed is in From C++98 to C++23.

Common mistakes

  • Thinking C++ is just "C with classes". Modern C++ has its own style: containers instead of raw arrays, smart pointers instead of manual delete, and std::string instead of character arrays.
  • Learning from very old material. A book that uses #include <iostream.h> or void main() is teaching code that no longer compiles on modern compilers. Check that your material covers C++11 or later.
  • Forgetting to compile again. After you edit a .cpp file, you must compile it again before running. Running the old program shows the old behaviour.

Key points

  • C++ is a fast, general-purpose, compiled language created by Bjarne Stroustrup, starting in 1979.
  • A compiler translates .cpp source files into a program; errors are reported before the program runs.
  • C++ is used in browsers, games, databases, finance and embedded devices.
  • The language is standardised by ISO; this course teaches C++23, and C++26 is the next version.
  • Modern C++ avoids most classic memory mistakes by using the standard library well.

Exercise

Write down three apps or devices you use every day and guess whether they might contain C++ code (for example a browser, a game or a car). Then look at the small program in this lesson and answer: what would it print if the second line were std::println("10 + 5 = {}", 10 + 5);?

Show solution

Good examples are Chrome or Firefox, a game built on Unreal Engine and the control software in a car or a washing machine. The changed program prints:

C++
#include <print>

int main() {
    std::println("Namaste from C++!");
    std::println("10 + 5 = {}", 10 + 5);
}
Output
Namaste from C++!
10 + 5 = 15

The text 10 + 5 = is printed exactly as written, and {} is replaced by the calculated value 15.

Quick Quiz

1. What does a C++ compiler do?

2. Who created C++?

3. Which C++ version does this tutorial teach?

4. What does {} mean inside std::println("Total: {}", 5 + 4);?

Learn C++ with a mentor

Free lessons get you started. Our Java, C & C++ Programming training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call