Featured
Delegating Constructor
Reduce constructor boilerplate by delegating to another constructor in the same class.
class Foo { Foo(int x) : Foo(x, 'y') {} Foo(int x, char y) : x(x), y(y) {} };Member Initialiser List
Initialise class member variables before the constructor body runs.
class Foo { Foo(int arg) : val(arg) {} int val; };Structured Binding
Unpack pairs, tuples, and aggregates cleanly.
auto [x, y] = std::pair#include <utility>{1, 2};
Recently Updated
See moreClass
User-defined type that groups data and functions.
class Circle { public: double area() { return 3.14 * radius * radius; } double radius; };Const Method
Member function that cannot modify the object it is called on.
class Foo { public: int get() const { return val; } private: int val; };Const Variable
Declare a variable whose value cannot change after initialisation.
const int size = 100; size = 200; // ERROR.Default Constructor
Constructor that can be called with no arguments.
class Foo { public: Foo() : val(0) {} int val; }; Foo obj; // Calls Foo().Reference
Alias for an existing variable.
int var = 1; int& ref = var; // ref is an alias for var. ref = 2; // var is now 2.Static Member
Class member shared by all objects.
class Shape { public: static int count; }; Shape::count;New and Delete
Allocate and free memory at runtime.
int* ptr = new int(1); delete ptr; int* arr = new int[3]{1, 2, 3}; delete[] arr;Pointer
Variable that stores the memory address of another variable.
int var = 1; int* ptr = &var; // Address of var. *ptr = 2; // var = 2.
For Beginners
See moreAccess Specifier
Set class member accessibility.
class Example { public: protected: private: };Boolalpha
Print booleans as "true"/"false" strings instead of the default 1/0.
std::cout#include <iostream> << std::boolalpha#include <iostream> << true << "\n" << false << "\n";Character Input
Write to standard input using I/O streams.
#include <iostream> std::cin#include <iostream> >> var;Character Output
Print to standard output using I/O streams.
#include <iostream> std::cout#include <iostream> << var << "\n";Delegating Constructor
Reduce constructor boilerplate by delegating to another constructor in the same class.
class Foo { Foo(int x) : Foo(x, 'y') {} Foo(int x, char y) : x(x), y(y) {} };Member Initialiser List
Initialise class member variables before the constructor body runs.
class Foo { Foo(int arg) : val(arg) {} int val; };Range-Based For Loop
Loop over containers without writing indices or iterators.
for (auto& element : container) { // Do something with element. }Structured Binding
Unpack pairs, tuples, and aggregates cleanly.
auto [x, y] = std::pair#include <utility>{1, 2};