Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kubgus/cpp-cheatsheet
A collection of C++ snippets that explain how the language works.
https://github.com/kubgus/cpp-cheatsheet
cpp
Last synced: about 11 hours ago
JSON representation
A collection of C++ snippets that explain how the language works.
- Host: GitHub
- URL: https://github.com/kubgus/cpp-cheatsheet
- Owner: kubgus
- License: unlicense
- Created: 2023-11-25T00:48:41.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-02T12:46:55.000Z (about 1 year ago)
- Last Synced: 2024-01-02T19:29:00.720Z (about 1 year ago)
- Topics: cpp
- Language: C++
- Homepage: http://cpp.gustafik.com/
- Size: 63.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# C++ Cheatsheet 📜
A collection of C++ snippets that explain how the language works.### More fun stuff ⚡
- [C++ Playground 🎮](https://github.com/kubgus/cpp-playground)### Made possible by @TheCherno 🚀
Cherno makes programming tutorials focused on C++. The concepts explained in this repo reference his explanations from different videos.
- [Website](https://thecherno.com/)
- [YouTube Channel](https://www.youtube.com/@TheCherno)
- [C++ Playlist](https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb)### Contribution 🤝
Feel free to contribute your code or point out any issues. We're all human, mistakes in this code are inevitable, but unlikely.## Built-in data types 🛠️
https://www.tutorialspoint.com/cplusplus/cpp_data_types.htm
| Data type | Size | Range (approx.) |
| :----------------: | :------: | --------------------------------------------- |
| `bool` | 1 byte | 0 and 1 |
| `char` | 1 byte | -127 to 127 |
| `unsigned char` | 1 byte | 0 to 255 |
| `short (int)` | 2 bytes | -32768 to 32767 |
| `unsigned short` | 2 bytes | 0 to 65535 |
| `int` | 4 bytes | -2147483648 to 2147483647 |
| `unsigned int` | 4 bytes | 0 to 4294967295 |
| `long (int)` | 8 bytes | -9223372036854775808 to 9223372036854775807 |
| `unsigned long` | 8 bytes | 0 to 18446744073709551615 |
| `long long` | 8 bytes | -(2^63) to (2^63)-1 |
| `unsigned long long` | 8 bytes | 0 to 18446744073709551615 |
| `float` | 4 bytes | Floating point numbers |
| `double` | 8 bytes | Floating point numbers |
| `long double` | 12 bytes | Floating point numbers |
| `auto` | - | - |> `auto` is a placeholder for a data type that is automatically deduced by the compiler: [Cherno's video](https://www.youtube.com/watch?v=2vOPEuiGXVo&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&index=56)
## Operators 🧮
https://www.tutorialspoint.com/cplusplus/cpp_operators.htm
| Operator | Description |
| :------: | ----------- |
| `sizeof` | Returns the size of a variable |
| `&` | Returns the address of a variable |
| `*` | Pointer to a variable |
| `? :` | Conditional operator |
| `.` | Member operator |
| `->` | Member operator |
| `++` | Increments the value of a variable |
| `--` | Decrements the value of a variable |
| `+` | Adds two operands |
| `-` | Subtracts two operands |
| `*` | Multiplies two operands |
| `/` | Divides two operands |
| `%` | Modulus operator |
| `=` | Assigns values from right side operands to left side operand |
| `+=` | Adds right operand to the left operand and assign the result to left operand |
| `-=` | Subtracts right operand from the left operand and assign the result to left operand |
| `*=` | Multiplies right operand with the left operand and assign the result to left operand |
| `/=` | Divides left operand with the right operand and assign the result to left operand |
| `%=` | Takes modulus using two operands and assign the result to left operand |
| `==` | Checks if the values of two operands are equal or not. If yes, then the condition becomes true |
| `!=` | Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true |
| `>` | Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true |
| `<` | Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true |
| `>=` | Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true |
| `<=` | Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true |
| `&&` | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true |
| `\|\|` | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true |
| `!` | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make it false |## Access Specifiers 🔒
| Specifier | Description |
| :-------: | ----------- |
| `public` | Members are accessible from outside the class |
| `private` | Members cannot be accessed (or viewed) from outside the class |
| `protected` | Members cannot be accessed from outside the class, however, they can be accessed in inherited classes |## Other Specifiers 📌
| Specifier | In this repo | C++ Reference | Description |
| :-------: | :-----: | :-----------------: | ----------- |
| `const` | [`const.cpp`](https://github.com/kubgus/cpp-cheatsheet/blob/main/const.cpp) | [Link](https://en.cppreference.com/w/cpp/language/cv) | Makes a variable immutable |
| `static` | [`static.cpp`](https://github.com/kubgus/cpp-cheatsheet/blob/main/static.cpp) | [Link](https://en.cppreference.com/w/cpp/keyword/static) | Makes a variable or function static |
| `extern` | [`static.cpp`](https://github.com/kubgus/cpp-cheatsheet/blob/main/static.cpp) | [Link](https://en.cppreference.com/w/cpp/keyword/extern) | Declares a variable that is defined in another translation unit or that is defined externally |
| `inline` | NULL | [Link](https://en.cppreference.com/w/cpp/language/inline) | Instructs the compiler to insert a copy of the code of a function at the point where the function is called at compile time |
| `constexpr` | NULL | [Link](https://en.cppreference.com/w/cpp/language/constexpr) | Instructs the compiler to evaluate the value of an expression at compile time |
| `virtual` | [`virtual.cpp`](https://github.com/kubgus/cpp-cheatsheet/blob/main/virtual.cpp) | [Link](https://en.cppreference.com/w/cpp/language/virtual) | Declares a function that can be overridden in a derived class |
| `override` | [`virtual.cpp`](https://github.com/kubgus/cpp-cheatsheet/blob/main/virtual.cpp) | [Link](https://en.cppreference.com/w/cpp/language/override) | Specifies that a virtual function overrides another virtual function |
| `final` | NULL | [Link](https://en.cppreference.com/w/cpp/language/final) | Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be inherited from |
| `explicit` | [`explicit.cpp`](https://github.com/kubgus/cpp-cheatsheet/blob/main/explicit.cpp) | [Link](https://en.cppreference.com/w/cpp/language/explicit) | Prevents a type to be converted to another type implicitly |
| `friend` | NULL | [Link](https://en.cppreference.com/w/cpp/language/friend) | Grants a function or another class access to private and protected members of a class |> `inline` vs `constexpr`: [Explanation](https://stackoverflow.com/questions/7113872/inline-vs-constexpr)
## Stack vs Heap 📚
https://www.youtube.com/watch?v=wJ1L2nSIV1s&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&index=54
| | Stack | Heap |
| :------------------: | ---------------------------------- | ----------------------------------- |
| Initialization | `int a;` | `int* a = new int;` |
| Memory Allocation | `int a[5];` | `int* a = new int[5];` |
| Access | `a = 2;` | `*a = 2;` |
| Deletion | Automatically when out of scope | `delete a;` |
| Size | Smaller | Larger |
| Stored | Together in memory | Fragmented |
| Allocating | Costs around 1 CPU instruction | Costs a lot more |## Casting 🎭
https://www.tutorialspoint.com/cplusplus/cpp_casting_operators.htm
| Operator | Description |
| :------: | ----------- |
| `static_cast` | Checks if the type of the expression is compatible with the target type and converts it if possible. |
| `reinterpret_cast` | Converts any pointer type to any other pointer type. The operation result is a simple binary copy of the value from one pointer to the other. (type punning) |
| `const_cast` | Modifies the const, volatile, and __unaligned attributes of the type. (usually used to remove `const`) |
| `dynamic_cast` | Returns a null pointer of the target type if the conversion is not possible. |