C++ Tutorial

C++ is a popular object-oriented programming language. TutorialsClass helps you to Learn C++ Programming Language for free.

C++ is generally used in applications. With the help of this tutorial, you will learn C++ language with examples step by step.

C++ Introduction

C++ is a fast, general purpose programming language. It was created by Bjarne Stroustrup at Bell Labs, as an extension of C with classes, and it is one of the most used…

C++ History and Versions

The story of C++ is the story of making C better for big programs. Short timeline Year Event 1979 Bjarne Stroustrup starts “C with Classes” at Bell Labs 1983 The name is…

C++ Features

Compiled and fast: the code is changed to machine code, so there is almost no overhead. Object oriented: classes, objects, inheritance, polymorphism, encapsulation. Multi paradigm: you can write procedural, object oriented and…

C++ vs C

C++ started as an extension of C, and most C code still compiles in C++. But the two languages are now different, and C++ has much more. Point C C++ Style Procedural…

C++ Installation and Setup

To write C++ you need a compiler, which changes your code to a program. The most used ones are GCC (g++), Clang and MSVC (Visual Studio). Install the compiler Windows Visual Studio…

Your First C++ Program: Hello World

The classic first program prints a message on the screen. Understand every line Code Meaning #include <iostream> Adds the input and output library, so we can use std::cout int main() The starting…

C++ Syntax

