C++ Cheat Sheet & Quick Reference
C++ Cheat Sheet & Quick Reference
C++
C++ quick reference cheat sheet that provides basic syntax and methods.
# Getting Started
hello.cpp
#include <iostream>
int main() {
std::cout << "Hello QuickRef\n";
return 0;
}
https://quickref.me/cpp.html 1/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
Variables
// Constants
const float RATE = 0.8;
https://quickref.me/cpp.html 2/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
User Input
int num;
Swap
int a = 5, b = 10;
std::swap(a, b);
Comments
If statement
if (a == 10) {
// do something
}
See: Conditionals
Loops
https://quickref.me/cpp.html 3/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
See: Loops
Functions
#include <iostream>
See: Functions
References
int i = 1;
int& ri = i; // ri is a reference to i
ri = 2; // i is now changed to 2
std::cout << "i=" << i;
https://quickref.me/cpp.html 4/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
i = 3; // i is now changed to 3
ri and i refer to the same memory location.
Namespaces
#include <iostream>
namespace ns1 {int val(){return 5;}}
int main()
{
std::cout << ns1::val();
}
#include <iostream>
namespace ns1 {int val(){return 5;}}
using namespace ns1;
using namespace std;
int main()
{
cout << val();
}
# C++ Arrays
Declaration
https://quickref.me/cpp.html 5/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
┌─────┬─────┬─────┬─────┬─────┬─────┐
| 92 | 97 | 98 | 99 | 98 | 94 |
└─────┴─────┴─────┴─────┴─────┴─────┘
0 1 2 3 4 5
Displaying
https://quickref.me/cpp.html 6/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
Multidimensional
j0 j1 j2 j3 j4 j5
┌────┬────┬────┬────┬────┬────┐
i0 | 1 | 2 | 3 | 4 | 5 | 6 |
├────┼────┼────┼────┼────┼────┤
i1 | 6 | 5 | 4 | 3 | 2 | 1 |
└────┴────┴────┴────┴────┴────┘
int x[2][6] = {
{1,2,3,4,5,6}, {6,5,4,3,2,1}
};
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 6; ++j) {
std::cout << x[i][j] << " ";
}
}
// Outputs: 1 2 3 4 5 6 6 5 4 3 2 1
https://quickref.me/cpp.html 7/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
# C++ Conditionals
If Clause
if (a == 10) {
// do something
}
if (number % 2 == 0)
{
std::cout << "even";
}
else
{
std::cout << "odd";
}
// Outputs: even
Else if Statement
https://quickref.me/cpp.html 8/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
- Operators
Relational Operators
a == b a is equal to b
a != b a is NOT equal to b
a > b a is greater b
Assignment Operators
a += b Aka a = a + b
a -= b Aka a = a - b
a *= b Aka a = a * b
a /= b Aka a = a / b
https://quickref.me/cpp.html 9/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
a %= b Aka a = a % b
Logical Operators
Bitwise Operators
a | b Binary OR
a ^ b Binary XOR
Ternary Operator
int x = 3, y = 5, max;
max = (x > y) ? x : y;
// Outputs: 5
std::cout << max << std::endl;
https://quickref.me/cpp.html 10/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
int x = 3, y = 5, max;
if (x > y) {
max = x;
} else {
max = y;
}
// Outputs: 5
std::cout << max << std::endl;
Switch Statement
int num = 2;
switch (num) {
case 0:
std::cout << "Zero";
break;
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
case 3:
std::cout << "Three";
break;
default:
std::cout << "What?";
break;
}
https://quickref.me/cpp.html 11/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
# C++ Loops
While
int i = 0;
while (i < 6) {
std::cout << i++;
}
// Outputs: 012345
Do-while
int i = 1;
do {
std::cout << i++;
} while (i <= 5);
// Outputs: 12345
Continue statements
Infinite loop
https://quickref.me/cpp.html 12/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
for (;;) {
std::cout << "infinite loop";
}
#include <iostream>
int main()
{
auto print = [](int num) { std::cout << num << std::endl; };
https://quickref.me/cpp.html 13/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
Break statements
Several variations
https://quickref.me/cpp.html 14/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
# C++ Functions
Arguments & Returns
#include <iostream>
int main() {
std::cout << add(10, 20);
}
Overloading
Built-in Functions
https://quickref.me/cpp.html 15/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
#include <iostream>
#include <cmath> // import library
int main() {
// sqrt() is from cmath
std::cout << sqrt(9);
class MyClass {
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
Creating an Object
https://quickref.me/cpp.html 16/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
Constructors
class MyClass {
public:
int myNum;
string myString;
MyClass() { // Constructor
myNum = 0;
myString = "";
}
};
Destructors
class MyClass {
public:
int myNum;
string myString;
MyClass() { // Constructor
myNum = 0;
myString = "";
}
~MyClass() { // Destructor
cout << "Object destroyed." << endl;
}
};
https://quickref.me/cpp.html 17/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
// Code here...
Class Methods
class MyClass {
public:
int myNum;
string myString;
void myMethod() { // Method/function defined inside the class
cout << "Hello World!" << endl;
}
};
Access Modifiers
class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
protected: // Protected access specifier
int z; // Protected attribute
};
MyClass myObj;
https://quickref.me/cpp.html 18/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
class MyClass {
private:
int myNum;
public:
void setMyNum(int num) { // Setter
myNum = num;
}
int getMyNum() { // Getter
return myNum;
}
};
MyClass myObj;
myObj.setMyNum(15); // Set the value of myNum to 15
cout << myObj.getMyNum() << endl; // Output 15
Inheritance
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut!" << endl;
}
};
https://quickref.me/cpp.html 19/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
Car myCar;
myCar.honk(); // Output "Tuut, tuut!"
cout << myCar.brand + " " + myCar.model << endl; // Output "Ford Mustang"
# C++ Preprocessor
Preprocessor
if elif
else endif
ifdef ifndef
define undef
include line
error pragma
defined __has_include
__has_cpp_attribute export
https://quickref.me/cpp.html 20/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
import module
Includes
#include "iostream"
#include <iostream>
Defines
#define FOO
#define FOO "hello"
#undef FOO
If
#ifdef DEBUG
console.log('hi');
#elif defined VERBOSE
...
#else
...
#endif
Error
Macro
https://quickref.me/cpp.html 21/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
Token concat
Stringification
# Miscellaneous
Escape Sequences
\b Backspace
\f Form feed
\n Newline
\r Return
https://quickref.me/cpp.html 22/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
\t Horizontal tab
\v Vertical tab
\\ Backslash
\? Question mark
\0 Null Character
Keywords
https://quickref.me/cpp.html 23/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
transaction_safe_dynamic
Preprocessor
if elif
else endif
ifdef ifndef
define undef
include line
error pragma
https://quickref.me/cpp.html 24/26
10/17/23, 10:51 AM C++ Cheat Sheet & Quick Reference
defined __has_include
__has_cpp_attribute export
import module
# Also see
C++ Infographics & Cheat Sheets (hackingcpp.com)
C++ reference (cppreference.com)
C++ Language Tutorials (cplusplus.com)
Top Cheatsheet
Recent Cheatsheet
https://quickref.me/cpp.html 25/26