C++ Tutorial
C++ is one of the most widely used programming languages in the world. It powers game engines, web browsers, databases, trading systems, cars and the apps on your phone. It was created by Bjarne Stroustrup in the 1980s as "C with classes", and it has grown into a modern language that is both fast and expressive.
This tutorial takes you from your first program to classes, templates, the Standard Template Library (STL), exceptions and smart pointers. Each topic explains one idea in plain words with short examples. Every example has a Try it in the editor button that opens it in our online C++ compiler in a new tab, where you can change it and run it. Nothing needs to be installed.
How this tutorial is organised
- Basics: output, variables, input, data types, operators, strings, decisions, loops, arrays, references and pointers.
- Functions: parameters, overloading, recursion and lambdas.
- Object-oriented programming: classes, constructors, encapsulation, inheritance, polymorphism, operator overloading and templates.
- The standard library: vectors, lists, stacks, queues, sets, maps, iterators and algorithms.
- More C++: files, exceptions, smart pointers, time and threads.
Use the topic list to jump anywhere, or follow Previous and Next to read in order.
Your first look at C++
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}Hello, World!
The next topic, Get Started, explains every line.
C or C++?
C++ grew out of C, and most C code is also valid C++. C++ adds classes, references, templates, exceptions and a large standard library, which let you write programs at a higher level without losing speed. You do not need to know C first: this tutorial starts from zero. If you know C already, you will move through the basics quickly.
Tips for learning C++
- Run every example, then change it. Predict the output before you press Run.
- Read the compiler's messages from the top. The first error usually causes the others. The editor adds a hint in plain words.
- Use Safe mode. It catches a vector index outside its size and memory that was never freed.
- Prefer the standard library.
std::string,std::vectorand smart pointers solve most problems more safely than raw arrays and pointers.
Practice exercises
- Run the example above, then make it print your name.
- Add a second line of output.
- Remove
#include <iostream>, run the program and read the error.