Skip to content

Repository files navigation

C++ Cheatsheet 📜

A collection of C++ snippets that explain how the language works.

More fun stuff ⚡

Made possible by @TheCherno 🚀

Cherno makes programming tutorials focused on C++. The concepts explained in this repo reference his explanations from different videos.

YouTube logo First video

YouTube Video Thumbnail

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 typeSizeRange (approx.)
bool1 byte0 and 1
char1 byte-127 to 127
unsigned char1 byte0 to 255
short (int)2 bytes-32768 to 32767
unsigned short2 bytes0 to 65535
int4 bytes-2147483648 to 2147483647
unsigned int4 bytes0 to 4294967295
long (int)8 bytes-9223372036854775808 to 9223372036854775807
unsigned long8 bytes0 to 18446744073709551615
long long8 bytes-(2^63) to (2^63)-1
unsigned long long8 bytes0 to 18446744073709551615
float4 bytesFloating point numbers
double8 bytesFloating point numbers
long double12 bytesFloating point numbers
auto--

auto is a placeholder for a data type that is automatically deduced by the compiler: Cherno's video

Operators 🧮

https://www.tutorialspoint.com/cplusplus/cpp_operators.htm

OperatorDescription
sizeofReturns 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 🔒

SpecifierDescription
publicMembers are accessible from outside the class
privateMembers cannot be accessed (or viewed) from outside the class
protectedMembers cannot be accessed from outside the class, however, they can be accessed in inherited classes

Other Specifiers 📌

SpecifierIn this repoC++ ReferenceDescription
constconst.cppLinkMakes a variable immutable
staticstatic.cppLinkMakes a variable or function static
externstatic.cppLinkDeclares a variable that is defined in another translation unit or that is defined externally
inlineNULLLinkInstructs the compiler to insert a copy of the code of a function at the point where the function is called at compile time
constexprNULLLinkInstructs the compiler to evaluate the value of an expression at compile time
virtualvirtual.cppLinkDeclares a function that can be overridden in a derived class
overridevirtual.cppLinkSpecifies that a virtual function overrides another virtual function
finalNULLLinkSpecifies that a virtual function cannot be overridden in a derived class or that a class cannot be inherited from
explicitexplicit.cppLinkPrevents a type to be converted to another type implicitly
friendNULLLinkGrants a function or another class access to private and protected members of a class

inline vs constexpr: Explanation

Stack vs Heap 📚

https://www.youtube.com/watch?v=wJ1L2nSIV1s&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&index=54

StackHeap
Initializationint a;int* a = new int;
Memory Allocationint a[5];int* a = new int[5];
Accessa = 2;*a = 2;
DeletionAutomatically when out of scopedelete a;
SizeSmallerLarger
StoredTogether in memoryFragmented
AllocatingCosts around 1 CPU instructionCosts a lot more

Casting 🎭

https://www.tutorialspoint.com/cplusplus/cpp_casting_operators.htm

OperatorDescription
static_castChecks if the type of the expression is compatible with the target type and converts it if possible.
reinterpret_castConverts 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_castModifies the const, volatile, and __unaligned attributes of the type. (usually used to remove const)
dynamic_castReturns a null pointer of the target type if the conversion is not possible.

About

A collection of C++ snippets that explain how the language works.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Contributors

Languages