Syntax means the rules of the language. Here are the main ones for C++. The rules Every statement ends with a semicolon ;. A block of code is inside curly braces {…

C++ Comments

Comments are notes in the code. The compiler ignores them. They explain the code to humans. Single line comment Multi line comment Multi line comments can not be nested. A /* inside…

C++ Input and Output: cin, cout and std::print

C++ reads and writes data with streams. std::cout is the output stream, std::cin is the input stream. They are in the header <iostream>. Output with cout Input with cin Read a full…

C++ Variables

A variable is a named place in memory with a type. The type says which values it can keep and how much memory it uses. Declare and initialize Always initialize A local…

C++ Data Types

The data type tells the compiler how much memory a variable needs and what operations are allowed. Fundamental types Type Keeps Typical size bool true or false 1 byte char One character…

C++ Type Conversion and Casting

Type conversion changes a value from one type to another. C++ has automatic (implicit) and manual (explicit) conversion. Implicit conversion Explicit conversion with static_cast In C++ use static_cast. It is clear and…

C++ Operators

Operators work on values and variables. Arithmetic operators Assignment, increment and decrement Comparison and logical operators Operator Meaning == != Equal, not equal < > <= >= Compare sizes <=> Three way…

C++ Strings: std::string and string_view

In C++ use std::string (in <string>), not the C style char array. It grows by itself and has many helper functions. Basics Search and change Compare In C++ == compares the content…

C++ if, else if and else

The if statement runs code only when a condition is true. if, else if and else Conditions with &&, || and ! What is true? In C++ the number 0 is false…

C++ switch Statement

switch chooses one path from many, by the value of an integer, a char or an enum (not a string or a double). Fall through Without break the code goes on to…

C++ for Loop and Range based for

A for loop repeats code a known number of times. Steps, counting down and sums Nested loops Range based for loop The easy way to go through a container or an array:…

C++ while and do while Loop

Use while when you repeat until a condition changes, and you do not know the count. do while: at least one time Digits of a number Ask until the input is valid…

C++ break, continue and goto

break stops a loop at once. continue skips to the next round. Search with break Leave nested loops break leaves only the inner loop. To leave all, use a flag, or put…

C++ Arrays and std::array

An array keeps many values of the same type in a row. The size is fixed, and the index starts at 0. C style array std::array: the better array std::array (in <array>)…

C++ Vectors (std::vector)

std::vector is a dynamic array: it can grow and shrink. It is the container you should use most of the time. It is in the header <vector>. Basics Create in different ways…

C++ Functions

A function is a named block of code which you can call many times. Declaration and definition A function must be known before you call it. When the definition comes later, write…

C++ Function Overloading and Default Arguments

In C++ many functions can have the same name if their parameters are different. This is function overloading. The compiler chooses the right one from the arguments. A different return type alone…

C++ References and const References

A reference is another name (an alias) for an existing variable. It is written with & in the declaration. Pass by reference With a reference parameter, the function works on the original…

C++ Pointers

A pointer is a variable which keeps the memory address of another variable. This is the most important and most difficult idea of C++. Address and dereference nullptr A pointer which points…

C++ Dynamic Memory: new, delete and Smart Pointers

Normal variables live on the stack and are removed at the end of their block. Memory which must live longer is taken from the heap (free store) with new, and must be…

C++ Classes and Objects

C++ is an object oriented language. A class is a blueprint, and an object is a real thing made from it. A class has data members (variables) and member functions (methods). Every…

C++ Constructors and Destructors

A constructor runs when an object is created. It has the same name as the class and no return type. A destructor runs when the object is destroyed, and its name is…

C++ Access Specifiers and Encapsulation

Encapsulation means keeping the data private and giving controlled access with public methods, so the object can not get a wrong state. Specifier Who can use the member public Everyone protected The…

C++ Inheritance

Inheritance lets a derived class reuse the members of a base class. It models an “is a” relation: a Dog is an Animal. Constructor and destructor order The constructors run from the…

C++ Polymorphism and Virtual Functions

Polymorphism means one call, many behaviors. In C++ it needs virtual functions and a base pointer or reference. virtual, override and final virtual in the base: the function can be replaced, and…

C++ Abstract Classes and Interfaces

An abstract class has at least one pure virtual function (written with = 0). You can not create an object of it. It defines a contract that the derived classes must follow….

C++ Operator Overloading

You can give the normal operators a meaning for your own classes. This is operator overloading. It makes the code natural, for example a + b for two vectors or two money…

C++ Templates and Generic Programming

A template lets you write one function or class for many types. The compiler makes the real code for each type you use. This is called generic programming, and it is the…

C++ Enums, Structs and Namespaces

enum class An enum class (scoped enum) is a set of named constants. It is safer than the old enum, because the names do not leak out, and there is no hidden…

C++ Exceptions: try, catch and throw

An exception is a way to report an error. When code throws, the normal flow stops, and C++ goes up the calls until a matching catch. Catch by const reference Always catch…

C++ STL Containers: map, set, unordered_map, queue and stack

The Standard Template Library (STL) gives you ready containers. You already know vector and array. Here are the others. map: key and value, sorted Careful: marks[“Karan”] on a missing key creates it…

C++ Algorithms and Iterators

The header <algorithm> has ready functions which work on ranges described by iterators. An iterator is like a pointer to an item of a container. begin() is the first and end() is…

C++ Lambda Expressions

A lambda (C++11) is a small nameless function which you write in place. It is mostly used with algorithms. The capture list The square brackets say which variables of the outside the…

C++ File Handling: fstream, ifstream and ofstream

C++ reads and writes files with streams, the same way as cout and cin. Include <fstream>. Class Use std::ofstream Output file stream: write std::ifstream Input file stream: read std::fstream Both Write and…

C++ Move Semantics and the Rule of Five

Copying a big object (like a vector of a million items) is slow. If the old object is not needed anymore, C++11 lets you move its data to the new object: it…

Modern C++ Features: ranges, optional, variant and format

Modern C++ is much easier and safer than the old style. These are the features to know. std::optional: a value which may be missing std::variant: one of many types Ranges and views…

C++ Multithreading: thread, mutex and async

C++11 added threads to the standard library. Include <thread>. On Linux, compile with -pthread. A std::thread must be joined (or detached) before it is destroyed. If not, the program ends with std::terminate….

C++ Recursion

In recursion a function calls itself to solve a smaller copy of the problem. Every recursion needs a base case (where it stops) and a recursive case. Fibonacci and memoization The plain…

C++ Cheat Sheet

A quick reference of the most used C++ syntax. Every example was compiled with GCC 16 and -std=c++23. Program, variables and types Control flow Containers and algorithms Functions, references and pointers Syntax…