Featured image of post C++ Cheatsheet

C++ Cheatsheet

C++ Cheatsheet

ConceptSyntax/ExampleDescription
Variablesint x = 5;Declaring a variable
Data Typesint, float, double, char, stringCommon data types in C++
Arraysint arr[5] = {1, 2, 3, 4, 5};Declaring an array
Pointersint* ptr = &x;Declaring a pointer
Functionsvoid myFunction() { ... }Defining a function
If Statementsif (x > 0) { ... }Basic if statement
Loopsfor (int i = 0; i < 5; i++) { ... }For loop
While Loopswhile (x < 5) { ... }While loop
Switch Statementsswitch (x) { case 1: ...; break; }Switch statement
Classesclass MyClass { ... };Defining a class
Inheritanceclass ChildClass : public ParentClass {}Inheriting a class
Exception Handlingtry { ... } catch (exception& e) { ... }Handling exceptions
File I/Oifstream file("file.txt");Reading from a file
Constructors/DestructorsMyClass() { ... } ~MyClass() { ... }Constructor and Destructor
Namespacesnamespace ns { ... }Defining a namespace