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 moreConstructor
Member function that runs when an object is created.
class Circle { public: Circle(double r) : radius(r) {} double radius; }; Circle c(2.0);Converting Constructor
Constructor that implicitly converts a value into the class type.
class Circle { public: Circle(double r) : radius(r) {} double radius; }; Circle c = 2.0; // Implicitly converts 2.0 to Circle.Default Constructor
Constructor that can be called with no arguments.
class Foo { public: Foo() : val(0) {} int val; }; Foo obj; // Calls Foo().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) {} };Destructor
Member function that runs when an object is destroyed.
class Buffer { public: Buffer() : data(new int[8]) {} ~Buffer() { delete[] data; } int* data; };Member Initialiser List
Initialise class member variables before the constructor body runs.
class Foo { Foo(int arg) : val(arg) {} int val; };This Pointer
Pointer to the current object.
class Circle { public: void setRadius(double radius) { this->radius = radius; } double radius; };Class
User-defined type that groups data and functions.
class Circle { public: double area() { return 3.14 * radius * radius; } double radius; };
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};